setfocus

awrude

Registered User.
Local time
Today, 14:19
Joined
May 23, 2013
Messages
18
I have a form that the user must enter in information in all the areas. I have it set up with the following on the first one.

Private Sub txtSerial1_Exit(Cancel As Integer)
If Len(Me.txtSerial1 & vbNullString) = 0 Then
MsgBox "Please enter a serial number"
Me.dteDate.SetFocus
Me.txtSerial1.SetFocus
Cancel = True
Exit Sub
End If
End Sub

is there a way i can put this as a function and call it when needed or do i need to put this if then in each EXIT? My issue is how i would do the setfocus.
 
A form has an event called the "on current event" when you move off the particular record you are viewing this on current event runs and this is where you would instruct it to check that all the fields have been filled according to your requirements.
 
You could write code for each individual text box, alternatively you could write some code which accesses the form as an object and the text boxes as a collection within the form. Doing it this way your code would only be called once and have the ability to check all of the text boxes in one go. If this is of interest then you may find this thread helpful:

http://msaccesshintsandtips.ning.com/profiles/blogs/lock-unlock-controls
 
If you want this kind of validation code to work, it has to be set in the Form_BeforeUpdate event! Period! Set in an event tied to a given Control, such as the txtSerial1_Exit event, all the user has to do, to defeat the validation, is to simply not enter the Control!

There are a number of ways to approach this, such as
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

 If Nz(Me.Control1,"") = "" Then
   MsgBox "Control1 Must Not Be Left Blank!"
   Cancel = True
   Control1.SetFocus
   Exit Sub
 End If
 
If Nz(Me.Control2, "") = "" Then
   MsgBox "Control2 Must Not Be Left Blank!"
   Cancel = True
   Control2.SetFocus
   Exit Sub
 End If

End Sub

You could loop through all or some Controls and do the same thing. If the above is too onerous, given your situation, this will loop through all Textboxes and Comboboxes and check that they're populated
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim ctl As Control
Dim CName As String


For Each ctl In Me.Controls
    Select Case ctl.ControlType
        Case acTextBox, acComboBox
            If Nz(ctl, "") = "" Then
              CName = ctl.Controls(0).Caption
              MsgBox "Following field is required: " & vbCrLf & vbCrLf & CName
              Cancel = True
              ctl.SetFocus
              Exit Sub
             End If
    End Select
Next ctl

End Sub

You could also use the Tag Property to mark certain Controls, and then loop through all Controls but only check on/address the status of these 'marked' Controls.


Linq ;0)>
 
Last edited:
If you want this kind of validation code to work, it has to be set in the Form_BeforeUpdate event! Period! Set in an event tied to a given Control, such as the txtSerial1_Exit event, all the user has to do, to defeat the validation, is to simply not enter the Control!



I went this way because i wanted it to do the validation as soon as the tabbed off the textbox. I also have it set up the way you reccomended to cover incase someone bypassed the box.
I actually copied and pasted the code from one of your other replies. Thank you for it. It helped greatly
 
Extra work, but as long as it meets your requirements!

Glad we could help!

Linq ;0)>
 
it does. I have many controls that are very similar do i have named them txtSerial1, txtSerial2, txtSerial3... and so on. I do not know if it can be done but could i run a for/next loop to run through all the similar control names? Something like:

for a = 1 to 4
if null(textSerial & a) then
'procedure here
end if
next a

Is there a way to make something similar to this work
 

Users who are viewing this thread

Back
Top Bottom