How To Change the Short Date Format from Visual Basic (168793)



The information in this article applies to:

  • Microsoft Visual Basic Control Creation Edition for Windows 5.0
  • Microsoft Visual Basic Learning Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
  • Microsoft Access for Windows 95 7.0
  • Microsoft Access 97
  • Microsoft Excel 97 for Windows
  • Microsoft Word 97 for Windows

This article was previously published under Q168793

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:
          P  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."

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kb32bitOnly kbhowto kbProgramming KB168793