setfocus method in the lostfocus event

G

Guest

Hi all,

I need to make sure that the user types a valid number in the first textbox
of the form used to insert new records.
In the lostfocus event of that textbox, I've written the following:

Private Sub medNum_LostFocus(Cancel As Integer)
If Me!medNum = "" Or IsNull(Me!medNum) Then
MsgBox "The number field must contain a valid number.",
vbExclamation, "Type a valid number"
Me!medNum.SetFocus
End If
End Sub

The problem is that the focus still goes at the following textbox, the set
focus line does not work.

I've tried to put the same code in the Exit event, but does not solve.

Any idea?

Many thanks
Massimo
 
A

Allen Browne

Cancel the event of you want to user to enter something, i.e.:
Cancel = True

Of course, that still won't work if the user never visits the control (e.g.
if they click after it), so it might be better to open your table in design
view, select the medNum field, and set its Required property to Yes in the
lower pane of table design.
 
G

Guest

Hi Allen,
setting Cancel = True does solve the issue, 'cause the cursor stays in the
same textbox forcing the user to write something, but then there is another
problem: the user cannot click on the "undo new record" button, unless he/she
has written a valid record number in the field.
Regarding the required property, it is already set to yes, but it is not
helpful to force the user to write the number, cause the medNum is the first
control that has the focus in the new record form, so at that point the
record has not been created yet and the user can move to another field
leaving it blank...
Everything is getting a little bit confusing... what am I doing wrong?

thanks
Massimo
 
A

Allen Browne

Train the user to press <Esc> if they need to undo the entry in the box.
This is much more efficient than trying to get to a command button on the
form. If they can't remember that, they can use Undo on the Edit menu to get
out, or even a toolbar button.

If the Required property of the field in the table is set to Yes, they won't
be able to save the record without an entry in this text box. What we do is
use the BeforeUpdate event of the Form to test if the control IsNull() where
we want to provide a custom message for the user.
 
G

Guest

the Esc key seems to be really a good solution, maybe it is good to put a
reminder for the user in the tooltip of the textbox.

thanks a lot
Massimo
 

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