Error Handling????

  • Thread starter Thread starter Strow
  • Start date Start date
S

Strow

I am new to this so I will try to explain this as best as I can. I have
a form and i want to have the values for fname,lname, and phone locked
so that you must enter a value. im having a little trouble as I am a
little rusty in access. i know you can go into the table to set the
properties to required, but I dont want to have it that way.I want it
to have the message saying "you must have whatever" and not let me
leave that area. any help would be greatly appreciated this is what i
have, but you can still tab to other fields on the report and other
reecords as well. i dont want to tab outside the box or no another
record. Seems like the main problem is she wont set her focus to the
required fields. please help me if you can.


Private Sub FirstName_LostFocus()
On Error GoTo ErrorHandler


If IsNull(FirstName) = True Then
MsgBox "You Must Enter A First Name"
End If


ErrorHandler:
FirstName.SetFocus
End Sub


Private Sub LastName_LostFocus()


If IsNull(LastName) = True Then
LastName.SetFocus
MsgBox "You Must Enter A Last Name"
End If
End Sub


Private Sub Telephone_LostFocus()


If IsNull(Telephone) = True Then
Telephone.SetFocus
MsgBox "You Must Enter A Telephone Number"
End If
End Sub
 
Strow said:
I am new to this so I will try to explain this as best as I can. I
have a form and i want to have the values for fname,lname, and phone
locked so that you must enter a value. im having a little trouble as
I am a little rusty in access. i know you can go into the table to
set the properties to required, but I dont want to have it that way.I
want it to have the message saying "you must have whatever" and not
let me leave that area. any help would be greatly appreciated this is
what i have, but you can still tab to other fields on the report and
other reecords as well. i dont want to tab outside the box or no
another record. Seems like the main problem is she wont set her focus
to the required fields. please help me if you can.

For what it's worth, I don't much like the practice of trapping a user
in a particular control and not letting them out unless they enter
something. Suppose the user wants to fill in the fields out of order?
Why shouldn't she be allowed to? That doesn't mean you have to let her
save the record with incomplete information.

I would take a two-pronged approach. First, to ensure data integrity, I
would set these fields as Required in the table design. Then I would
run code in the form's BeforeUpdate event to check that all required
fields have been filled in, and cancel the update with an appropriate
error message if they haven't. For simplicity's sake, I usually do this
by setting each required control's Tag property to "Required", and then
using a simple BeforeUpdate event procedure like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)

Cancel = fncRequiredFieldsMissing(Me)

End Sub

The fncRequiredFieldsMissing() function is defined in a standard module
like this:

'----- start of function code -----
Function fncRequiredFieldsMissing(frm As Form) As Boolean

Dim ctl As Access.Control
Dim strErrCtlName As String
Dim strErrorMessage As String
Dim lngErrCtlTabIndex As Long
Dim blnNoValue As Boolean

lngErrCtlTabIndex = 99999999 'more than max #controls

For Each ctl In frm.Controls
With ctl
Select Case .ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If .Tag = "Required" Then
blnNoValue = False
If IsNull(.Value) Then
blnNoValue = True
Else
If .ControlType = acTextBox Then
If Len(.Value) = 0 Then
blnNoValue = True
End If
End If
End If
If blnNoValue Then
strErrorMessage = strErrorMessage & vbCr & _
" " & .Name
If .TabIndex < lngErrCtlTabIndex Then
strErrCtlName = .Name
lngErrCtlTabIndex = .TabIndex
End If
End If
End If
Case Else
' Ignore this control
End Select
End With
Next ctl

If Len(strErrorMessage) > 0 Then
MsgBox "The following fields are required:" & vbCr & _
strErrorMessage, _
vbInformation, "Required Fields Are Missing"
frm.Controls(strErrCtlName).SetFocus
fncRequiredFieldsMissing = True
Else
fncRequiredFieldsMissing = False
End If

End Function
'----- end of function code -----

If any required fields are missing, a message is displayed listing them,
and the focus is set to the first "missing" control in the tab order.
 
Dirk, thanks alot my friend, its almost the ned of my work day here,
but let me assure you, i will definately take your example coding into
effect when i meet with my boss tomorrow, thanks a lot
 
hi,
you don't need to keep them locked up in a text box untill they enter data.
just don't let them run the main code. i assume you have a button with a
click event that runs the main code.
put something like this at the very start.
sub runmaincode()
If IsNull(me!FirstName) Then
MsgBox "You Must Enter A First Name"
me.FirstName.setfocus
exit sub
else
If IsNull(me!LastName) Then
MsgBox "You Must Enter A Last Name"
me.LastName.setfocus
exit sub
if isnull(me.telephone) then
Msgbox "you must enter a phone number"
me.telephone.setfocus
exit sub
end if
end if
end if

regards
FSt1
 
Although the MVPs may be well aware of the Tag property, its use here
transforms this into something almost sublime.

Very elegant. Thank you.
 
Chaim said:
Although the MVPs may be well aware of the Tag property, its use here
transforms this into something almost sublime.

Very elegant. Thank you.

I am honored, sir.
 
FSt1

i dont have a click event button to run the main code, textbox has its
own code with a lost focus event. im not quite sure i understand what
you are saying here
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top