On exit event

E

Edward

Hi everybody,
I have written a simple code for onExit event of a textbox on a form

Private sub First_Name(cancel as Integer)
if First_Name.Text="" then
First_Name.Setfocus
End if
End Sub

I don't understand why Setfocus dosen't work? Any thoughts?
 
D

Dymondjack

Try explicitly calling out the control:

Private Sub First_Name_Exit(Cancel As Integer)
If Me.First_Name.Text = "" Then
Me.First_Name.SetFocus
End If
End Sub

If you are trying to verify that a name has been entered, I myself use the
form's BeforeUpdate event and check for any missing user input. You can
cancel the event if there's no info, with a msgbox asking the user to supply
all required info...
 
J

John Spencer (MVP)

You can't set the focus to a control you are attempting to exit. If you want
to cancel the exit then just set Cancel = true. Also, instead of testing for
a zero-length string, you might test for a zero-length string and null.

Private sub First_Name_Exit (Cancel as Integer)

IF Len(Trim(First_Name.Text & "")) = 0 then
Msgbox "First Name is required" '<<<< nice to tell user what is needed
Cancel = True '<<<< Cancel the exit event
End if

End Sub

By the way, next time copy the code and then paste it, instead of typing it
into the message. That way any syntax errors will show up.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
E

Edward

Thanks, I think we were able to sent focus back to the control in VB ,but
your method worked. One more question In VBA 2003 and office userforms
textbox-exit even has cancel argument as boolean and that's what you used in
your suggestion(Caancel=True) do you know why MS has changed it to integer?
it seems Cancle can have only two states (True,False) so boolean makes more
sense than integer.
 
C

Clifford Bass

Hi Edward,

Really basic question: Why don't you, in the table containing the
First_Name field, just set the First_Name field's "Required" property to
"Yes" and its "Allow Zero Length" to "No"? It would eliminate the need to do
that coding.

Clifford Bass
 
J

John Spencer (MVP)

I have no idea why Microsoft does what they do. In this case it could be
--Arbitrary decision by the programmer
--Planning for future capability
--Decision by manager
--Performance - perhaps integer is faster than boolean
--or make up your own reason.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
D

Douglas J. Steele

Did Access Basic have a Boolean data type in its first incarnation? (I know
it didn't have a Date data type...)
 

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