简体   繁体   中英

How to set decimal separator in MS Access using VBA?

My software creates PAIN001.XML files directly from an Access financial database. The decimal separator must always be a dot. The numbers are formatted with:

MyText = Format(MyNumber, "#0.00")

However, the format string's dot is automatically replaced by the system decimal separator, which might be "," instead of "." !

In Excel there are easy solutions, for example:

Application.DecimalSeparator = "."
...

However, MS Access doesn't recognize this application property. Is there a simple way to define a decimal separator within Access vba code ?

Of course, one can create a function which scans each MyText number for wrong decimal separators and replaces them with a dot, but this function would have to be called separately for each number, slowing down the code quite a lot…

The decimal separator must always be a dot.

Then use Str :

MyText = Str(MyNumber)

To convert such a string to a number use Val :

MyNumber = Val(MyText)

I guess the problem is not solveable with the decimal separator Application.DecimalSeparator = "." , even if it was supported by the Access library. It is a rather complicated issue, for the non-US users, as we are used to have , as a decimal separator.

In general, VBA considers only . as a decimal separator. Without taking care of the application default separator, the location of the user and their settings. Thus, some interesting cases could happen:

在此处输入图片说明

Sub TestMe()
    Dim myText As String
    myText = "123,42"
    Debug.Print Replace(Format(myText, "#0.00"), ",", ".")
End Sub

A possible solution, that I have implemented some time ago was to use Replace() and to replace as in the gif above. It could be a bit slow indeed, but taking into account the usage of VBA and Access, extreme speed is not something the app could achieve anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM