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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Required Fields 5

Status
Not open for further replies.

drewcp

Programmer
Jul 31, 2008
30
US
how can i set a field on a form as required?

OR

how can i run validation rules in the 'lost focus' field so that you can't just tab over the field and not fill it in?



I know that you would usually set the table property to required but i have the form based off of a query and my tables are all link tables so i can not change the fields to required on the table.

thank you in advance!
 

Fairly standard code for this is:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 If IsNull(Me.Control1) Then
   MsgBox "Control1 Must Not Be Left Blank!"
   Cancel = True
   Control1.SetFocus
 End If
End Sub

You can't use LostFocus because you don't know that they'll even tab into the field!

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
How are ya drewcp . . .

You validate the required field in the [blue]Before Update[/blue] event of the form, using the events cancel arguement to return to the field should validation fail.

Note: with this method, you can't save a record until validation passes.

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
I'm doing better than good aceman, thanks for asking, how are you?

thank you so much both of you for your responses, i plugged this into the before update


Private Sub Combo44_BeforeUpdate(Cancel As Integer)
If IsNull(Me.Combo44) Then
MsgBox "Required Field!"
Cancel = True
Combo44.SetFocus
End If
End Sub


But it gave me the same problem that i was having before where i could save it and move on and it wouldn't validate. It would only run the validation when i would edit the record. It would also give me an error saying that it could not set the focus without it being saved.

so i tried this next one to see if it was a problem with the before update or it i had screwed up the code


Private Sub Combo44_LostFocus()
If IsNull(Me.Combo44) Then
MsgBox "Required Field!"
Cancel = True
Combo44.SetFocus
End If
End Sub


Which this one did what it was supposed to so i knew that the code was right but like you said missinglinq, what if they don't tab into the field at all?

My other problem (sorry i just keep adding to the list) is that it doesn't set the focus back to combo44 so i tried switching out the last statement with this


DoCmd.GoToControl "Combo44"


but that didn't work either

If you have anymore suggestions for me i would so greatly appreciate it.
 

As both Missinglinq and TheAceMan1 stated...
Put your code in the BeforeUpdate event of your Form.


Randy
 
thanks randy, i didn't notice that it was supposed to be in the form event so i was trying to put it in the event for combo 44

it works perfect, thank you all so much!

is there a way that i can tweak the code though so that it will also show on a lost focus of the combo44? because i am going to be doing this for several fields on my form and if it would show that there was an error when it occurred, then they could fix the error right then instead of going all the way through the form and then going back and fixing the errors.
 

That's really a waste of time because more often than not, the users will simply not tab or mouse into the control. Simply stack them in the Form_BeforeUpdate event and it will go thru them one by one, returning focus to the appropriate textbox.

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 
If IsNull(Me.Control1) Then
   MsgBox "Control1 Must Not Be Left Blank!"
   Cancel = True
   Control1.SetFocus
 End If

If IsNull(Me.Control2) Then
   MsgBox "Control2 Must Not Be Left Blank!"
   Cancel = True
   Control2.SetFocus
 End If

If IsNull(Me.Control3) Then
   MsgBox "Control3 Must Not Be Left Blank!"
   Cancel = True
   Control3.SetFocus
 End If

End Sub

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
thank you very much

just wondering though, how are they supposed to do data entry without entering into the control?
 
drewcp said:
[blue] . . . how are they supposed to do data entry without entering into the control?[/blue]
Understand . . . the forms [blue]BeforeUpdate[/blue] event occurs [blue]just before a record is committed/saved![/blue]

The above allows users to enter data as smoothly as they can, skipping what they please!. When they attempt to save and move on to another record ... they'll surely find what they missed via the [blue]Cancel[/blue] arguement of the forms [blue]BeforeUpdate[/blue] event!

If you have other required fields they can easily be added to the code in the event! [thumbsup2]

I hope this is clear [surprise]

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
If you don't want empty or blank field too, replace this:
If IsNull(Me.Control1) Then

with this:
If Trim(Me.Control1 & "") = "" Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Another case of my trying EVERYEVENT on EVERY field except where I should have and that was the Form BeforeUpdate.

Forgot that with a continuous form each record has a form event.

Stars to both TheAceMan1 and PHV.... again
 
How are ya Poduska . . .

To nail this to the wall:
TheAceMan1 said:
[blue]A [purple]fields[/purple] Before Update event is your last chance to take action [purple]before committing that field to a record![/purple]

A [purple]forms[/purple] Before Update event is your last chance to take action [purple]before committing that record to a table![/purple][/blue]
The above involves all form views ... [blue]Single, Continuous, and DataSheet[/blue]. [thumbsup2]

Note: In the sequence of events (form or field), the [blue]After Update[/blue] event ([blue]commit has occured[/blue]) follows the [blue]Before Update[/blue] ... that is unless you take action in the [blue]Before Update[/blue] event using the [purple]Cancel[/purple] property.

[blue]Cheers![/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
drewcp . . .

Peform the following for an explicit example of validating multiple required fields:
[ol][li]In the [blue]Tag[/blue] property of all required fields, add a question mark [blue]?[/blue] (no quotations please).[/li]
[li]Copy/paste the following to the forms [blue]Before Update[/blue] event:
Code:
[blue]   Dim Msg As String, Style As Integer, Title As String
   Dim ctl As Control, DL As String
   
   DL = vbNewLine & vbNewLine
   
   For Each ctl In Me.Controls
      If ctl.Tag = "?" Then
         If Trim(ctl & "") = "" Then
            Msg = "Required field '" & ctl.Name & "' is Missing Data!" & DL & _
                  "You'll have to go back and correct this . . ."
            Style = vbCritical + vbOKOnly
            Title = "Required Data Error! . . ."
            MsgBox Msg, Style, Title
            
            Me(ctl.Name).SetFocus [green]'set focus to the field in error.[/green]
            Cancel = True         [green]'cancel saving the record![/green]
            Exit Sub              [green]'return to the form for correction.[/green]
         End If
      End If
   Next[/blue]
[/li]
[li][blue]Perform your testing![/blue][/li][/ol]
The user makes corrections one by one until all required fields have data.

[blue]Cheers! . . .[/blue]



Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top