Mandatory fields/text boxes in a form

M

Mark

Hi,

I need some help. I have a form that I will get a user to
fill out with a couple of mandatory fields/text boxes I'd
like them to fill in. And when they don't fill in the
mandatory field... a message pops up saying that they need
to fill in the field. In addition and my problem, I would
like the cursor to stay on that field before moving onto
the next field/text box.

In the text box properties, I have an [Event Procedure]
for both "AfterUpdate" and "OnExit" which pops up a
message to the user entering data into the form to enter
the information. Which works fine... however, I would
like to have the cursor stay on that particular field/text
box before continuing to the next field. I've tried
placing the code listed below in the "AfterUpdate"
and "OnExit" thinking that this would work, but it moves
the cursor to the next text box once the message
window/prompt is clicked "ok". I even placed ":
TextBoxName.SetFocus" in the If... Then statement thinking
this would work... I've listed the code below. Also, I
have tried turning the "required field properties" to yes
and no. What am I not doing right? Would the "validation
rule" and "validation text" properties be used to do
this. I am sure there is more than one way to get this to
work. I can't seem to figure this out. Can you help me?

Thanks, Mark

Below is the code I have been working with in
the "AfterUpdate" section:


Private Sub Referral_Date_AfterUpdate()
If (IsNull(Me.Referral_Date) Or Me.Referral_Date = "")
Then
MsgBox "This is a mandatory field, please enter a
response here before proceeding. If not, you will be asked
again to enter this data before exiting this page.":
Referral_Date.SetFocus
End If
End Sub
 
H

HSalim

Mark,
This is a difficut question to answer and requires a little programming
effort.

1. Not all events have a cancel property. The exit event, which is
cancellable does not always fire.
For example if the user switches to another form that is open the exit
event does not fire. (Although when the user
switches back to the current form, the focus remains on the field that
you left, unless you have set it elsewhere using code.)

2. The SetFocus Method cannot be used in an exit or lost focus event to set
focus back to itself.
This is by design. Remember it is not a cancellable event.

So, What are your choices?

a. you can force the user to input the value before he leaves the scene by
using an input box
Dim resp As String
resp = "12/31/2002"
Do While CDate(resp) < "1/1/2003"
resp = InputBox("you must enter a valid date", "Mandatory field",
Date)
If Not IsDate(resp) Then
resp = "1/1/1900"
End If
Loop
me.Referral_date.value = resp
However, input boxes cannot be used in many cases - for example in a listbox
and, if the control has other validation that would need to be duplicated
after the inputbox.
for these reasons, you might choose to

b. If the Exit event fires, you can cancel the exit by using Cancel = True
However, if it does not, you can still bring the focus back by this
method:
1. Declare two variables at the form module level
blnGoBack as boolean, strCtlName as string

2. Add this code to the module:
Function CheckFocus()
If blnGoback Then
Forms!Form3!(sCtlName).SetFocus ' ofcourse use realnames here
End If
End Function

In the afterupdate and lost focus events
use some logic to toggle blnGoback

if me.referraldate = "" then
blnGoback = True
sCtlName = "txtReferrralDate"
Else
blnGoback = False
sCtlName = ""
End if
now, in the GOT Focus events of the controls in your form add this line
CheckFocus

This will send your focus back to where it came from. - I can just hear
the red necks saying it to me...
(just kidding, of course!)

Be careful to toggle blnGoBack to False once you have the right values,
otherwise the poor focus
will never be able to leave town.

Regards
HS




MDW said:
I think the code you have would work...but try putting the
code in the "On Lost Focus" event rather than the "After
Update" event.

Private Sub Referral_Date_LostFocus()
If (Referral_Date.Text = "") Then
MsgBox "Your message here."
Referral_Date.SetFocus
End If
End Sub

-----Original Message-----
Hi,

I need some help. I have a form that I will get a user to
fill out with a couple of mandatory fields/text boxes I'd
like them to fill in. And when they don't fill in the
mandatory field... a message pops up saying that they need
to fill in the field. In addition and my problem, I would
like the cursor to stay on that field before moving onto
the next field/text box.

In the text box properties, I have an [Event Procedure]
for both "AfterUpdate" and "OnExit" which pops up a
message to the user entering data into the form to enter
the information. Which works fine... however, I would
like to have the cursor stay on that particular field/text
box before continuing to the next field. I've tried
placing the code listed below in the "AfterUpdate"
and "OnExit" thinking that this would work, but it moves
the cursor to the next text box once the message
window/prompt is clicked "ok". I even placed ":
TextBoxName.SetFocus" in the If... Then statement thinking
this would work... I've listed the code below. Also, I
have tried turning the "required field properties" to yes
and no. What am I not doing right? Would the "validation
rule" and "validation text" properties be used to do
this. I am sure there is more than one way to get this to
work. I can't seem to figure this out. Can you help me?

Thanks, Mark

Below is the code I have been working with in
the "AfterUpdate" section:


Private Sub Referral_Date_AfterUpdate()
If (IsNull(Me.Referral_Date) Or Me.Referral_Date = "")
Then
MsgBox "This is a mandatory field, please enter a
response here before proceeding. If not, you will be asked
again to enter this data before exiting this page.":
Referral_Date.SetFocus
End If
End Sub
.
 

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

Top