Solved

Get Regional Settings from Windows PC

Posted on 2001-06-22
14
212 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vba is buggy because of corrupted user profile. Any fast way to fix it? 28 129
Window placement 17 77
Excel - Save a copy of work book 13 83
Problem to With line 4 43
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

863 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

26 Experts available now in Live!

Get 1:1 Help Now