D
DS
What is the Code for Open Form but Do Not open or Do Nothing actually if
there are No Records found?
Thanks
DS
there are No Records found?
Thanks
DS
DS said:What is the Code for Open Form but Do Not open or Do Nothing actually
if there are No Records found?
Thanks
They really aren'y putting any records in.. the form has a list box onBrian said:Try this, using the table name & its first field name (of the table that is
the record source of the form being opened) in the DCount.
Private Sub Form_Open(Cancel As Integer)
If DCount "[FirstFieldName]","[TableName]") = 0 Then
MsgBox "No records found."
Cancel = True
Exit Sub
End Sub
This will display the message if there are no records, then cancel opening
the form. The question is: How will anyone put anything into the table if
they cannot open its form until there are records in its underlying table?
:
What is the Code for Open Form but Do Not open or Do Nothing actually if
there are No Records found?
Thanks
DS
Hi, Thank you for all of the input. I'll give this a try. It's for useDirk said:There are several ways to do this.
1. Check if there will be records before opening the form. Note that
this actually involves querying the data twice, so I don't like it much.
But code might look like this:
If Not IsNull(DLookup("SomeField", "SomeTableOrQuery")) Then
DoCmd.OpenForm "YourForm"
End If
2. Open the form and use code in its Open event to cancel its own
opening if there are no records:
' ----- code for form module -----
Private Sub Form_Open(Cancel As Integer)
If Me.Recordset.RecordCount = 0 Then
Cancel = True
End If
End Sub
' ----- end code for form module -----
When you do this, if you open the form from code you also have to
trap the error (2501) that will be raised due to the opening being
cancelled. The simplest example would be:
On Error Resume Next
DoCmd.OpenForm "Your Form"
If Err.Number <> 0 Then
If Err.Number <> 2501 Then
' This is a real error!
MsgBox Err.Description, vbExclamation, "Error " &
Err.Number
End If
End If
3. Open the form invisibly, check to see if it has any records, and
either make it visible or close it accordingly:
DoCmd.OpenForm "YourForm", WindowMode:=acHidden
With Forms!YourForm
If .Recordset.RecordCount = 0 Then
MsgBox "No records to display."
DoCmd.Close acForm, "YourForm", acSaveNo
Else
.Visible = True
End If
End With
DS said:They really aren'y putting any records in.. the form has a list box on
it that is populated from a listbox on the first form. I just don't
want that second form to show if there is nothing in the listbox on
the second form. I hope this works because the Listbox on the second
form is set to populate from the first forms after update property.
Thanks for the help.
Dirk said:There are several ways to do this.
1. Check if there will be records before opening the form. Note that
this actually involves querying the data twice, so I don't like it much.
But code might look like this:
If Not IsNull(DLookup("SomeField", "SomeTableOrQuery")) Then
DoCmd.OpenForm "YourForm"
End If
2. Open the form and use code in its Open event to cancel its own
opening if there are no records:
' ----- code for form module -----
Private Sub Form_Open(Cancel As Integer)
If Me.Recordset.RecordCount = 0 Then
Cancel = True
End If
End Sub
' ----- end code for form module -----
When you do this, if you open the form from code you also have to
trap the error (2501) that will be raised due to the opening being
cancelled. The simplest example would be:
On Error Resume Next
DoCmd.OpenForm "Your Form"
If Err.Number <> 0 Then
If Err.Number <> 2501 Then
' This is a real error!
MsgBox Err.Description, vbExclamation, "Error " &
Err.Number
End If
End If
3. Open the form invisibly, check to see if it has any records, and
either make it visible or close it accordingly:
DoCmd.OpenForm "YourForm", WindowMode:=acHidden
With Forms!YourForm
If .Recordset.RecordCount = 0 Then
MsgBox "No records to display."
DoCmd.Close acForm, "YourForm", acSaveNo
Else
.Visible = True
End If
End With
DS said:[...]Dirk said:There are several ways to do this. [...]
2. Open the form and use code in its Open event to cancel its own
opening if there are no records:
' ----- code for form module -----
Private Sub Form_Open(Cancel As Integer)
If Me.Recordset.RecordCount = 0 Then
Cancel = True
End If
End Sub
' ----- end code for form module -----
When you do this, if you open the form from code you also have to
trap the error (2501) that will be raised due to the opening being
cancelled. The simplest example would be:
On Error Resume Next
DoCmd.OpenForm "Your Form"
If Err.Number <> 0 Then
If Err.Number <> 2501 Then
' This is a real error!
MsgBox Err.Description, vbExclamation, "Error " &
Err.Number
End If
End If
I chose option Number 2 and it works great!...
The only thing I don't
understand is where to put this error code.
Does it go after the code on the On Open Property. or does it go on
the On Error property of the form?
Brian said:Sorry about my late post, Dirk. I started it before yours was posted,
got called away to something else, and then finished it without first
checking for other responses...