Log in

View Full Version : Field type change to dbText



jcsabi
06-19-2013, 04:33 AM
Hello,

I would like to change the type of a field from double into text (dbText), without error 3219. It is possible without any data loss by hand, but how is it possible in VBA?

Thanks,
Csaba


Sub CSVexport()
DoCmd.TransferDatabase acImport, "Microsoft Access", Path, acTable, "E_CS_N", "E_CS_N"
' This is generate an error 3219:
CurrentDb.TableDefs("E_CS_N").Fields("G3E").Type = dbText
DoCmd.TransferText acExportDelim, "CSVexport", "E_CS_N", Path & ".csv", False, , 1250
DoCmd.RunSQL ("DROP TABLE [E_CS_N];")
End Sub

jcsabi
06-20-2013, 12:59 AM
Exactly, my problem is the G3E field type is dbDouble, but it contains integers, and after the export, it has unnecessary decimal symbols. There are several tables for this exporting process, so I need to solve it in VBA.

There is another way. After the CSV export load the csv file into a string, then replace the text ",00;" to ";" in the string and save it to the csv file, like this:

Sub CSVexport(Path As String)
Dim fs, fl3, f3
Dim src As String
DoCmd.TransferDatabase acImport, "Microsoft Access", Path, acTable, "E_CS_N", "E_CS_N"
DoCmd.TransferText acExportDelim, "CSVexport", "E_CS_N", Path & ".csv", False, , 1250
DoCmd.RunSQL ("DROP TABLE [E_CS_N];")
Set fs = CreateObject("Scripting.FileSystemObject")
Set fl3 = fs.GetFile(Path & ".csv")
Set f3 = fl3.OpenAsTextStream(1, -2)
src = f3.ReadAll()
f3.Close
src = Replace(src, ",00;", ";")
Kill Path & ".csv"
fs.CreateTextFile Path & ".csv"
Set fl3 = fs.GetFile(Path & ".csv")
Set f3 = fl3.OpenAsTextStream(2, -2)
f3.Write (src)
f3.Close
End Sub