I'm using the below code to export a query in a specific txt format, setup so it can be imported into another program. However I can't seem to figure out now to force the output to be Dot "." separated instead of my own locale which uses Comma "," as a separator (the query in the database has the data shown as commas). Basically I need all the commas changed to dots when running the export (perhaps excluding the 'anm' column)
The left is as it should be, the right is what it is currently.
Option Compare Database
Option Explicit
Public Function fnTransposeToTxt()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim fd As DAO.Field
Dim fnum As Integer
Dim path As String
Dim OK As Boolean
Dim var As Variant
' export to this file
path = CurrentProject.path & "\Export.txt"
fnum = FreeFile
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("VBK_Knude", dbOpenSnapshot, dbReadOnly)
With rst
If Not (.BOF And .EOF) Then
.MoveFirst
OK = True
Open path For Output As fnum
End If
Do Until .EOF
For Each fd In .Fields
var = fd.Value
Select Case fd.Name
Case "Z_F", "DEPTH", "OB", "PERPEND", "Q"
var = Format$(Val(var & ""), "0.00")
Case "DIMENSION"
var = Format$(Val(var & ""), "0.000")
Case "CATCHMENT"
var = Format$(Val(var & ""), "0.0000")
End Select
Print #fnum, fd.Name & " " & var
Next
.MoveNext
If Not (.EOF) Then
Print #fnum, ""
End If
Loop
.Close
End With
Set rst = Nothing
Set dbs = Nothing
If OK Then
Close #fnum
MsgBox "table imported to " & path
End If
End Function
Edit: New working code below based on answers. Option Compare Database Option Explicit
Public Function fnTransposeToTxt()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim fd As DAO.Field
Dim fnum As Integer
Dim path As String
Dim OK As Boolean
Dim var As Variant
' export to this file
path = FilToSave
fnum = FreeFile
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("VBK_Knude", dbOpenSnapshot, dbReadOnly)
With rst
If Not (.BOF And .EOF) Then
.MoveFirst
OK = True
Open path For Output As fnum
End If
Do Until .EOF
For Each fd In .Fields
var = fd.Value
Select Case fd.Name
Case "AFLKOEF"
var = Format$(var, "0.0")
var = Replace(var, ",", ".")
Case "XY", "TEXTXY", "Z_F", "DYBDE", "OB", "PERPEND", "Q", "STATION", "PERPEND", "AFSTRØM"
var = Format$(var, "0.00")
var = Replace(var, ",", ".")
Case "DIMENSION"
var = Format$(var, "0.000")
var = Replace(var, ",", ".")
Case "OPLAND"
var = Format$(var, "0.0000")
var = Replace(var, ",", ".")
End Select
Print #fnum, fd.Name & " " & var
Next
.MoveNext
If Not (.EOF) Then
Print #fnum, ""
End If
Loop
.Close
End With
Set rst = Nothing
Set dbs = Nothing
If OK Then
Close #fnum
MsgBox "table imported to " & path
End If
End Function
Unfortunately, the Format
function of MS Access VBA won't let you specify the decimal separator to use. It will always use the regional settings of the current user. The only way to fix this is to manually replace the decimal separator with the one you use, for every single value.
Thus,
var = Format$(Val(var & ""), "0.00")
becomes
var = Format$(Val(var & ""), "0.00")
var = Replace(var, ",", ".")
For completeness, the only other option I am aware of would be to use Str
to convert the number to a string, which will, contrary to Format
, always use the dot as the decimal separator. Unfortunately, Str
does not allow you to specify the number of digits after the decimal point, so this will probably not be an option for your use case.