Force Date Format for a User Form

scotball

Active Member
Joined
Oct 23, 2008
Messages
375
Hi folks,

I have inherited an evaluation form and have a couple of improvements I want to do to it.

There are 2 date fields on the form, Session Date and DOB. There is no formatting control on this so I want to force the date entered to be formatted correctly and throw a message box up if someone tries to use a different format.

Found this code but I need to tweak it so that it's pulling date from the form rather than a cell location:

Code:
Dim dDate As DatePrivate Sub CommandButton1_Click()
    Sheet1.Range("G1").Value = dDate
End Sub
 
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Mid(TextBox1.Value, 4, 2) > 12 Then
        MsgBox "Invalid date format, please re-enter as dd/mm/yyyy", vbCritical
        TextBox1.Value = vbNullString
        TextBox1.SetFocus
        Exit Sub
    End If
     
    dDate = DateSerial(Year(Date), Month(Date), Day(Date))
    TextBox1.Value = Format(TextBox1.Value, "dd/mm/yyyy")
    dDate = TextBox1.Value
End Sub

In the VBA the boxes are called: DOBTextBox and SessionsBox

I want to ensure that the users enter the dates as dd/mm/yyyy in the Form but also when the macro copies the information from the form into a sheet that it retains the correct formatting.

Issue arises when someone enters 03/06/2008 as DOB but when it copies to the sheet the formatting switches to 06/03/2008.

Any guidance would be much appreciated.
Lee
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Could I just change it to:

Code:
Private Sub CommandButton1_Click()    DOBTextBox.Value = dDate
End Sub

and this would work when the user inputs a date into the box?
 
Upvote 0
Hi,
try

Code:
Dim dDate As Date
Private Sub CommandButton1_Click()
    With Sheet1.Range("G1")
        .Value = dDate
        .NumberFormat = "dd/mm/yyyy"
    End With
End Sub






Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.TextBox1
        If Len(.Text) > 0 Then
            If Not .Text Like "##/##/####" Then
                MsgBox "Invalid date format" & Chr(10) & "Please re-enter as dd/mm/yyyy", vbCritical, "Invalid Format"
                Cancel = True
            Else
                dDate = DateValue(.Text)
            End If
        End If
    End With
End Sub

Dave
 
Upvote 0
Thanks Dave,

Can you help/explain... the line: With Sheet1.Range("G1") ... this would read to me as from Sheet1!G1 this is where dDate would get it's value from?

If the value is to come from an input box in a Form called DOBTextBox, how would I change the code to get the correct information?
 
Upvote 0
Thanks Dave,

Can you help/explain... the line: With Sheet1.Range("G1") ... this would read to me as from Sheet1!G1 this is where dDate would get it's value from?

If the value is to come from an input box in a Form called DOBTextBox, how would I change the code to get the correct information?


From VBA Helpfile

The With statement allows you to perform a series of statements on a specified object without requalifying the name of the object.

You can read more in the helpfile

Just change TextBox1 to the actual name of your textbox control

Dave
 
Upvote 0
Code:
If GroupListBox.Value = "" ThenMsgBox "Please add in Location", vbExclamation
  Exit Sub
End If

The List box has items in there but I want the message to pop up if the user doesnt select one of the items... so far the code above doesnt work... the other code you gave me works a treat!! Thank you.
 
Upvote 0
Code:
If GroupListBox.Value = "" ThenMsgBox "Please add in Location", vbExclamation
  Exit Sub
End If

The List box has items in there but I want the message to pop up if the user doesnt select one of the items... so far the code above doesnt work... the other code you gave me works a treat!! Thank you.

That's another question - post in new thread with copy of your code.

Glad solution helped.

Dave
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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