RecordCount of current RecordSource

coasterman

Registered User.
Local time
Today, 06:53
Joined
Oct 1, 2012
Messages
59
I have a form which filters records by a number of different query. The query is selected from a combo box on the main form and this works fine.

What I would like to do is prevent the recordsource from being changed to a query where there are no matched records.

I've being playing with some code but am not getting to far.

Private Sub cboQueryPicker_AfterUpdate()


Code:
    Dim myquery As String

    On Error GoTo Errorhandler
    myquery = Me.cboQueryPicker.Column(1)
    
    

    Me.RecordSource = myquery
    If Not RecordCount > 0 Then
    MsgBox "No records to display"
    End If
   
Exit_Point:
    Exit Sub

    'If user cancels the paramater search then exit the sub at the Exit_Point: above
Errorhandler:
    Select Case Err.Number
    Case 2001, 2501    ' "Cancel" error codes
        Resume Exit_Point
    Case Else
        MsgBox Err.Description, vbExclamation, "Error " & Err.Number
        Resume Exit_Point
    End Select

End Sub

I suspect the method isn't supported. I'm not at all familiar with DAO but would that be the way to do it?
 
You could also test with DCount():

If DCount("*", myquery) > 0 Then

and only set the source if it returns records.
 
Thank you both. This will be a good entry for me into DAO I think and I have now got the code working with your suggestion Pat.

I had also completely overlooked the DCount function, I'm completely sold on the simplicity of it.
 

Users who are viewing this thread

Back
Top Bottom