SetFocus Code

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
This code will not SetFocus to TextBox2…any ideas as to why?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Code:
[FONT=Verdana]Private Sub TextBox2_AfterUpdate()[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]If Me.TextBox2.Value < Me.TextBox3.Value Then<o:p></o:p>[/FONT]
[FONT=Verdana]Me.TextBox2.SetFocus[/FONT]
[FONT=Verdana]MsgBox "Counter Value is Less Than Previous Reading.", vbCritical[/FONT]
[FONT=Verdana]Exit Sub<o:p></o:p>[/FONT]
[FONT=Verdana]End If<o:p></o:p>[/FONT]
[FONT=Verdana]<o:p></o:p>[/FONT]
[FONT=Verdana]End Sub[/FONT]
<o:p></o:p>
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Could it be because you are comparing text and not numbers?

Code:
If Val(Me.TextBox2.Value)<Val(Me.TextBox3.Value) Then
 
Upvote 0
I also have problems with .SetFocus in the Enter, Exit, AfterUpdate and BeforeUpdate events.

Have you tried moving the checking to the BeforeUpdate event and using the Cancel variable rather than .SetFocus?
 
Upvote 0
Part of the code was cut off, the main thing is to use Val to convert the text in both textboxes to numbers.

Then you can compare them properly, assuming they are meant to be numbers.

By the way which textbox is entered first, is there always a value in TextBox3 when you leave TextBox2?
 
Upvote 0
Thanks Mike...
I haven't tried that but thanks for the suggestion. I'll give it a go and let you know. Gotta mow the lawn first:(
 
Upvote 0
Norie,

I set both TextBoxes to Val(Me.TextBox#.Value)

TextBox3 value is called from a datasheet...with no user input.

All TextBox values are numeric.
 
Upvote 0
Well it should work, and it did for me.

Mike has got a good idea though.
 
Upvote 0
OK…This is how I “fixed” it.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
I moved the MsgBox and SetFocus code to CommandButton1_Click.
This button runs my process after all textboxes have been populated.
<o:p> </o:p>
If TextBox2.Value < TextBox3.Value Then
MsgBox pops and focus is set correctly.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top