Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting focus to a field, on a form in Access 97 1

Status
Not open for further replies.

mrfilez

Technical User
Apr 10, 2001
50
0
0
US
I am setting certain fields as being required. I would like for the cursor to automatically go to the field that is required, but not filled in. This is what I have so far.

Private Sub txtFirstName_BeforeUpdate(Cancel As Integer)

If IsNull(txtFirstName) Then
'Checks to see if Information has been input into field.
MsgBox txtFirstName.Name & " is required!"
'This field is a required field.
txtFirstName.SetFocus
Exit Sub
End If

End Sub

Private Sub txtLastName_BeforeUpdate(Cancel As Integer)

'Checks to see if Information has been input into field.
'This field is a required field.

If IsNull(txtLastName) Then
MsgBox txtLastName.Name & " is required!"
txtLastName.SetFocus
Exit Sub
End If

End Sub

Private Sub cboTitle_BeforeUpdate(Cancel As Integer)

'Checks to see if Information has been input into field.
'This field is a required field.

If Nz(cboTitle) Then
MsgBox cboTitle.Name & " is required!"
cboTitle.SetFocus
Exit Sub
End If


End Sub

Private Sub txtReportsTo_BeforeUpdate(Cancel As Integer)

'Checks to see if Information has been input into field.
'This field is a required field.

If IsNull(txtReportsTo) Then
MsgBox txtReportsTo.Name & " is required!"
txtReportsTo.SetFocus
Exit Sub
End If


End Sub

It does not set the focus back to the txtFirstName field.
Any idea what I am doing wrong here? I have 4 different fields on this form. My fields go in this order: txtFirstName, txtLastName, cboTitle, txtReportsTo.
According to what I have read, and Lonnie Johnson this should work.

Thanks,
Don Thank you for your help and time. It is greatly appreciated!!!

Thanks,
Don
please CC both addresses
f613493c@mailfxhome.fedex.com
mrfilez@midsouth.rr.com
 
You don't understand when the controls' BeforeUpdate event fires. It fires after data is entered into the field (and before it's stored in the record).

You're probably thinking of the form's BeforeUpdate event, which is where this kind of validation logic is normally placed. You could take the bodies of these 4 event procedures, concatenate them, and put them in the Form_BeforeUpdate event procedure, and it would do what you want...almost. You also need to set the Cancel argument to True before you exit; otherwise the update will occur anyway, and Access will (usually) move to the next record.

BTW, you have used Nz for the third control, rather than IsNull. Rick Sprague
 
Thanks for the info. I am about to make the changes you have told me about. I changed the IsNull for the combo box to NZ, because I could not get the combo box to be a required field. Lonnie told me to try the Nz and it worked. It may not work on the Forms BeforeUpdate, but it did work for the Control. Thank you for your help and time. It is greatly appreciated!!!

Thanks,
Don
please CC both addresses
f613493c@mailfxhome.fedex.com
mrfilez@midsouth.rr.com
 
I have a question. How do I tell if code is set at the control level or at the Form level? Thank you for your help and time. It is greatly appreciated!!!

Thanks,
Don
please CC both addresses
f613493c@mailfxhome.fedex.com
mrfilez@midsouth.rr.com
 
I'm not sure I understand the question. If the event procedure name is "Form_BeforeUpdate", it's at the form level. If it's "controlname_BeforeUpdate", it's at the control level.

You create a Form_BeforeUpdate event procedure in one of two ways. In the module window, you can choose Form from the object combo box at the top left, then choose BeforeUpdate from the combo box at the top right. Or, in the form Design View, you can click the little square to the left of the ruler bar at the top, then go to the properties pane, scroll to the On BeforeUpdate event, set it to "[Event procedure]", and click the "..." builder button to the right of the property.

Does that address your question? Rick Sprague
 
Yes, that is it. Thanks. I thought that that was the answer, but I like to make sure.

Thank you for your help and time. It is greatly appreciated!!!

Thanks,
Don
please CC both addresses
f613493c@mailfxhome.fedex.com
mrfilez@midsouth.rr.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top