找不到匹配项的SQL会导致MsgBox

时间:2014-09-04 18:08:10

标签: vba access-vba ms-access-2010

我的表单获取用户输入的数据,构造SQL语句并返回结果。我想在找不到匹配项时弹出一个消息框。

我目前的代码/想法:

If qdf.sql = 0 Then
        MsgBox "No clients matching your information." & _
        vbCrLf & "have been found. Please try again." & _
        , vbCritical, "No Matches"
    Else
        DoCmd.OpenForm "frmSearchResults"
        Me.Visible = False
End If

我无法确定if qdf.sql = 0的正确语法。

更新:完整查询

Private Sub cmdSearch_Click()

'On Error GoTo cmdSearch_Click_err

Dim db As Database
Dim strSQL As String
Dim rs As DAO.Recordset
Dim qdf As QueryDef
Dim strClientID As String
Dim strLastName As String
Dim strFirstName As String
Dim strDOB As String

Set db = CurrentDb
Set rs = db.OpenRecordset(qdf.sql)

' call QueryCheck module to determine if query exists
If Not QueryExists("qrySearch") Then
    Set qdf = db.CreateQueryDef("qrySearch")
Else
    Set qdf = db.QueryDefs("qrySearch")
End If

' handle nulls in the user's entries
    If IsNull(Me.txtClientID.Value) Then
        strClientID = " Like '*' "
        Else
        strClientID = "='" & Me.txtClientID.Value & "' "
    End If

    If IsNull(Me.txtLastName.Value) Then
        strLastName = " Like '*' "
        Else
        strLastName = " Like '" & Me.txtLastName.Value & "*' "
    End If

    If IsNull(Me.txtFirstName.Value) Then
        strFirstName = " Like '*' "
        Else
        strFirstName = " Like '*" & Me.txtFirstName.Value & "*' "
    End If

    If IsNull(Me.txtDOB.Value) Then
        strDOB = " Like '*' "
        Else
        strDOB = "='" & Me.txtDOB.Value & "' "
    End If

strSQL = "SELECT Clients.* " & _
         "FROM Clients " & _
         "WHERE Clients.clientid" & strClientID & _
         "AND Clients.namelast" & strLastName & _
         "AND Clients.namefirst" & strFirstName & _
         "AND Clients.birthdate" & strDOB & _
         "ORDER BY Clients.namelast,Clients.namefirst;"

Debug.Print strSQL

' check to see if the results form is open and close if it is
DoCmd.Echo False

If Application.SysCmd(acSysCmdGetObjectState, acForm, "frmSearchResults") = acObjStateOpen Then
    DoCmd.Close acForm, "frmSearchResults"
End If

' run SQL statment
qdf.sql = strSQL

' check for no matches found
    If rs.RecordCount = 0 Then
        MsgBox "No clients matching your information were found." & _
        vbCrLf & "Please search again.", vbInformation, "No Matches"
    Else
        DoCmd.OpenForm "frmSearchResults"
        Me.Visible = False
    End If

'cmdSearch_Click_exit:
'    DoCmd.Echo True
'    Set qdf = Nothing
'    Set db = Nothing

'Exit Sub

'cmdSearch_Click_err:
'        MsgBox "An unexpected error has occurred." & _
'        vbCrLf & "Please note of the following details and contact the EIIS support desk:" & _
'        vbCrLf & "Error Number: " & Err.Number & _
'        vbCrLf & "Description: " & Err.Description _
'        , vbCritical, "Error"
'    Resume cmdSearch_Click_exit

End Sub

2 个答案:

答案 0 :(得分:0)

如果您有任何ADO经验,可以使用类似

的内容
dim strSQL as String
dim conn as Connection
dim cmd as Command
dim rs as Recordset

(在此处设置连接/命令)

cmd.commandtext = (your select query)
set rs = Command.execute

if rs.eof then        

(或者,如果rs.recordcount = 0,则返回记录计数需要使用正确的cursortype - 通常是adOpenStatic)

'msgbox no match

else
   'do stuff

如果其中任何一个是外星人,那么发布您的实际查询,我会尽力给您完整的代码。祝你好运!

答案 1 :(得分:0)

If qdf.sql = 0 then无法执行正确检查的原因是qdf包含有关查询的信息,例如您在该语句中检查的SQL文本,但不包含结果。

要获取查询结果,您需要在构建查询后将其分配给Recordset。因此,首先构建您的查询,然后将其分配给记录集。

Dim db as DAO.Database
Set db = CurrentDb

Dim qdf as DAO.Querydef
Set qdf = db.CreateQueryDef("qrySearch")

Dim rs as DAO.Recordset
Set rs = CurrentDb.OpenRecordset(qdf.sql)

然后,您可以检查您的记录集已返回的内容。

If rs.RecordCount = 0 then

因此,如果您有' run SQL statment行,则需要放置Set rs行。