Date format from txtbox changing when submitted to data table

inkbird1

Board Regular
Joined
Apr 21, 2020
Messages
51
Hi all,
I have a userform with the date textbox as dd/mm/yyyy. I have formated the textbox value as

Private Sub UserForm_Initialize()
txtDate.Value = Format(Now(), "dd/mm/yy")
txtTime.Text = Format(Now(), "hh:mm")

When I click the add entry on my userform and go to the repository data table. The format has changed to mm/dd/yyyy despite changing the cell format back to dd/mm/yyyy

Any ideas?
 

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)
Hi,
what comes out of your textbox is text so need to coerce the content to required data type - As you are using numbers with valid date separator, DateValue function should recognize the order for day, month and year according to the short date format specified in your system.

Rich (BB code):
Cells(r, 1).Value = DateValue(Me.txtDate.Value)

Be aware, that if content in your textbox is not a valid date (or empty), function will error & you should manage this.

Dave
 
Upvote 0
Hi,
what comes out of your textbox is text so need to coerce the content to required data type - As you are using numbers with valid date separator, DateValue function should recognize the order for day, month and year according to the short date format specified in your system.

Rich (BB code):
Cells(r, 1).Value = DateValue(Me.txtDate.Value)

Be aware, that if content in your textbox is not a valid date (or empty), function will error & you should manage this.

Dave
THanks Dave, where do i put the code you have provided??
 
Upvote 0
THanks Dave, where do i put the code you have provided??

I gave an example how to use DateValue function in code but if need assistance applying it to your add entry code, then please share the code here with forum & I or others will guide you.

Dave
 
Upvote 0
Still not working - here is the code

VBA Code:
Private Sub UserForm_Initialize()
txtTime.Text = Format(Now(), "hh:mm")
txtDate.Text = Format(Now(), "Long Date")

End Sub

Private Sub cmdADD_Click()

''''''''''''''''''Check fields for no entries'''''''''''''''''''''''''''''''''''''''''''''''''''

If txtTime.Value = "" Then
        MsgBox "Please enter a value, then try again.", vbExclamation
        Exit Sub
    End If
 
If txtDate.Value = "" Then
        MsgBox "Please enter a value, then try again.", vbExclamation
        Exit Sub
    End If

If cbo_EnteredBy.Value = "" Then
        MsgBox "Please enter a value, then try again.", vbExclamation
        Exit Sub
    End If



''''''''''''''''End Checking for blanks ''''''''''''''''''''''''''''''''''''''''''''''''''''

'dimention the variable

    Dim ws As Worksheet

    Dim addme As Range

    'set the variable

    Set ws = Sheet5

    'set variable for the destination

    Set addme = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

    'hold in memory

    Application.ScreenUpdating = False

    'send the values to the database

    With ws

        'set the date format to suit your area

        addme.Value = Format(txtDate.Value, "dd/mm/yyyy")
        addme.Offset(0, 1).Value = Me.txtTime.Value
        addme.Offset(0, 2).Value = Me.cbo_EnteredBy
        addme.Offset(0, 3).Value = Me.cboType.Value
       addme.Offset(0, 4).Value = Me.cboArea.Value
       addme.Offset(0, 5).Value = Me.txtDescription.Value
     End With

    'reset the form

    Unload Me

    'update the sheet

    Application.ScreenUpdating = True
  

End Sub
Private Sub cmdClose_Click()
frmIncidents.Hide
End Sub
Private Sub cmdReset_Click()
Unload Me
   frmIncidents.Show
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,716
Members
449,093
Latest member
Mnur

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