Primary Key and Form Opening

GraemeG

Registered User.
Local time
Today, 16:22
Joined
Jan 22, 2011
Messages
212
Hi,

I have a basic (pre record selector) form with one text box to enter a primary key to then open the main form to the specific record.
using

Code:
[Private Sub CmdSearch_Click()
DoCmd.OpenForm "Internal Survey Form", , , "Propref = '" & Me.txtPropref & "'"
End Sub

It still opens to a blank form if there is no record matching.
How can I prevent the main form opening and have a message saying no record found?

Thanks
Graeme
 
Use the DCount() Function against the Table or Query to see if the Record exists, using the

"Propref = '" & Me.txtPropref & "'"

in the Criteria of the Function, before opening the Form. IF it doesn't exist, pop up a message box.

Linq ;0)>
 
Use the DCount() Function against the Table or Query to see if the Record exists, using the

"Propref = '" & Me.txtPropref & "'"

in the Criteria of the Function, before opening the Form. IF it doesn't exist, pop up a message box.

Linq ;0)>

Hi Thanks
I havent used the DCount before how cant it be incoprated into what I have or do I have to re-write something else?
 
Code:
Private Sub CmdSearch_Click()
If DCount("*", "YourTableOrQueryName", "[Propref] = '" & Me.txtPropref & "'") > 0 Then
 DoCmd.OpenForm "Internal Survey Form", , , "[Propref] = '" & Me.txtPropref & "'"
Else
 MsgBox "Matching Record Does Not Exist!"
End If
End Sub
You'll need to replace YourTableOrQueryName with the actual name of your Table or Query that "Internal Survey Form" is based on. This also assumes that Propref is defined as a Text Field, which the syntax you're using to open the "Internal Survey Form" would suggest is true.

Linq ;0)>
 
Code:
Private Sub CmdSearch_Click()
If DCount("*", "YourTableOrQueryName", "[Propref] = '" & Me.txtPropref & "'") > 0 Then
 DoCmd.OpenForm "Internal Survey Form", , , "[Propref] = '" & Me.txtPropref & "'"
Else
 MsgBox "Matching Record Does Not Exist!"
End If
End Sub
You'll need to replace YourTableOrQueryName with the actual name of your Table or Query that "Internal Survey Form" is based on. This also assumes that Propref is defined as a Text Field, which the syntax you're using to open the "Internal Survey Form" would suggest is true.

Linq ;0)>

Amazing! Fantastic! Thanks :)
 

Users who are viewing this thread

Back
Top Bottom