VBA Tipp: Ländereinstellungen ermitteln/setzen

Aus DBWiki
Wechseln zu: Navigation, Suche

Problem

Ich möchte herausfinden, wie die Ländereinstellungen für die Datumsformate auf meinem Rechner aussehen bzw. diese ändern.

Lösung

Dies leisten die folgenden Funktionen:

Public Const LOCALE_SLONGDATE = &H20
Public Const LOCALE_SSHORTDATE = &H1F
Public Const WM_SETTINGCHANGE = &H1A
Public Const HWND_BROADCAST = &HFFFF&
 
Private Declare Function GetLocaleInfo Lib "kernel32" Alias _
    "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As _
    Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
 
Private Declare Function SetLocaleInfo Lib "kernel32" Alias _
    "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As _
    Long, ByVal lpLCData As String) As Long
 
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 SendMessage Lib "user32" Alias _
    "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long
 
Private Declare Function GetSystemDefaultLCID Lib "kernel32" _
    () As Long
 
Public Function SetShortDate(Optional sFmt As String = "dd.MM.yyyy")
  Dim dwLCID As Long, Res As Long
  dwLCID = GetSystemDefaultLCID()
  If SetLocaleInfo(dwLCID, LOCALE_SSHORTDATE, sFmt) = 0 Then
    MsgBox "Fehler in SetShortDate: SetLocaleInfo"
  Else
    Res = SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, 0&)
  End If
End Function
 
Public Function SetLongDate(Optional sFmt As String = "dddd, d. MMMM yyyy")
  Dim dwLCID As Long, Res As Long
  dwLCID = GetSystemDefaultLCID()
  If SetLocaleInfo(dwLCID, LOCALE_SLONGDATE, sFmt) = 0 Then
    MsgBox "Fehler in SetLongDate: SetLocaleInfo"
  Else
    Res = SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, 0&)
  End If
End Function
 
Public Function GetShortDate()
  Dim dwLCID As Long, Res As Long, Tmp As String, L As Long
  dwLCID = GetSystemDefaultLCID()
  Tmp = String(255, 0)
  L = Len(Tmp)
  Res = GetLocaleInfo(dwLCID, LOCALE_SSHORTDATE, Tmp, L)
  GetShortDate = Mid(Tmp, 1, Res - 1)
End Function
 
Public Function GetLongDate()
  Dim dwLCID As Long, Res As Long, Tmp As String, L As Long
  dwLCID = GetSystemDefaultLCID()
  Tmp = String(255, 0)
  L = Len(Tmp)
  Res = GetLocaleInfo(dwLCID, LOCALE_SLONGDATE, Tmp, L)
  GetShortDate = Mid(Tmp, 1, Res - 1)
End Function

So kann man z.B. herausfinden, welches Datumsformat für "Datum, lang" eingestellt ist:

Debug.Print GetLongDate()
dddd, d. MMMM yyyy
Wiki hinweis.png Anmerkung: Die Einstellung für das Access-Format "Datum, mittel" erfolgt nicht in den Ländereinstellungen, sondern wird von Access aus den Ländereinstellungen abgeleitet


Andere Einstellungen

Für andere Ländereinstellungen siehe die folgenden Konstanten:

Public Const LOCALE_ICENTURY = &H24
Public Const LOCALE_ICOUNTRY = &H5
Public Const LOCALE_ICURRDIGITS = &H19
Public Const LOCALE_ICURRENCY = &H1B
Public Const LOCALE_IDATE = &H21
Public Const LOCALE_IDAYLZERO = &H26
Public Const LOCALE_IDEFAULTCODEPAGE = &HB
Public Const LOCALE_IDEFAULTCOUNTRY = &HA
Public Const LOCALE_IDEFAULTLANGUAGE = &H9
Public Const LOCALE_IDIGITS = &H11
Public Const LOCALE_IINTLCURRDIGITS = &H1A
Public Const LOCALE_ILANGUAGE = &H1
Public Const LOCALE_ILDATE = &H22
Public Const LOCALE_ILZERO = &H12
Public Const LOCALE_IMEASURE = &HD
Public Const LOCALE_IMONLZERO = &H27
Public Const LOCALE_INEGCURR = &H1C
Public Const LOCALE_INEGSEPBYSPACE = &H57
Public Const LOCALE_INEGSIGNPOSN = &H53
Public Const LOCALE_INEGSYMPRECEDES = &H56
Public Const LOCALE_IPOSSEPBYSPACE = &H55
Public Const LOCALE_IPOSSIGNPOSN = &H52
Public Const LOCALE_IPOSSYMPRECEDES = &H54
Public Const LOCALE_ITIME = &H23
Public Const LOCALE_ITLZERO = &H25
Public Const LOCALE_NOUSEROVERRIDE = &H80000000
 
Public Const LOCALE_SCURRENCY = &H14
Public Const LOCALE_SDECIMAL = &HE
Public Const LOCALE_SGROUPING = &H10
Public Const LOCALE_STHOUSAND = &HF
 
Public Const LOCALE_S1159 = &H28
Public Const LOCALE_S2359 = &H29
Public Const LOCALE_SABBREVCTRYNAME = &H7
Public Const LOCALE_SABBREVDAYNAME1 = &H31
Public Const LOCALE_SABBREVDAYNAME2 = &H32
Public Const LOCALE_SABBREVDAYNAME3 = &H33
Public Const LOCALE_SABBREVDAYNAME4 = &H34
Public Const LOCALE_SABBREVDAYNAME5 = &H35
Public Const LOCALE_SABBREVDAYNAME6 = &H36
Public Const LOCALE_SABBREVDAYNAME7 = &H37
Public Const LOCALE_SABBREVLANGNAME = &H3
Public Const LOCALE_SABBREVMONTHNAME1 = &H44
Public Const LOCALE_SABBREVMONTHNAME10 = &H4D
Public Const LOCALE_SABBREVMONTHNAME11 = &H4E
Public Const LOCALE_SABBREVMONTHNAME12 = &H4F
Public Const LOCALE_SABBREVMONTHNAME13 = &H100F
Public Const LOCALE_SABBREVMONTHNAME2 = &H45
Public Const LOCALE_SABBREVMONTHNAME3 = &H46
Public Const LOCALE_SABBREVMONTHNAME4 = &H47
Public Const LOCALE_SABBREVMONTHNAME5 = &H48
Public Const LOCALE_SABBREVMONTHNAME6 = &H49
Public Const LOCALE_SABBREVMONTHNAME7 = &H4A
Public Const LOCALE_SABBREVMONTHNAME8 = &H4B
Public Const LOCALE_SABBREVMONTHNAME9 = &H4C
Public Const LOCALE_SCOUNTRY = &H6
Public Const LOCALE_SDATE = &H1D
Public Const LOCALE_SDAYNAME1 = &H2A
Public Const LOCALE_SDAYNAME2 = &H2B
Public Const LOCALE_SDAYNAME3 = &H2C
Public Const LOCALE_SDAYNAME4 = &H2D
Public Const LOCALE_SDAYNAME5 = &H2E
Public Const LOCALE_SDAYNAME6 = &H2F
Public Const LOCALE_SDAYNAME7 = &H30
Public Const LOCALE_SENGCOUNTRY = &H1002
Public Const LOCALE_SENGLANGUAGE = &H1001
Public Const LOCALE_SINTLSYMBOL = &H15
Public Const LOCALE_SLANGUAGE = &H2
Public Const LOCALE_SLIST = &HC
Public Const LOCALE_SLONGDATE = &H20
Public Const LOCALE_SMONDECIMALSEP = &H16
Public Const LOCALE_SMONGROUPING = &H18
Public Const LOCALE_SMONTHNAME1 = &H38
Public Const LOCALE_SMONTHNAME10 = &H41
Public Const LOCALE_SMONTHNAME11 = &H42
Public Const LOCALE_SMONTHNAME12 = &H43
Public Const LOCALE_SMONTHNAME2 = &H39
Public Const LOCALE_SMONTHNAME3 = &H3A
Public Const LOCALE_SMONTHNAME4 = &H3B
Public Const LOCALE_SMONTHNAME5 = &H3C
Public Const LOCALE_SMONTHNAME6 = &H3D
Public Const LOCALE_SMONTHNAME7 = &H3E
Public Const LOCALE_SMONTHNAME8 = &H3F
Public Const LOCALE_SMONTHNAME9 = &H40
Public Const LOCALE_SMONTHOUSANDSEP = &H17
Public Const LOCALE_SNATIVECTRYNAME = &H8
Public Const LOCALE_SNATIVEDIGITS = &H13
Public Const LOCALE_SNATIVELANGNAME = &H4
Public Const LOCALE_SNEGATIVESIGN = &H51
Public Const LOCALE_SPOSITIVESIGN = &H50
Public Const LOCALE_SSHORTDATE = &H1F
Public Const LOCALE_STIME = &H1E
Public Const LOCALE_STIMEFORMAT = &H1003