使用Select WHERE Query Access检查数据库是否找到匹配项

时间:2013-06-11 16:36:57

标签: vba

伙计我正在使用选择查询使用vb访问代码,我检查记录是否匹配没有问题,我遇到问题,如果sTextbox与数据库中的值不匹配...它应该打印“没有记录匹配“只有一次,但在这个声明中,它打印出MsgBox(”无记录匹配“)无数次,直到数据库中的记录全部被比较...我无法添加退出操作,因为它只会运行语句一次,它不会将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

结束如果

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分: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