HOWTO: Change the Short Date Format from Visual Basic

Last reviewed: September 30, 1997
Article ID: Q168793
The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0
  • Microsoft Access versions 7.0, 97
  • Microsoft Excel 97 for Windows
  • Microsoft Word 97 for Windows

SUMMARY

This article presents a step-by-step example on how to programmatically change the short date format of the system's Regional Settings. Otherwise the short date format can be manually changed through the Regional Settings applet in Control Panel.

MORE INFORMATION

Step-by-Step Example

  1. In a new or existing project, add a form (Form1).

  2. Add a CommandButton (Command1) to the form.

  3. Add the following code to the General Declarations section of Form1:

          Option Explicit
    

          Private Const LOCALE_SSHORTDATE = &H1F
          Private Const WM_SETTINGCHANGE = &H1A
          'same as the old WM_WININICHANGE
          Private Const HWND_BROADCAST = &HFFFF&
    

          Private Declare Function SetLocaleInfo Lib "kernel32" Alias _
    
              "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As _
              Long, ByVal lpLCData As String) As Boolean
          Private Declare Function PostMessage Lib "user32" Alias _
              "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
              ByVal wParam As Long, ByVal lParam As Long) As Long
          Private Declare Function GetSystemDefaultLCID Lib "kernel32" _
              () As Long
    
    

  4. Place the following code in Command1_Click event procedure:

          Private Sub Command1_Click()
    
             Dim dwLCID As Long
             dwLCID = GetSystemDefaultLCID()
             If SetLocaleInfo(dwLCID, LOCALE_SSHORTDATE, "dd-MMM-yy") _
                = False Then
                MsgBox "Failed"
                Exit Sub
             End If
             PostMessage HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0
          End Sub
    
    

  5. Run the program and open the form. Click on Command1 and then exit the program.

  6. Go to Control Panel and double-click on the Regional Settings icon. Select the Date tab. Note that the Short date style has been changed to "dd-MMM-yy."
Keywords          : vb432 VB4WIN vb5all vb5howto VBKBDLL VBKBWinAPI vbwin GnrlVb kbprg
Technology        : kbvba
Version           : WINDOWS:4.0 5.0 7.0 97
Platform          : WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.