Text box setfocus

alk1000

New Member
Joined
Sep 14, 2006
Messages
4
Hi All,

What I’m trying to do is straight forward.
I’ve a form with multiple textboxes, I validate textbox4 value to be between 2 numbers based on a combolist selection; if it's not then a Msgbox will show up detailing what should be done. I’m using the below afterupdate lines:

Private Sub TextBox4_AfterUpdate()
If DEForm.TextBox4 <> "" Then
DNinterval = Split(DEForm.ComboBox6.Value, " - ")
DNLower = CLng(DNinterval(0))
DNUpper = CLng(DNinterval(1))
If CLng(DEForm.TextBox4) < DNLower Or CLng(DEForm.TextBox4) > DNUpper Then
MsgBox "Please enter an extension between " & DNLower & " and " & DNUpper, vbCritical + vbOKOnly, "Illegal Data Entry"
DEForm.TextBox4.SetFocus
Exit Sub
End If
End If
End Sub

But when I type a number which doesn’t meet my test criterion, I got the error message box but the focus won’t come back to textbox4!

Does anyone have a clue on why this is happening and how can I leave the cursor into textbox4 till the user types a valid entry.

Cheers,
Daniel
Ps: combobox6 will have a value like: 88500 - 88999
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Thanks Tom,

I tried it, it still doesn't move the cursor back to textbox4! so if I put a value which doesn't match my validation test, and I try to click inside another textbox, the MsgBox pops up but the cursor doesn't appear in any of the textboxes! but pressing TAB works perfectly, the MsgBox pops up and the cursor stays in textbox4. Any clue!

Below is how it looks now:

Private Sub TextBox4_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If DEForm.TextBox4 <> "" Then
DNinterval = Split(DEForm.ComboBox6.Value, " - ")
DNLower = CLng(DNinterval(0))
DNUpper = CLng(DNinterval(1))
If CLng(DEForm.TextBox4) < DNLower Or CLng(DEForm.TextBox4) > DNUpper Then
MsgBox "Please enter an extension between " & DNLower & " and " & DNUpper, vbCritical + vbOKOnly, "Illegal Data Entry"
Cancel = True
DEForm.TextBox4.SetFocus
Exit Sub
End If
End If
End Sub

Tom, another point, what if the user put a wrong value but then decides to click on the "Quit" button on the form, how can I avoid this test from happening? The Quit button, just unload the form.
Cheers,
Daniel
 
Upvote 0
The setfocus method only applies to frames. Why? I don't know. Probably because textboxes in msforms are windowless. Frames are windowed controls. This code worked for me.

Code:
Dim ForeGoTest As Boolean

Private Sub Quit_Click()
    ForeGoTest = True
    Unload Me
End Sub

Private Sub TextBox4_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If DEForm.TextBox4 <> "" Then
        DNinterval = Split(DEForm.ComboBox6.Value, " - ")
        DNLower = CLng(DNinterval(0))
        DNUpper = CLng(DNinterval(1))
        If (CLng(DEForm.TextBox4) < DNLower Or CLng(DEForm.TextBox4) > DNUpper) And Not ForeGoTest Then
            Cancel = True
            MsgBox "Please enter an extension between " & DNLower & " and " & DNUpper, vbCritical + vbOKOnly, "Illegal Data Entry"
        Exit Sub
        End If
    End If
End Sub

Download the example that contains a mock setup of your form.

<a href="http://home.fuse.net/tstom/0623081117.326163.zip"><img src="http://home.fuse.net/tstom/zip.gif"width="48"height="48"border="0"></a> <a href="http://home.fuse.net/tstom/0623081117.326163.zip">0623081117.326163.zip</a>

As for your Quit button. Make sure that its "TakeFocusOnClick" property is set to false. Also, if this is your default cancel button, set the Cancel property to true. This will allow an exit using the cancel key which is the default behavior in many window's forms applications.
 
Upvote 0
Many many thanks Tom,


I found out why the cursor is not coming back to textBox4 but not sure how to fix it.
The reason is the use of Frames. I would've attached my form to show it to you but I don't have the right to attach documents :(.
In a nutshell I've 2 Frames, Frame1 encompasses the whole form where all toolboxes resides and then Frame2 -is inside Frame1- where TextBox4 and ComboList6 resides!

have you ever came across such scenario!
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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