View Full Version : Help with Click event and If statement


Dmayes
09-13-2007, 08:05 AM
Ok so I am trying to put in some VBA code to check the Number_Of_Transactions field on the form to see if there is a number in there greater than 0 when the OK command button is clicked. If so I want it to add the record to a table if not I want a message saying enter a number and give the user a chance to enter a number before it writes the record to a table. The code below is from the OK button and it displays the message if the field is blank but it also writes it to the table with the field blank. What am I doing wrong?


Thanks!

Private Sub New_Record_Click()
On Error GoTo Err_New_Record_Click


If Number_of_Transactions > 0 Then
DoCmd.GoToRecord , , acNewRec
Else: MsgBox "Please enter a valid number"
End If


Exit_New_Record_Click:
Exit Sub

Err_New_Record_Click:
MsgBox Err.Description
Resume Exit_New_Record_Click

End Sub

odin1701
09-13-2007, 08:18 AM
Why not just change the table so that this field is required to have data in it?

Dmayes
09-13-2007, 10:03 AM
Well I tired that but I had put in some code so I caould have a Clear button and it was causing an error. I took the clear off and put in a Cancel button to close the form and set it up as being required and that worked.

Thanks!