Solved

Get Regional Settings from Windows PC

Posted on 2001-06-22
14
209 Views
Last Modified: 2012-06-27
How can I retreive the Regional Date Settings from a Windows PC.  I have a vb application, I want a label to display the date format used on the PC. Example:
dd/mm/yyyy  or  mm.dd.yy  or  mm/dd/yyyy  etc.  Retreiving any other settings such as the time format etc... would be helpful.  How to retreive this string from the system?
0
Comment
Question by:MitchSavage
  • 4
  • 3
  • 2
  • +3
14 Comments
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
ID: 6220176
save this code in a .bas module:

'*********BEGIN CODE
Attribute VB_Name = "Module1"
Option Explicit
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

Declare Function SetLocaleInfo Lib "kernel32" Alias _
"SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, _
ByVal lpLCData As String) As Boolean

Declare Function GetUserDefaultLCID% Lib "kernel32" ()

'Enum para envio de valores
'de la configuracion regional.
Public Enum LOCAL_INFO
  ICountry = &H5
  ICurrencyDecDigits = &H19
  IDefaultCodePage = &HB
  IIDEDefCountry = &HA
  IIDEDefLang = &H9
  IDecDigits = &H11
  SyAM = &H28
  SyPM = &H29
  SyShortCtryName = &H7
  SyShortDayName1 = &H31
  SyShortDayName2 = &H32
  SyShortDayName3 = &H33
  SyShortDayName4 = &H34
  SyShortDayName5 = &H35
  SyShortDayName6 = &H36
  SyShortDayName7 = &H37
  SyShortLangName = &H3
  SymShortMonthName1 = &H44
  SyCountry = &H6
  SyShortCurrency = &H14
  SyDateSep = &H1D
  SyLongDayName1 = &H2A
  SyLongDayName2 = &H2B
  SyLongDayName3 = &H2C
  SyLongDayName4 = &H2D
  SyLongDayName5 = &H2E
  SyLongDayName6 = &H2F
  SyLongDayName7 = &H30
  SymCtryNameInEng = &H1002
  SyLangInEnglish = &H1001
  SyNumberDigitsGroup = &H10
  SyInternacLang = &H15
  SKeybDistrib = &H2
  SyListSep = &HC
  SyLongDate = &H20
  SyDecimalSep = &H16
  ICurrencyDigitsGroup = &H18
  SyMonthName1 = &H38
  SyMonthName2 = &H39
  SyMonthName3 = &H3A
  SyMonthName4 = &H3B
  SyMonthName5 = &H3C
  SyMonthName6 = &H3D
  SyMonthName7 = &H3E
  SyMonthName8 = &H3F
  SyMonthName9 = &H40
  SyMonthName10 = &H41
  SyMonthName11 = &H42
  SyMonthName12 = &H43
  SyCurrencyThousandSep = &H17
  SyNativeCtryName = &H8
  SyNativeDigits = &H13
  SyNativeLangName = &H4
  SyNegativeSign = &H51
  SyPositiveSign = &H50
  SyShortDate = &H1F
  SyThousand = &HF
  SyHourSeparator = &H1E
  SyHourFormat = &H1003
End Enum
Public Function Get_locale(RSValue As LOCAL_INFO) ' Retrieve the regional setting

      Dim Symbol As String
      Dim iRet1 As Long
      Dim iRet2 As Long
      Dim lpLCDataVar As String
      Dim Pos As Integer
      Dim Locale As Long
     
      Locale = GetUserDefaultLCID()

'LOCALE_SDATE is the constant for the date separator
'as stated in declarations
'for any other locale setting just change the constant

'Function can also be re-written to take the
'locale symbol being requested as a parameter
     
      iRet1 = GetLocaleInfo(Locale, RSValue, _
      lpLCDataVar, 0)
      Symbol = String$(iRet1, 0)
     
      iRet2 = GetLocaleInfo(Locale, RSValue, Symbol, iRet1)
      Pos = InStr(Symbol, Chr$(0))
      If Pos > 0 Then
           Symbol = Left$(Symbol, Pos - 1)
           Get_locale = Symbol
      End If

End Function

Public Function Set_locale(RSValue As LOCAL_INFO) 'Change the regional setting

      Dim Symbol As String
      Dim iRet As Long
      Dim Locale As Long
     
'LOCALE_SDATE is the constant for the date separator
'as stated in declarations
'for any other locale setting just change the constant

'Function can also be re-written to take the
'locale information being set as a parameter

      Locale = GetUserDefaultLCID() 'Get user Locale ID
      Symbol = "-" 'New character for the locale
      iRet = SetLocaleInfo(Locale, RSValue, Symbol)
     
End Function

'*** END CODE
0
 
LVL 15

Expert Comment

by:ameba
ID: 6220211
Private Sub Form_Click()
    Caption = Replace(Replace(Replace(Format(DateSerial(2000, 11, 22), "Short Date"), _
        "22", "dd"), "11", "mm"), "2000", "yyyy")
End Sub
0
 

Expert Comment

by:ppaulinolopes
ID: 6220214

This code retrieves the short date format set in Regional Settings in the computer, and sets to "mm/dd/yyyy" this configuration.

Just add 2 command buttons to the project (command1 retrieves, command2 set), and paste the code below:

'''''bas module 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
Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Boolean
Declare Function GetUserDefaultLCID% Lib "kernel32" ()
Public Const LOCALE_SSHORTDATE = &H1F
'''''end of bas module code


'''''form code
Public Sub Get_locale() ' Retrieve the regional setting
      Dim Symbol As String
      Dim iRet1 As Long
      Dim iRet2 As Long
      Dim lpLCDataVar As String
      Dim Pos As Integer
      Dim Locale As Long
      Locale = GetUserDefaultLCID()
      iRet1 = GetLocaleInfo(Locale, LOCALE_SSHORTDATE, lpLCDataVar, 0)
      Symbol = String$(iRet1, 0)
      iRet2 = GetLocaleInfo(Locale, LOCALE_SSHORTDATE, Symbol, iRet1)
      Pos = InStr(Symbol, Chr$(0))
      If Pos > 0 Then
           Symbol = Left$(Symbol, Pos - 1)
           MsgBox "Regional Setting = " + Symbol
      End If
End Sub
Public Sub Set_locale() 'Change the regional setting
      Dim Symbol As String
      Dim iRet As Long
      Dim Locale As Long
      Locale = GetUserDefaultLCID() 'Get user Locale ID
      Symbol = "mm/dd/yyyy" 'New character for the locale
      iRet = SetLocaleInfo(Locale, LOCALE_SSHORTDATE, Symbol)
End Sub
Private Sub Command1_Click()
      Get_locale
End Sub
Private Sub Command1_Click()
      Set_locale
End Sub
'''''end of form code
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6220779
ppaulinolopes, interesting code.
0
 
LVL 27

Expert Comment

by:Ark
ID: 6223154
ameba: COOL!
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6223761
ameba, your code is interesting but it does not work for me. ppaulinolopes's is the correct one.

hongjun
0
 
LVL 15

Expert Comment

by:ameba
ID: 6224079
hongjun,
you are right, I was only trying to use one-liner

Private Sub Form_Click()
    Caption = Replace(Replace(Replace(Replace(Format(DateSerial(2000, 11, 22), "Short Date"), _
       "22", "dd"), "11", "mm"), "20", "yy"), "00", "yy")
End Sub
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 15

Expert Comment

by:ameba
ID: 6224119
Btw, hongjun, nice single-page profile   ;-)
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6225788
>>Btw, hongjun, nice single-page profile
Thanks

hongjun
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6225826
I would like to say something since the code posted by
 ppaulinolopes is similar to mine.

I posted a code that you could use for every value on reginal settings of Control Panel applet (as you asked on question), not only date.
Cheers
0
 

Author Comment

by:MitchSavage
ID: 6231641
ppaulinolopes's code is in fact very interesting.  I am using the code posted by Richie Simonetti.  Richie's code is working so I am awarding Richie the points.  Thanks everyone for your help.

Best Regards to all
Mitch......
0
 

Author Comment

by:MitchSavage
ID: 6231660
Richie Simonetti,
Sorry it took so long to respond and award points.  I had to make a few mods here and there to my program, but now I have exactly what I want.  I'm also using some of the other regional items that you provided the correct code for.  So, Thanks alot for the help.

Best Regards,

MitchSavage.....
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6231838
You are welcome anytime!
Cheers
0
 

Expert Comment

by:ppaulinolopes
ID: 6232412

One more comment about this question:

1) The code posted by Richie_Simonetti really works

2) The code posted by ppaulinolopes really works too

3) Richie_Simonetti was faster then ppaulinolopes

4) Richie_Simonetti should award the points

5) That's the life

See you, guys...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now