Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Number format regional settings in VB6

Posted on 2008-10-01
5
Medium Priority
?
2,307 Views
Last Modified: 2010-05-18
I have an app that is developed in vb6. When I do calculations I format numbers based on a '.' for the decimals. I now have customers in the Netherlands who format using ',' and when I do any calculations or submit the data I get errors.

How can I change this code for example:

myTotal = format(mysubtotal + mytax,"0.00")

Another One

rs!total = format(myTotal, "0.00')
I don't want to use the comma but would rather like code to do the math using the local regional settings.
Not all values are currency other some are qty's .

Please can anyone help

Thanks


0
Comment
Question by:cwaldick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Michael_D
ID: 22615463
If you want to use format function is to change Regional Settings of the. You might want to save current settings before calling the format function and restore them back afterward

you can find a code for changing  reg.settings here: http://www.vbcode.com/Asp/showsn.asp?theID=8337

Another solution (better) would be to create your own function that format the numbers the way you need.
0
 
LVL 22

Accepted Solution

by:
danaseaman earned 2000 total points
ID: 22617145
It is not recommended changing Regional Settings on another users machine.
You can format a number using U.S. English - LCID-1033, or any other supported LCID regardless of the users configurations using the following code:

Option Explicit
 
Private Declare Function GetLocaleInfoA Lib "kernel32" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpData As Any, ByVal cchData As Long) As Long
Private Declare Function GetNumberFormatA Lib "kernel32" (ByVal Locale As Long, ByVal dwFlags As Long, ByVal lpValue As String, ByRef lpFormat As NUMBERFMT, ByVal lpNumberStr As String, ByVal cchNumber As Long) As Long
 
Private Type NUMBERFMT
        NumDigits       As Long     ' number of decimal digits
        LeadingZero     As Long     ' if leading zero in decimal fields
        Grouping        As Long     ' group size left of decimal
        lpDecimalSep    As String   ' ptr to decimal separator string
        lpThousandSep   As String   ' ptr to thousand separator string
        NegativeOrder   As Long     ' negative number ordering
End Type
 
Private Sub Form_Load()
   Debug.Print FormatNumberByLCID("123456789", 1033) 'U.S. English
   Debug.Print FormatNumberByLCID("123456789", 1043) 'Dutch
End Sub
 
Public Function FormatNumberByLCID(ByVal sNumber As String, _
   Optional ByVal LCID As Long) As String
 
   Const LOCALE_SDECIMAL As Long = &HE
   Const LOCALE_STHOUSAND As Long = &HF&
   Const LOCALE_SGROUPING As Long = &H10
   Const LOCALE_IDIGITS As Long = &H11
   Const LOCALE_ILZERO As Long = &H12
   Const LOCALE_INEGNUMBER As Long = &H1010
   
   Dim Buffer          As String * 255
   
   Dim NF   As NUMBERFMT
   
   With NF
      .NumDigits = Val(pfGLI(LCID, LOCALE_IDIGITS))
      .Grouping = Val(pfGLI(LCID, LOCALE_SGROUPING))
      .lpDecimalSep = Left$(pfGLI(LCID, LOCALE_SDECIMAL), 1)
      .lpThousandSep = Left$(pfGLI(LCID, LOCALE_STHOUSAND), 1)
      .NegativeOrder = Val(pfGLI(LCID, LOCALE_INEGNUMBER))
      .LeadingZero = Val(pfGLI(LCID, LOCALE_ILZERO))
   End With
         
   GetNumberFormatA LCID, 0, sNumber, NF, Buffer, 255
   FormatNumberByLCID = StripNull(Buffer)
 
End Function
 
Public Function pfGLI(ByVal LCID As Long, ByVal reqInfo As Long) As String
   Dim nRet             As Long
   Dim Buffer           As String
   
   nRet = GetLocaleInfoA(LCID, reqInfo, Buffer, 0&)
 
   If nRet Then
      Buffer = Space$(nRet)
      nRet = GetLocaleInfoA(LCID, reqInfo, Buffer, Len(Buffer))
      pfGLI = Left$(Buffer, nRet - 1)
   End If
 
End Function
 
Public Function StripNull(ByVal StrIn As String) As String
   Dim nul              As Long
   nul = InStr(StrIn, vbNullChar)
   Select Case nul
      Case Is > 1
         StripNull = Left$(StrIn, nul - 1)
      Case 1
         StripNull = ""
      Case 0
         StripNull = Trim$(StrIn)
   End Select
End Function

Open in new window

0
 
LVL 17

Expert Comment

by:CSecurity
ID: 22620049
first check that is there comma ,
InStr(1, IP, ",", vbTextCompare)

and then replace , with .
using replace function
regards...
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 22620083
>>first check that is there comma ,
>>InStr(1, IP, ",", vbTextCompare)

>>and then replace , with .
>>using replace function
>>regards...


What if regional setting are using comma as thousand separator? Then 1000 will be represented as 1,000.00
After applying your formula you will get 1.000.00 which is not legal decimal number at all.

danaseaman's solution is the way to do it right
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

722 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