No records in form

Lyncroft

QPR for ever
Local time
Today, 18:48
Joined
May 18, 2002
Messages
168
When a user opens a continuous form that doesn't have any records in it I want a message to come up to that effect (the form looks a bit odd with no records in).

Sure this must be pretty easy but cant quite get there.
 
Use this in the OnOpen event of your form...

Code:
Private Sub Form_Open(Cancel As Integer)
    
    If Me.RecordsetClone.RecordCount = 0 Then
        MsgBox "There are zero records in the data source!", vbInformation, "No Records Found"
        DoCmd.Close acForm, Me.Name
    End If
    
End Sub
HTH
 
Thats very helpful thanks.

One more thing though (he says pleadingly) but how would I would I give the option to open the form to add a record, eg, message says "no records found - click yes to enter a record, no to quit"
 
Assuming you are opening the form without any data restrictions...

Code:
Private Sub Form_Open(Cancel As Integer)
    
    Select Case MsgBox("There are zero records in the data source." & vbCrLf & vbLf & "Click the Yes button to open the form to add new records or click the No button to close the form.", vbQuestion + vbYesNo, "Zero Records")
        Case vbYes: 'Open form
            'do nothing, let the form open
        Case vbNo: 'Close form
            DoCmd.Close acForm, Me.Name
        Case Else: 'Default case to trap any errors
            'Do nothing
    End Select
    
End Sub
HTH
 

Users who are viewing this thread

Back
Top Bottom