Link to home
Start Free TrialLog in
Avatar of military donut
military donutFlag for United States of America

asked on

MS Access VB onFocus

hello,

I have been trying to run this code:

Private Sub FName_Exit(Cancel As Integer)

If IsNull(Me!FName) Then
MsgBox "Eh...fill this in!"
Me.FName.SetFocus
End If

End Sub


But every time it changes to the next field on the form on Access 2007

I can't seem to get it to stay on that field.  I just want it to make sure the field is not null or empty.  Actually I want to have the button I have setup to check then entire form for this...the button is the Add Record/Save but can't figure out how to do this...



Any help?

Ernest
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There are several ways to approach this problem.  
1. Code as described above.
2. Make the fields required in the table and then put code to change the Field Labels (To Red or Astricks, etc) to highlight the missing values when you click the save button.
3. Put the code in the lost focus (Property Sheet of field) to highlight the field missing informaiton.

As for what you describe above, I think it is because you are using the 'On Exit' which means the focus is no longer on the field that you are trying to take an action on.  

As a side note, if you make the field required in the table it will not allow you to add the record until all the required fields are entered.  In addition, you can put requirements in the table that control the formating (ie phone number (###) ###-####) or require the value to be in a range, etc.
Right idea, wrong code, wrong event
Private Sub FName_Exit(Cancel As Integer)
 If IsNull(Me!FName) Then
 MsgBox "Eh...fill this in!"
 Me.FName.SetFocus
 End If
 End Sub


try

Private Sub FName_LostFocus()
Dim Response as integer
If nz(Me.FName.Value,"") = ""Then
    Response =MsgBox ("Eh...fill this in!  If you REALLY don't want/need to, click NO",VbYesNO, "Fill it in")
    If response = vbno then exit sub
    Me.FName.SetFocus
End If

End Sub
Avatar of military donut

ASKER

Great! Works perfectly