Determine Windows's Regional Settings for the Short Date Format, and Prevent Corrupting Your Database

click to enlarge.

published 21.August.2008

Introduction

As a junior programmer many many years ago I had to deploy a VB6 application to a corporate network, where I had been informed by their technical support team that every windows machine on the network was configured with the regional and language options of English (United Kingdom).

Being young and naive I didn't question this assumption or didn't check for it within my Visual Basic code. Needless to say one machine was configured as English (United States) and that machine was trying to save invalid dates into the database. Luckily I spotted the situation early on, and because I had an audit trail which logged updates against a windows machine name, I was able to correct the dirty data.

A better solution would have been to test that the regional settings for the machine where in the correct format to start with, and prevent the program from launching until the settings had been corrected. Now when any of programs load, besides checking that the MDAC version is current, I use the code below to validate that the machines regional settings are correct.

Using Windows GetLocaleInfo API Function

The first step is to copy the block of code below into a module within your application code, so that you can reference the API function GetLocaleInfo.

Public Const LOCALE_SSHORTDATE As Long = &H1F
Public Const LOCALE_SYSTEM_DEFAULT As Long = &H400

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

GetLocaleString Function

Next you need to copy the function below into a global functions module, so that the code can be called from anywhere within your application.

Public Function GetLocaleString(ByVal lLocaleNum As Long) As String
Dim lBuffSize   As String
Dim sBuffer     As String
Dim lRet        As Long

    lBuffSize = 256
    sBuffer = String$(lBuffSize, vbNullChar)

    lRet = GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,
        lLocaleNum, sBuffer, lBuffSize)

    If lRet > 0 Then
        GetLocaleString = Left$(sBuffer, lRet - 1)
    End If
End Function

Using the GetLocaleString function to check the short date format

The sample code below shows how you call the GetLocaleString function and test that it is equal to format of dd/MM/yyyy. If it does not match then it prompts the user with an appropriate error message, and terminates the application.

If GetLocaleString(LOCALE_SSHORTDATE) <> "dd/MM/yyyy" Then
    MsgBox "The control panel date format is not English/UK" _
        & vbCr & "Please change to English/UK and restart.", _
        vbCritical, "Date Format Error"

    'code to exit your application
End If

What Next?

Bookmark this article at :-

 

 

About Author:
I'm a father, husband, and a software developer who works for the NHS. more »

Sections :
« Articles
« Contact


 

FreeImage - an open source library project for developers who would like to support popular graphics image formats like PNG, BMP, JPEG, TIFF.