do command if criteria met

ianatkins

Registered User.
Local time
Today, 17:30
Joined
Oct 25, 2002
Messages
16
Hi,
Ive got a form with a few buttons on it, for example one runs a report based on the information in the form.

I would like some code that when the key field of the form is null and trhe button is pressed then a message box is opened saying something like "This option cannot be run without data" and the command is not run.

Is this possible?

Thanks for any reply
ian.
 
Test if the text box linked to the key field is Null in the OnClick event of your button...

If IsNull(tbKeyField) or tbKeyField = "" Then
MsgBox "This option cannot be run without data. The Key Field can not be empty!"
Else
'Your code here if the keyfield is not null
End If

HTH
 
Last edited:
Try using this code:

If IsNull(youControlNameInHere) = True then
msgbox("This option cannot be run without data",vbCritical,"No Data")
Exit Sub
Else
...code to run your report
End if

Bear in mind that this only tests for a Null and not a zero.

HTH
Rob
 
Thanks for the speedy replies guys.

Here is my existing on click code :

Private Sub logcall_Click()
On Error GoTo Err_logcall_Click
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmcall"

stLinkCriteria = "[ID]=" & Me![ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_logcall_Click:
Exit Sub

Err_logcall_Click:
MsgBox Err.Description
Resume Exit_logcall_Click
End Sub

Where abouts should I put your code, and if the field name is say "Second Name" which variables in your code should I change.

Thanks Alot,
Ian
 
Ian,

Try this:

Private Sub logcall_Click()
On Error GoTo Err_logcall_Click
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmcall"

stLinkCriteria = "[ID]=" & Me![ID]

If IsNull([Second Name]) = True Then
MsgBox("This option cannot be run without data",vbCritical,"No Data")
Exit Sub
Else
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
Exit_logcall_Click:
Exit Sub

Err_logcall_Click:
MsgBox Err.Description
Resume Exit_logcall_Click
End Sub
 
Thanks alot,

The following line throws up a compile error (the line is highlighted red):

MsgBox("This option cannot be run without data",vbCritical,"No Data")

Im using access 97 if this makes a difference ..
Thanks again
Ian.
 
Private Sub logcall_Click()
On Error GoTo Err_logcall_Click

If IsNull([Second Name]) Or [Second Name] = "" Then
MsgBox "This option cannot be run without data.",vbCritical,"No Data"
Exit Sub
Else
DoCmd.OpenForm "frmcall", , , "[ID]=" & Me![ID]
End If
Exit_logcall_Click:
Exit Sub

Err_logcall_Click:
MsgBox Err.Description
Resume Exit_logcall_Click
End Sub

HTH
 
Hi there,

Try ghudson's suggestion as that code tests for Null and a zero length string. Also what is the error message you are getting?
It could be that Access 97 doesn't recognise the vbCritical part. In Access 2K it does. However I think the constant for that is 16 so you may want to try:

MsgBox "This option cannot be run without data.",16,"No Data"

or if that doesn't work then use

MsgBox "This option cannot be run without data.", ,"No Data"

The latter is the default vbOkOnly i.e. just the 'OK' button

HTH
Rob
 

Users who are viewing this thread

Back
Top Bottom