Restricted Entry in Userform Text Box

lisauk5583

Board Regular
Joined
Dec 1, 2004
Messages
56
Hi,

I have a text box within a userform that i wish to populate with a date. However, if anything other than a date in format (dd/mm/yy) is entered I want a message box to appear informing user that they must enter a date.

Any ideas would be gratefully received.

Thanks

Lisa
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Sootie

New Member
Joined
Nov 15, 2004
Messages
23
Hi lisauk5583
I dont know if this will help but when I needed to delete a particular name from a sheet I used a user form with a combo box
my code was you should be able to adjust it to your situation


Private Sub cmdOK_Click()
Dim Name As String, Rng As Range
If cboName.Text <> Empty Then 'A name was chosen
MsgBox ("You are about to REMOVE a Student!")
'Store name for Deletion
Name = cboName.Text
Else
'No name was chosen
Exit Sub
End If
Me.Hide
Sheets("Student Details").Select
On Error Resume Next
Set Rng = Range("Name").EntireColumn.Find(What:=Name)
If Not Rng Is Nothing Then
Rng.EntireRow.Delete
End If
End Sub
 

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,348
something like this might help/ Or alt. use a datepicker control


Code:
Private Sub CommandButton1_Click()
If Not IsDate(TextBox1.Text) Then
MsgBox ("Date please")
TextBox1.SetFocus
Exit Sub
End If

End Sub
 

lisauk5583

Board Regular
Joined
Dec 1, 2004
Messages
56
Thanks pcc - that only accepts dates and works fine. However if I enter a date, for example 37/05/92 it doesnt classify this as an error. This is the code I have on the text box.

Code:
On Error Resume Next
    tbReceived.Text = Format(tbReceived.Value, dd / mm / yy)
    If Not IsDate(tbReceived.Text) Then
    MsgBox ("Please enter a valid date")
    tbReceived.SetFocus
    Exit Sub
    End If
End Sub

Any Ideas?

Thanks

Lisa
 

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,348
not sure but could try this. no time right now

Code:
Private Sub CommandButton1_Click()
On Error Resume Next
    TextBox1.Text = Format(TextBox1.Value, "dd/mm/yy")
    
    If Not IsDate(TextBox1.Text) Then
    MsgBox ("Please enter a valid date")
    TextBox1.SetFocus
    Exit Sub
    End If
End Sub
 

lisauk5583

Board Regular
Joined
Dec 1, 2004
Messages
56
Thanks, that seems to work better. If you input a date that appears wrong it will reset the format to the mosot suitable so for 37/11/05 would become 05/11/37. Thanks for your help.
 

Forum statistics

Threads
1,147,507
Messages
5,741,566
Members
423,667
Latest member
Kai_357

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
Top