+ Reply to Thread
Results 1 to 6 of 6

Date format not consistent when storing or editing date cells using userform

  1. #1
    Registered User
    Join Date
    05-31-2023
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    11

    Date format not consistent when storing or editing date cells using userform

    Hi all,

    I am building a userform that is capable of storing data in a sheet (called 'Database') in the same file. there is also functionality to search values per columns on the userform using a listbox to be able to select a row and then edit and submit the data again. This is where some problems arise regarding the date formatting.

    I have 4 textboxes where the date is selected using a calendar. I want it to be in the format DD-MM-YYY. When i use my calendar to select a date it tastes the right date in the right format in the textbox, however if i the save my data it changes to MM-DD-YYYY if i look in the database. It does this only if it's possible: so let's say i put it in 7-9-2023, it changes to 9-7-2023, but 25-9-23 stays 25-9-2023. This is the first part of the problem. I have checked with changing the language / regional settings of the operating system but even if theyre set to DD-MM-YYYY it still changes it. Also changing the Excel language does not change anything. Is there anyway to make sure it always stores the right date/date format when saving the data?

    Here is the part of the code that saves the data to the sheet after filling them on the userform. textboxes with the dates are txtWlevink, txtGlevink, txtWlevver, txtGLevver:


    Please Login or Register  to view this content.

    The second question is regarding the edit part of the userform. When trying to edit a row, it copies the data back to the userform when clicking edit after selecting a row in the listbox on the userform. When the dates are 'possible' in the US notation (such as 9-2-2023) are copied back as a 5 character number instead of the date. Is there any way to fix this?

    this part of the code is responsible for editing:

    Please Login or Register  to view this content.
    basically the question is: How can i fix this so that when the computer and Excel are set to the Dutch format (DD-MM-YYYY) that the Database sheet and the userform only use that format? Different people need to use this file so even better would be if it always works when someone uses it even with different settings.


    by the way:
    i tried using
    Please Login or Register  to view this content.
    But that does not work
    Attached Files Attached Files
    Last edited by Excelhelp_0912; 09-07-2023 at 11:29 AM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Date format not consistent when storing or editing date cells using userform

    Hi there,

    The important thing to remember about TextBox values is that (regardless of what they "look like") they are always STRING values, so it is often necessary (e.g. for a Date) to convert that string value into an appropriate numeric value.

    I think that your "cmdEdit_Click" needs to be changed as per the highlighted statements - this will display on the UserForm (in the specified format) the dates retrieved from the worksheet:

    Please Login or Register  to view this content.

    As far as copying date values to the worksheet is concerned, I think you need to use something like the "mdteDateValue" function (shown below) to convert the date values in the TextBoxes to the appropriate numeric values which should be entered in the worksheet. Three highlighted lines in your "Submit" routine also need to be changed:

    Please Login or Register  to view this content.

    Hope this helps.

    Please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,224

    Re: Date format not consistent when storing or editing date cells using userform

    If you are rewriting data from the worksheet to the text fields of the form, use the Format function without the formatting code, e.g.
    Please Login or Register  to view this content.
    In the case of the date, the "date" will be inserted into the text field in the format such as the SYSTEM setting of the short date. If you need to have a different date format in the form, then you need to add a formatting code in the Format function.

    If you are transcribing a date from a form text field to a worksheet cell then force the appropriate data type (in this case the Date type).
    Please Login or Register  to view this content.
    Note that a text field stores text and directly rewriting the value from the field to a worksheet cell inserts the text. In some cases, this text, at the VBA-Excel boundary, may be implicitly converted to some data type. So don't rely on implicit conversions, and always take care of the correct data type yourself.

    The conversion scheme shown above for both environments (TextBox and Range) works well as long as the date format is not forced. If you force a specific format, then the solution that Greg M showed should be used.

    Artik

  4. #4
    Registered User
    Join Date
    05-31-2023
    Location
    Amsterdam
    MS-Off Ver
    2016
    Posts
    11

    Re: Date format not consistent when storing or editing date cells using userform

    Hi, thanks for the quick reply. However if i use it now and select dates in the beginning of september 2023. it the stores completely different dates in 2022. i think this is due to the not being a 0 in front. So 5-9-2023 got stored as 25-09-2022. do you know how i can make sure that that does not happen? The edit function works perfectly by the way so a big thanks for that!

    i forgot to add that probably the easiest fox, is that if you click on a date on the calendar it automatically puts a 0 in front of the (single digits) days and months. This is the code that is used by the calendar:

    Please Login or Register  to view this content.
    i edit the the day click sub to this:

    Please Login or Register  to view this content.
    And now everything works perfectly! It seems i forgot one small thing though.

    this line of code in the submit Sub:
    Please Login or Register  to view this content.
    is used to check the latest time some user edited/submitted a row. however now it is, based on the system. I tried too use the mdteDateValue but can't get it to work. Do you have any idea?
    Last edited by Excelhelp_0912; 09-07-2023 at 07:59 AM. Reason: forgot to add my code

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Date format not consistent when storing or editing date cells using userform

    Hi again,

    Maybe:

    Please Login or Register  to view this content.
    Hope this helps.

    Regards,

    Greg M

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Date format not consistent when storing or editing date cells using userform

    Hi again,

    The following function may be better suited to your needs as it avoids an error condition being generated if any of the "Date" TextBoxes contains a blank value:

    Please Login or Register  to view this content.
    The associated lines in the "Submit" routine should now read:

    Please Login or Register  to view this content.

    Hope this helps.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] checking if a date/date range already exists in 4 other date storing tables
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-13-2014, 06:01 PM
  2. Userform textbox & sheet cells date format do not match
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2014, 12:56 PM
  3. [SOLVED] Excel 2007, Userform Textbox Date Format and Calendar Control Userform
    By riffology in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-03-2014, 06:18 PM
  4. Replies: 4
    Last Post: 05-31-2013, 11:20 AM
  5. [SOLVED] Storing a date in a variable with a different format
    By JTroidl in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2013, 08:17 PM
  6. Replies: 1
    Last Post: 09-30-2012, 03:01 PM
  7. Userform date format reverting back to us format on change event
    By jpruffle in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-10-2009, 12:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1