0

伙计们,我正在使用 select where 使用 vb 访问代码进行查询,我检查记录是否匹配没有问题,如果 sTextbox 与数据库中的值不匹配,我遇到问题......它应该打印“No Record Match " 仅一次,但在此语句中,它会多次打印出 MsgBox("No Record Match") 直到数据库中的记录已全部比较...我无法添加 Exit Do,因为它只会运行该语句一次并且它不会将 sTextbox 与数据库的所有值进行比较......我不允许使用任何其他方式,除了 sql 查询语句。非常感谢您的帮助。

Dim MyDB As DAO.Database, MyRec As DAO.Recordset, MyList As String
Dim fQue As String, lQue As String, tQue As String

Set MyDB = CurrentDb

If IsNull(sTextbox) Or sTextbox = "" Then
     MsgBox ("You must enter Value in Search Box")
End If

If (sTextbox <> "") Or (sTextbox <> "") Then
MsgBox ("There is value")


Set MyDB = CurrentDb
Set MyRec = MyDB.OpenRecordset("SELECT * From clientInfo")


Do While Not MyRec.EOF 'Loop to check if sTextbox = tQue
  fQue = MyRec![FirstName]
  lQue = MyRec![LastName]
  tQue = MyRec![towerUnit]
  MyRec.MoveNext

    If (sTextbox = tQue) Then
       Set MyDB = CurrentDb
       Set MyRec = MyDB.OpenRecordset("SELECT * From clientInfo where TowerUnit='" & sTextbox & "'")

       FullName = lQue & ", " & fQue
       MsgBox (FullName)
       ClientTextbox.Value = lQue & ", " & fQue
        UnitTextbox.Value = tQue
        Exit Do
    End If

    If (sTextbox <> tQue) Then

        MsgBox ("No Record Found")
        ''Problem lies here because it prints out MsgBox until the While loop is false...
    End If

Loop

万一

4

2 回答 2

0
Dim MyDB As DAO.Database, MyRec As DAO.Recordset
Dim fQue As String, lQue As String, tQue As String

Set MyDB = CurrentDb

If Len(sTextbox)=0 Then
    MsgBox ("You must enter Value in Search Box")
    Exit Sub
End If

Set MyDB = CurrentDb

Set MyRec = MyDB.OpenRecordset("SELECT * From clientInfo where TowerUnit='" _
                                & sTextbox & "'")

If Not MyRec.EOF Then

    fQue = MyRec![FirstName]
    lQue = MyRec![LastName]
    tQue = MyRec![towerUnit]

    ClientTextbox.Value = lQue & ", " & fQue
    UnitTextbox.Value = tQue

Else

    MsgBox ("No Record Found")

End If
于 2013-06-11T17:35:31.027 回答
0
Dim recordNotFound as Boolean = False
<Your While Loop>  
 ...

  If (sTextbox <> tQue) Then
        recordNotFound = True
        ''Or you can do something more complicated like recordNotFound = recordNotFound Or True depending on what you are trying to accomplish
        ''Problem lies here because it prints out MsgBox until the While loop is false...
    End If
 ...
<End Of your While Loop>

if recordNotFound Then MsgBox ("No Record Found")
于 2013-06-11T16:43:33.053 回答