1

Very new to VBA. I guess MsgBox debugging is the VBA equivalent to print statement debugging in other languages?

If I use Format to turn a date into a string using dd/mm/yyyy format, and then print that string with a MsgBox, it instead prints in dd-mm-yyyy format:

Function MsgBoxFormat()
    Const dt As Date = "2022-04-01"
    MsgBox (Format(dt, "dd/mm/yyyy"))
End Function

Message box showing "01-04-2022"

It only seems to do this for the results of the Format function and only if it uses dd/mm/yyyy formatting. Other formats and literal strings are not affected:

Function MsgBoxFormat()
    Const dt As Date = "2022-04-01"
    MsgBox ( _
        Format(dt, "dd/mm/yyyy") & vbNewLine & _
        Format(dt, "dd-mm-yyyy") & vbNewLine & _
        Format(dt, "yyyy-mm-dd") & vbNewLine & _
        Format(dt, "dd mmm yyyy") & vbNewLine & _
        "01/04/2022" _
    )
End Function

enter image description here

I'm a bit worried I've fundamentally misunderstood something about VBA here. Or could this be something obscure relating to my PC's locale settings?

This is VBA 7.1 running on an Access database.

Jack Deeth
  • 3,062
  • 3
  • 24
  • 39

1 Answers1

2

That's because "/" is not a slash but the local date separator. Use force:

MsgBox (Format(dt, "dd\/mm\/yyyy"))
Gustav
  • 53,498
  • 7
  • 29
  • 55