How to test if query is empty

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • panteraboy
    New Member
    • Apr 2008
    • 48

    How to test if query is empty

    Hi there again folks. Im continuing on from the code posted on SQL Error.

    I wish to display a msg box and prevent a new form from opening if it is an empty recordset. The following comes after the SQL Code in the click event

    Code:
     
             qdf.SQL = strSQL
             Dim msg As String
             msg = "Sorry there are no models in stock with that specification"
           
            If IsNull(qdf.Fields("model")) Then
            MsgBox (msg)
            
            Else
            
             'DoCmd.OpenQuery "Admin_query"
             DoCmd.Close acForm, Me.Name
        
            
             Set qdf = Nothing
             Set db = Nothing
             DoCmd.OpenForm "laptop_specs", , "Admin_query"
             
            End If
    but it still opens a blank white screen as there is no data. Am i on the right path here " If IsNull(qdf.Fiel ds("model")) Then " or is there another method to determine if the query is empty.
    Regards panteraboy
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Originally posted by panteraboy
    Hi there again folks. Im continuing on from the code posted on SQL Error.

    I wish to display a msg box and prevent a new form from opening if it is an empty recordset. The following comes after the SQL Code in the click event

    Code:
     
             qdf.SQL = strSQL
             Dim msg As String
             msg = "Sorry there are no models in stock with that specification"
           
            If IsNull(qdf.Fields("model")) Then
            MsgBox (msg)
            
            Else
            
             'DoCmd.OpenQuery "Admin_query"
             DoCmd.Close acForm, Me.Name
        
            
             Set qdf = Nothing
             Set db = Nothing
             DoCmd.OpenForm "laptop_specs", , "Admin_query"
             
            End If
    but it still opens a blank white screen as there is no data. Am i on the right path here " If IsNull(qdf.Fiel ds("model")) Then " or is there another method to determine if the query is empty.
    Regards panteraboy
    Try the Dlookup() like:
    Code:
    IF IsNull(Dlookup("model","tblX","ID=" & Me.ID) then
       msgbox msg
       end sub
    endif
    You'll need to have a unique key (Here Me.ID) for the WHERE parameter.

    Nic;o)

    Comment

    • panteraboy
      New Member
      • Apr 2008
      • 48

      #3
      Cheers for that Nic. Did the trick nicely. Its easy when you know how lol.
      Regards panterboy

      Comment

      Working...