VBA question for textbox msgbox errors

godevs

New Member
Joined
Oct 9, 2007
Messages
5
I am a beginner, and don't use VBA too much. I have a form with a textbox that I want a user to put in a valid date. So for the textbox_afterupdate, I have an IsDate for the value that the user enters. If not a valid date, I have a msgbox popup. However, when the user clicks okay it goes to the next tabstop, how do I get the cursor to go back to the original textbox where the error occured?

Thanks.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Don't use the AfterUpdate event.

If you use the Exit event you can easily prevent the user moving to the next control.
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Not IsDate(TextBox1.Text) Then
        MsgBox "Please enter a valid date."
        Cancel = True
    Else
        ' format date as required
        TextBox1.Text = Format(TextBox1.Text, "dd/mm/yyyy")
    End If
End Sub
 
Upvote 0
godevs,

Welcome to MrExcel.com!

If you right-click on your controls toolbox toolbar you'll see an option for additional controls. Try adding the Calendar Control xx.0 (good) or scroll down to Microsoft Date and Time Picker Control x.0 (better) and see if those don't work better for you.
 
Upvote 0
To anyone else reading along, if your right-click on the top of the tab you get the options to add, delete, rename... pages. The additional controls... option happens when you right-click inside the body of the page.

Andrew, obviously I'm not about to try and replicate this error. What I would try to do is a RESET on the commandbar that you nuked. Unfortunately, I can't quite suss out which one it is by looking through the output from this:
Code:
Sub booboo()

For i = 1 To Application.VBE.CommandBars.Count

    Debug.Print i; ":"; Application.VBE.CommandBars(i).Name
    
    With Application.VBE.CommandBars(i).Controls
    
        For j = 1 To .Count
        
            Debug.Print "   "; j; ".."; .Item(j).Caption
            
        Next j
    
    End With

Next i
End Sub

Norie -- do you know the name of that commandbar???
 
Upvote 0
Greg

I reckon it's either of these.
Code:
 24 :Toolbox
    1 ..C&omponents...
    2 ..&Add Tab...
    3 ..Doc&kable
    4 ..&Hide
 25 :Toolbox Group
    1 ..&Add Tab...
    2 ..&Delete Tab
    3 ..&Rename Tab...
    4 ..Move &Up
    5 ..Move Do&wn
Names and caption seem to kind of make sense.:)
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,922
Members
449,094
Latest member
teemeren

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