1

I have a form in Access where I have a textbox which generates an email in Outlook. This email has to populate the date for a specific event. My PC language is French (Canada), so the month displays in French in the email while the rest is in English.

No matter which language the PC is in, I want the date to be in English.
i.e. 'December 19, 2022' or '19 December 2022', instead of '19 décembre 2022'.

1
  • According to Format a date and time field: [...] formats for dates and times vary depending on the geographic location specified in the Microsoft Windows Regional and Language Options setting on your computer. Commented Mar 15, 2023 at 14:15

1 Answer 1

2

You can use my function MonthNameInvariant:

' Constants:
    Public Const MaxMonthValue          As Integer = 12
    Public Const MinMonthValue          As Integer = 1


' Returns the English month name for the passed month number.
' Accepted numbers are 1 to 12. Other values will raise an error.
' If Abbreviate is True, the returned name is abbreviated.
'
' 2015-11-25. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function MonthNameInvariant( _
    ByVal Month As Long, _
    Optional ByVal Abbreviate As Boolean) _
    As String
    
    Const AbbreviatedLength As Integer = 3
    
    Dim MonthName( _
        MinMonthValue To _
        MaxMonthValue)      As String
    Dim Name                As String
    
    If Not IsMonth(Month) Then
        Err.Raise DtError.dtInvalidProcedureCallOrArgument
        Exit Function
    End If
    
    ' Non-localized (invariant) month names.
    MonthName(1) = "January"
    MonthName(2) = "February"
    MonthName(3) = "March"
    MonthName(4) = "April"
    MonthName(5) = "May"
    MonthName(6) = "June"
    MonthName(7) = "July"
    MonthName(8) = "August"
    MonthName(9) = "September"
    MonthName(10) = "October"
    MonthName(11) = "November"
    MonthName(12) = "December"
    
    If Abbreviate = True Then
        Name = Left(MonthName(Month), AbbreviatedLength)
    Else
        Name = MonthName(Month)
    End If
    
    MonthNameInvariant = Name

End Function

Full code and documentation in my library at GitHub: VBA.Date.

Addendum:

To create a text formatted as you have specified, you could use something like this:

Dim SendDate As Date
Dim TextDateInMail As String

' The date value in the mail to send.
SendDate = Date

' The formatted text date in the mail to send.
TextDateInMail = MonthNameInvariant(Month(SendDate)) & Format(SendDate, " dd, yyyy")

' Value of TextDateInMail:
' April 01, 2023
Sign up to request clarification or add additional context in comments.

7 Comments

Can I mark an answer as duplicate stackoverflow.com/a/49798375/4677305 :p-
I came across your code in another thread as well. I tried it and it didn't do anything. Also, I tried the above code given it is a bit different from the other thread. But, I get an error as soon as I type 'Public Const MaxMonthValue As Integer = 12'. Error says: Compile Error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules. Can you tell what is wrong and what to do in this case?
Include in the two constants (listed above) in the module header.
@DarrenBartrup-Cook: Oh, you searching is better than my memory.
Thank you so much Gustav! You have been a great help. I had to modify it a bit to make it work for my purpose. But, eventually it worked perfectly. :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.