Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining system regional settings in VBA code

Status
Not open for further replies.

Quimbly

Programmer
Oct 24, 2002
33
CA
How do you do it?

In other words, at run-time, I want to determine the system regional settings and be able to test against the returned info.

Of course, using something like MonthName(1), it would give me either an English name for January, or the word in some other language. But there's got to be a better way...

Any suggestions?

 
Hi Quimbly,

Take a look at the Application.International in the F1 (HELP)

Here's a quick example:
Code:
Sub test()
'Use this to check out the number of the xlCountrySetting
'MsgBox Application.International(xlCountrySetting)

    If Application.International(xlCountrySetting) = 47 Then
        StatBarMsgString = "Gjeldende dato og tid: "
    ElseIf Application.International(xlCountrySetting) = 31 Then
        StatBarMsgString = "Huidige dag en datum: "
    Else
        StatBarMsgString = "Current date and time: "
    End If
    Application.StatusBar = StatBarMsgString
    
End Sub

Enjoy,
Joost Verdaasdonk
 
Well, I found this code:


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

Public Function ReadLocaleInfo(ByVal lInfo As Long) As String

Dim sBuffer As String
Dim rv As String

sBuffer = String$(256, 0)
rv = GetLocaleInfo(LOCALE_USER_DEFAULT, lInfo, sBuffer, Len(sBuffer))

If rv > 0 Then
ReadLocaleInfo = Left$(sBuffer, rv - 1)
Else
'MsgBox "Not found"
ReadLocaleInfo = ""
End If
End Function

I've tried it with the following inputs:

MsgBox ("UserDefault: " & ReadLocaleInfo(LOCALE_USER_DEFAULT))
MsgBox ("SDecimal: " & ReadLocaleInfo(LOCALE_SDECIMAL))
MsgBox ("ILDate: " & ReadLocaleInfo(LOCALE_ILDATE))
MsgBox ("Country: " & ReadLocaleInfo(LOCALE_ICOUNTRY))

...and in each case, it returned an empty string.

Any ideas?
 
There are two APIs GetUserDefaultLCID and GetLocaleInfo. If you search MSDN or other souces for these there are plenty of examples for retrieving the info you need. To get started heres an example that gets the date and time format strings. To get other info you need to use different LOCAL_??? values.

Code:
Public 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

Public Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
Code:
Public Function f_GetUserLOCALEinfo(ll_Type As Long) As String

   Dim ll_LocaleID As Long
   Dim ll_Return As Long

   ll_LocaleID = GetUserDefaultLCID()
   
   ll_Return = GetLocaleInfo(ll_LocaleID, ll_Type, f_GetUserLOCALEinfo, Len(f_GetUserLOCALEinfo))
   
   If ll_Return Then
      f_GetUserLOCALEinfo = Space(ll_Return)
      ll_Return = GetLocaleInfo(ll_LocaleID, ll_Type, f_GetUserLOCALEinfo, Len(f_GetUserLOCALEinfo))
      If ll_Return Then
         f_GetUserLOCALEinfo = Left$(f_GetUserLOCALEinfo, ll_Return - 1)
      Else
         MsgBox "Error ???"
      End If
   Else
      MsgBox "Error ???"
   End If

End Function
Code:
Private Sub Command6_Click()

   Dim ls_DateString As String
   Dim ls_TimeString As String
   Dim ls_24hour As String
   
   Const LOCALE_SHORTDATE = &H1F
   Const LOCALE_STIMEFORMAT = &H1003
  
   ls_DateString = f_GetUserLOCALEinfo(LOCALE_SHORTDATE)
   ls_TimeString = f_GetUserLOCALEinfo(LOCALE_STIMEFORMAT)

  
End Sub

 
Hi,

Have you even tried my simpler solution?

This Api Should work fine but I think you're problem should be solved with the above.

Enjoy,
Joost
 
SonOfEmidec1100,
Can you show me how to get the integer value indicating the upper boundary of the two-digit year range?

CAL_ITWODIGITYEARMAX

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top