How to set the focus on a text box in VBA

EMN

New member
Local time
Today, 02:11
Joined
Oct 29, 2015
Messages
4
Hi,

I'm having trouble setting the focus on a date text box in VBA.

I have performed error handling which will prevent the user enter an incorrect date format. I have tried several options from .setfocus etc but nothing seems to work, and trying to set the focus to another text box first then the txtdate textbox, could you please help me find a solution?

Many Thanks!

--------------------------------------------------------------------------
Private Sub txtdate_AfterUpdate()
If IsDate(txtdate) = False Then
MsgBox "Invalid Date format - dd-mm-yy", vbInformation, "Invalid Date Entered"
Cancel = True
txtdate.Text = ""
txtWO.SetFocus
txtdate.SetFocus

Exit Sub
End If
txtdate.Value = Format(txtdate.Value, "dd-mmm-yyyy")
End Sub
-------------------------------------------------------------------------
 
You are missing the Me. formatting from most/all of your code.

it should be Me.txtDate.Setfocus
 
FYI, moved your thread out of the introductions forum.

Also, validation should be done in the before update event. Cancel = True doesn't do anything in the after update event. In fact it should probably error, but you likely don't have Option Explicit at the top:

http://www.baldyweb.com/OptionExplicit.htm
 
HI,

Thanks for your replies but the Me.txtdate.Text = ""
and the Me.txtdate.SetFocus don't make any difference.

I have commented out the Cancel = True in the afterupdate section- this too has made no difference.

I don't have a beforeupdate - I did try creating a beforeupdate with the same code in it yesterday but this didn't help either.

At the moment, the text box is showing an error when a user enters an incorrect date format - which is correct
It then clears the date- which is correct
But it moves down then to the next text box but I want it to remain in the Txtdate text box until the user enters a correct date?

How can this be done.

Thanks in advance.
 
Private Sub txtdate_BeforeUpdate(Cancel As Integer)
If IsDate(txtdate) = False Then
MsgBox "Invalid Date format - dd-mm-yy", vbInformation, "Invalid Date Entered"
Cancel = True
txtdate.Text = ""
txtWO.SetFocus
txtdate.SetFocus

Exit Sub
End If
txtdate.Value = Format(txtdate.Value, "dd-mmm-yyyy")
End Sub
 
Is your control (The text box) call the same as your field name? This can cause problems. You can ensure you are referring the form control by using the Me. prefix

Code:
Private Sub txtdate_AfterUpdate()
If IsDate(Me.txtdate) = False Then
MsgBox "Invalid Date format - dd-mm-yy", vbInformation, "Invalid Date Entered"
Me.txtdate = ""
Me.txtWO.SetFocus
Me.txtdate.SetFocus

Exit Sub
End If
Me.txtWO.SetFocus
Me.txtdate = Format(Me.txtdate, "dd-mmm-yyyy")
End Sub
 
Hi,

Thanks for the reply, I have tried this and am getting an error
"Compile error- Procedure declaration does not match description of events or procedure having the same name"

I put the code before the UpdateAfter.

I called DIM txtdate as Integer also but it doesn't make a difference.

Sorry I'm new to VBA so any help could be greatly appreciated. Thanks
 
HI Minty,

I have tried this but its still not putting the focus on the txtdate textbox.

(1) I have commented out the txtWo as this is the next text box
(2) User enters an invalid date in the txtdate textbox - so they get the error described = correct
(3) The field is then cleared upon receiving the error message = correct
(4) Want the focus back in the txtdate textbox instead of moving onto the next textbox( which is txtWo) until a date has been entered.

Thanks!
 
Have you still got the before update code loaded ?
I would make sure you delete any other code associated with the control and start over.

Also just to prove you are not going mad create a command button that simply has the code

Me.txtdate.SetFocus

on its click event. If that doesn't work I would suspect you text box control isn't called what you think it is.... ?
 
If you keep this in the after_update event you will have even more headaches than this focus issue. One is that having two set foci in the after update will have the effect of canceling the action of any command buttons. It doesn't make sense trying to catch errors after the table has been updated. If you do that you have to undo the update.

The before update event is where this should go. When Cancel is set to true in the before update the focus remains on the text box. You don't need to set focus. Create a before update event for the text box, put in code below and it should work.

If IsDate(Me.txtdate) = False Then
MsgBox "Invalid Date format - dd-mm-yy", vbInformation, "Invalid Date Entered"
Cancel = True
Else
Cancel = False 'may not be needed but added for clarity
End If


Note that this will lock the user into that text box until they enter a correct date and there doesn't appear any way to provide any exceptions to this, even when they would be appropriate (Button to exit and discard changes). Still I believe this is the best you can do. Using other events won't guarantee the problem gets fixed.
 
First, setfocus can't be called just anywhere. But you should get something like a "this function or method is not available at this time" error if the call is attempted from a bad place. If the call is silent, what you have is NOT that the setfocus occurs but fails, but rather that a setfocus occurs but is silently overridden by a subsequent explicit or implicit setfocus.

Also, remember that X.setfocus will cause a Y.Lost_Focus event (where Y might or might not equal X) followed by an X.Got_Focus event. If there is something in either of those two event routines that affects what you are trying to do, just remember that you get two events for the price of one there. And I don't even want to THINK about what you get if your .Lost_Focus event is the one that tries to do a .SetFocus method.

Second, that error you reported (mismatch of declaration vs. actual call) might mean that you have overloaded an intrinsic event (or tried to "roll your own" event) in such a way that Access cannot actually call the event - because Access event procedure calls are pretty much hard-coded and thus immutable. That can screw up a LOT of things. It is possibly the only thing you could do that is worse than using a reserved word for a field name.
 
And I don't even want to THINK about what you get if your .Lost_Focus event is the one that tries to do a .SetFocus method.

I think about that kind of stuff all the time. Sometimes I dream about it. So I tried it. Like the after_date event it just ignores the setfocus to itself unless you set focus somewhere else first.
 
HI Minty,

I have tried this but its still not putting the focus on the txtdate textbox.

(1) I have commented out the txtWo as this is the next text box!

I put this in for a reason - you have to move off the control to be able to set focus back to it. See the other comments above.
 

Users who are viewing this thread

Back
Top Bottom