I personally would AVOID the
SENDKEYS
command
At one time
SENDKEYS
was very useful and often the only way to accomplish some tasks; however, it can be very flaky as one cannot always be sure what object has the focus due to the user interaction! Say the user has your application and Word open. I know, 99.9% of the time in this case, most likely not an issue... it's the 0.1% that will cause you the most heartburn.
One option I use is a form level variable that I set for check points such as this:
Open your form in design view.
<alt><f11> to open the VBA Editor Window
Show the code for your form (easy enough, on the left should be the navigation pane, your form should be listed, simply right-click and show code
You should have the:
- Option Compare Database
-
Option Explicit
lines in this module (and all others IMHO read the trouble shooting section here>
> Before Posting (VBA or SQL) Code as for why this is so importaint
Just below these two lines insert
- Private zFailSafe As Boolean
or any other name you'd like to give it
I tend to set this value to False in the form's on_load event just to make sure of the value.
Now in your code
-
Private Sub Txt_Cust_Name_Exit(Cancel As Integer)
-
If not zFailSafe then
-
If IsNull(Me.Txt_Cust_Name) Then
-
-
MsgBox "This is a Required Field." & vbCrLf & "Please Enter the Customer Name Before Proceeding"
-
Cancel = True
-
Me.Txt_Cust_Name.SetFocus
-
-
End If
-
end if
-
-
End Sub
In your close and open command button set the zFailSafe to True:
- Private Sub Cmd_Open_Main_Menu_06_Click()
-
-
zFailSafe = True
-
MsgBox "This New Customer Has NOT Been Added"
-
DoCmd.Close acForm, "Frm_New_Customer", acSaveNo
-
-
DoCmd.OpenForm "Frm_Main_Menu", acNormal
-
-
End Sub
Now this way, your control's exit event my fire; however, nothing happens if the "Open_Main_Menu" is used to close the form.
>> As for checking the data during entry (using the before_update event)... a very good point and if the form is bound something that one really should consider - I usually do this at the table level for most data validation which helps to keep the form's code cleaner; however, there are some situations that one simply must code for to ensure proper data entry.