beforeUpdate not called...

jalldridge

Registered User.
Local time
Today, 18:51
Joined
May 3, 2005
Messages
60
A stupid question here...

I have a form that has a single bound text box that is set to currency. The table is also set as currency with decimal places set at 2. I have my own save button

Now I want to catch if the user types in text instead of numers and display a suitable error message.

When the user presses the save button I call this code:

DoCmd.RunCommand acCmdSaveRecord

heres the before update event

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo errHandle
    
    If Not IsNumeric(hourlyRate) Then
        
        MsgBox "Please enter a number for the hourly rate", vbInformation
        hourlyRate.SetFocus
        Cancel = True
    Else
        If (hourlyRate < 0) Then
            MsgBox "Please enter a positive hourly rate", vbInformation
            hourlyRate.SetFocus
            Cancel = True
        End If
    End If
    Exit Sub
errHandle:
    MsgBox Err.Description & " " & Err.Number
    Resume Next
End Sub

However the update event isnt called and I get an access message saying that the value you entered isnt correct for the field blah blah blah.

Q - How do I stop this access message from being shown???

btw - If I enter in a negative number my message box is shown, and then i get another access message saying that the run command was cancelled.

arghhh - I dont want these :-)
 
Last edited:
RE : beforeUpdate not called...

validate the field in the on click property of the save button
 
Thanks for the hint...

However I have found that the form_Error event is the first thing that is called, thereby I'm not getting to my other code.

I'm can trap error 2113 here when I do my save and present a msg to the user. One issue with this though...

If I hit my cancel button and I have text in this field then, then the formError event is called and the user is told to put in numeric values. If they are hitting the cancel they dont want to know, just want to cancel. :-)

Any ideas about this one???

I suppose I could make the text box an unbound control and use ADO to do an update query. Seems a bit of overkill for simple error checking...
 
Last edited:
Use the KeyPress event for the text box to only allow numeric values [and these three keys -,.]. Check the help files for ASCII keys if you need to add more.
Code:
Private Sub TextBox1_KeyPress(KeyAscii As Integer)
    
    Select Case KeyAscii
        Case 44 To 46, 48 To 57
            KeyAscii = KeyAscii
        Case Else
            MsgBox "Only numbers are allowed.", vbInformation, "Invalid Key"
            KeyAscii = 0 'key nothing
    End Select
    
End Sub
 
Ah ha!!!

I had started to play with that event but didnt quite get it to work.

Thank you very much for helping me out :-)
 

Users who are viewing this thread

Back
Top Bottom