Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, respected professionals from CodeProject.

I have created dialog box with edit control and 2 buttons.

First button loads double( let us say, for example 25.45 ) from MS Access 2007 database into edit control.

Second one stores the value from edit control into the same field in database. It enables the user to edit the value of that field in the database ( for example, let us say that user entered 56.89 into edit box ).

I use ADO to execute following query:

wchar_t text[10]; // here I store the text from edit control

GetDlgItemText( ..., text, ... );

// create query

wchar_t query[50] = L”INSERT INTO MyTable ( MyField ) VALUES( _wtof( text) )”;

// execute it

Everything works fine on Windows XP, but on Windows 7 I get an error that I will describe bellow via an example:

On Windows XP:

I press first button . I get 25.45 ( pay attention to dot in 25.45 ! ) loaded in my edit control. Then I press second button. The value from edit control is successfully stored.

On Windows 7:

I press first button . I get 25,45( pay attention to comma in 25,45 ! ) loaded in my edit control. Then I press second button. The value from edit control is stored as 2545.

When I open the database on Windows XP, decimal value is stored as 25.45 ( again, pay attention to dot in 25.45 ! ).

When I open the database on Windows 7, decimal value is stored as 25,45 ( again, pay attention to comma in 25.45 ! ).

It seems that MS Access 2007 stores double number on Windows XP as xxx.yyy and on Windows 7 as xxx,yyy .

After going to Control Panel-> Regional and Language Options, I have seen that the settings for decimals on Windows 7 use comma for decimal numbers, and on Windows XP use dot.

My Question:

How can I change my fields definition, to avoid this behavior , so the format of the field uses dot like this : xxx.yyy ?

Is there something I can do with the C++ / Win32 / ADO code to fix this behavior ?

NOTE: I can NOT force the user to change Regional and Language Options, my code must adjust to this.

Thank you.

Regards.
Posted

The edit controls and MS Access are working correctly.
They are using the decimal as defined in the Regional and Language Options as you have noticed.

The problem is the use of the _wtof[^] function.

The documentation states:
Quote:
The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current locale.
So the _wtof function uses the current locale. This is however not the one as set in the Regional and Language Options as you might expect.
In the documentation from setlocale, _wsetlocale[^] it states:
Quote:
At program startup, the equivalent of the following statement is executed:
C++
setlocale( LC_ALL, "C" );
A value of C specifies the minimal ANSI conforming environment for C translation. The C locale assumes that all char data types are 1 byte and that their value is always less than 256.
This means that the '.' is used as the decimal point and converting the text with the ',' fails.

To solve your problem you need to set the locale to the one used by windows. For this you can use the function _wsetlocale like:
C++
_wsetlocale( LC_ALL, L"" );
Alternatively you can use LC_NUMERIC to only effect the decimal point:
C++
_wsetlocale( LC_NUMERIC, L"" );
 
Share this answer
 
Comments
AlwaysLearningNewStuff 29-Aug-13 18:00pm    
If I use _wcsetlocale function, will it change the locale for the entire OS during my program execution, or will the change affect only my program and leave OS and other programs unchanged?

If it only changes locale of my program then this could be the answer I was looking for.

Thank you anyway for replying to my question, I appreciate it.

Regards.
André Kraak 30-Aug-13 2:45am    
Only the locale of your program is changed.
AlwaysLearningNewStuff 30-Aug-13 4:55am    
This could be what I am looking for, then.
Since I use pure Win32 API, I have this question:
Should I put the _wsetlocale( LC_NUMERIC, L"" ); in the WM_CREATE section, or in WinMain ?

That would be all.

Thank you for your help, my 5.

Regards.
André Kraak 30-Aug-13 9:04am    
I personally place the call in the place where I use functions that depend on it. This ensure the correct local is used at all time.
You can use _wsetlocale( LC_NUMERIC, NULL ) to check whether the current value is L"C" and use then use _wsetlocale( LC_NUMERIC, "" ).
I like Andre's solution (and I gave +5 for it too). An alternative thing you can do (and you probably need to do if you are dealing with multiple versions of the stored data) is get the string from the database, and replace all "," with "." in the string before converting to double.
 
Share this answer
 
Comments
AlwaysLearningNewStuff 30-Aug-13 2:15am    
I guess I could write the function to do that, and change the string before I build my query string... It seems as the easiest solution, and I would not touch locale...

5ed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900