Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"On No Data" event for forms? 2

Status
Not open for further replies.

Turb

Technical User
Feb 11, 2003
154
US
Hi all!
I have used the "On No Data" event on a few Reports before, but does anyone know how to do something like this for forms?

Thanks!


- Turb
 
Here are some notes:

Code:
Private Sub Form_Open(Cancel As Integer)
Dim strRecError
 
If Me.RecordSource <> "" Then
    If Me.Recordset.RecordCount = 0 Then
        strRecError = "No Records. "
    End If
Else
    strRecError = "No recordset. "
End If
If strRecError <> "" Then
    If MsgBox(strRecError & "Continue?", vbYesNo) = vbNo Then
        Cancel = True
    End If
End If
End Sub
 
Yes,

Shamelessly borrowed from one of Remou's posts somewhere...[wink]
Code:
Private Sub Form_Open(Cancel As Integer)
Dim strRecError
 
If Me.RecordSource <> "" Then
    If Me.Recordset.RecordCount = 0 Then
        strRecError = "No Records. "
    End If
Else
    strRecError = "No recordset. "
End If
If strRecError <> "" Then
    If MsgBox(strRecError & "Continue?", vbYesNo) = vbNo Then
        Cancel = True
    End If
End If
End Sub
That should give you a starting point.

Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Remou...

[rofl]

Haha [lol]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
HarleyQuinn
You got that after my edit. :-D
 
[smile]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
WOW!
Thanks for your suggestions guys! :)

But I'm getting a "Method or data member not found" compile error on this ".Recordset". :(



- Turb
 
Try this:
If Me.RecordsetClone.RecordCount = 0 Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
BINGO!
That did it.
This is what I ended up with on the "MyForm" On Open event:
Code:
Dim strRecError
 
If Me.RecordSource <> "" Then
    If Me.RecordsetClone.RecordCount = 0 Then
        strRecError = "Please try again."
    End If
Else
    strRecError = "Please try again."
End If
If strRecError <> "" Then
    If MsgBox(strRecError, vbExclamation + vbOK, "MyMessageBoxTitle") = vbOK Then
        DoCmd.Close
        DoCmd.OpenForm "MyForm", acNormal
Else
    DoCmd.Close
    End If
End If

My thanks to everyone!


- Turb
 
Whoops! I spoke too soon.

The code above doesn't work properly for me after all (I hide the database window entirely and all forms, etc. are popup. The code as I wrote it above doesn't work properly in that environment).
I had to put "Cancel = True" back into the code (I was trying to stop the "OpenForm action was cancelled" message by replacing it with the "DoCmd.'s") and I also removed the last 'Else' statement.

New Code:
Code:
Dim strRecError

If Me.RecordSource <> "" Then
    If Me.RecordsetClone.RecordCount = 0 Then
        strRecError = "Please try again."
    End If
Else
    strRecError = "Please try again."
End If
If strRecError <> "" Then
    If MsgBox(strRecError, vbExclamation + vbOKOnly, "MyMessageBoxTitle") = vbOK Then
        Cancel = True
    End If
End If


- Turb
 
In that case, check for data before you open the form. DCount should suit.

[tt]If DCount("*","NameOfFormQueryOrTable","OptionalWhere"=0 Then
'No Records
<...>[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top