require entry in text box

  • Thread starter Thread starter Sean
  • Start date Start date
S

Sean

Could someone give me an example of an event procedure
(onlostfocus at form level) that would ensure the user
has made an entry in several text boxes. The data does
not need to be in any format just not null.

Much appreciated

Thanks Sean
 
If you want to verify that data have been entered before a user can leave a
textbox, use the textbox's BeforeUpdate event:

Private Sub TextBoxName_BeforeUpdate(Cancel As Integer)
If Len(Me.TextBoxName & "") = 0 Then
Cancel = True
MsgBox "You must enter a value here!"
End If
End Sub

Note that the above will work only if the user actually "enters" the
textbox.

The foolproof way to do this, assuming that all the important textboxes are
bound to fields in the form's RecordSource, is to use the form's
BeforeUpdate event to do something similar:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Len(Me.TextBoxName1 & "") = 0 Then
Cancel = True
MsgBox "You must enter a value in TextBoxName1!"
Me.TextBoxName1.SetFocus
ElseIf Len(Me.TextBoxName2 & "") = 0 Then
Cancel = True
MsgBox "You must enter a value in TextBoxName2!"
Me.TextBoxName2.SetFocus
ElseIf Len(Me.TextBoxName3 & "") = 0 Then
Cancel = True
MsgBox "You must enter a value in TextBoxName3!"
Me.TextBoxName3.SetFocus
End If
End Sub
 
-----Original Message-----
If you want to verify that data have been entered before a user can leave a
textbox, use the textbox's BeforeUpdate event:

Private Sub TextBoxName_BeforeUpdate(Cancel As Integer)
If Len(Me.TextBoxName & "") = 0 Then
Cancel = True
MsgBox "You must enter a value here!"
End If
End Sub

Note that the above will work only if the user actually "enters" the
textbox.

The foolproof way to do this, assuming that all the important textboxes are
bound to fields in the form's RecordSource, is to use the form's
BeforeUpdate event to do something similar:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Len(Me.TextBoxName1 & "") = 0 Then
Cancel = True
MsgBox "You must enter a value in TextBoxName1!"
Me.TextBoxName1.SetFocus
ElseIf Len(Me.TextBoxName2 & "") = 0 Then
Cancel = True
MsgBox "You must enter a value in TextBoxName2!"
Me.TextBoxName2.SetFocus
ElseIf Len(Me.TextBoxName3 & "") = 0 Then
Cancel = True
MsgBox "You must enter a value in TextBoxName3!"
Me.TextBoxName3.SetFocus
End If
End Sub
Hi Sean,
ken has given a good example. however you may find it more
efficient using the following as an example.

dim ctl as access.control
dim strErrMsg as string
dim fNotComplete as boolean

for each ctl in controls
with ctl
select case .controltype
case actextbox
if len(.value & "")<1 then
fNotComplete =true
end if
case accombobox
if isnull(.value) then
fNotComplete =true
end if
end select
if fNotComplete then
' have error statement in control tag if control specific
strerrmsg=.tag
msgbox strerrmsg,vbexclamation,"Doh!"
..focus
exit for
end if
next ctl
 
Back
Top