Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

focus problem

Status
Not open for further replies.

cvaccess

Technical User
Jun 26, 2002
55
US
Hi,

I am stumped on how to set focus on a specific field (txtSent) when there is an error. It does show the message box but does not set focus to the txtSent field. Please help, thanks.

Private Sub txtSent_LostFocus()
Dim Date1 As Date
Dim Date2 As Date

Date1 = txtSent.Value
Date2 = txtReceived.Value

If Date1 < Date2 Then
txtSent.SetFocus
MsgBox &quot; Invalid date &quot;



End If
End Sub
 
Try putting the SetFocus after the MsgBox. VBSlammer
redinvader3walking.gif
 
Nope. That didn't work. Thanks, any other ideas?

Private Sub txtSent_LostFocus()
Dim Date1 As Date
Dim Date2 As Date

Date1 = txtSent.Value
Date2 = txtReceived.Value

If Date1 < Date2 Then
MsgBox &quot; Invalid date &quot;
txtSent.SetFocus


End If
End Sub
 
If txtSent is the last control on your form, when it loses focus then the current record will cycle to the next record and focus will be applied to the control with an index of 0.

As a workaround, you can use an invisible command button as the last control on your form, and place your code in it's GotFocus() event. To make it transparent, don't use the visible property. Set its Transparent property to Yes.

VBSlammer
redinvader3walking.gif
 
What is wrong with this? Here is what I did based on your reply. I created a transparent command button (cmdFocus). Nothing happens when I get focus on the field. Help? Thanks.

Private Sub cmdFocus_GotFocus()
Dim Date1 As Date
Dim Date2 As Date

Date1 = txtSent.Value
Date2 = txtReceived.Value

If Date1 < Date2 Then
MsgBox &quot; Invalid date &quot;
txtSent.SetFocus
End If
End Sub
 
Do you mean the GotFocus() event doesn't fire at all? Have you set a break point in your code so you can step through it with the debugger?

VBSlammer
redinvader3walking.gif
 
Yeah. I tried to debug with the breakpoint but it never fired off. I think I know why. Wouldn't the transparent command button need to get focus for it to fire off? When I clicked on the empty area (at runtime) it did fire off and set focus but how do I do this when the transparent command button is not within the fields getting the focus?

Thanks.
 
In reality, the best way to do what you're doing is in the Form's BeforeUpdate() event. That way no matter how the user decides to exit the record your validation procedure will fire.
VBSlammer
redinvader3walking.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top