Writing text file with tab coluns

I basically want to create a text file with a column separated by tab like this:

Column 1 <tab> Column 2
123 <tab> 43534

My data is coming from a SQL database. I tried this code:

Function teste
Dim sql, numCur, numRows, row, txt
sql = "SELECT * FROM Paradas"
numCur=$DBCursorOpenSQL("DB", sql)
numRows=$DBCursorRowCount(numCur)
If numRows > 0 Then
For row=1 To numRows
txt = $DBCursorGetValue(numCur, "ID")
txt = txt & vbTab & $Format("#.###",$DBCursorGetValue(numCur, "Qtd_Horas"))
$FileWrite("C:\Relatorios\teste.txt", txt,1)
$DBCursorNext(numCur)
Next
End If
$DBCursorClose(numCur)
End Function

Unfortunately, it doesn’t work. But if I change the code for this:

Function teste
Dim sql, numCur, numRows, row, txt
sql = "SELECT * FROM Paradas"
numCur=$DBCursorOpenSQL("DB", sql)
numRows=$DBCursorRowCount(numCur)
If numRows > 0 Then
For row=1 To numRows
txt = $DBCursorGetValue(numCur, "ID")
txt = txt & "," & $Format("#.###",$DBCursorGetValue(numCur, "Qtd_Horas"))
$FileWrite("C:\Relatorios\teste.txt", txt,1)
$DBCursorNext(numCur)
Next
End If
$DBCursorClose(numCur)
End Function

Now the code is working, but the result is:

Column 1, Column 2
123, 43534

I tried this too, but it doesn’t work again.

Function teste
Dim sql, numCur, numRows, row, txt
sql = "SELECT * FROM Paradas"
numCur=$DBCursorOpenSQL("DB", sql)
numRows=$DBCursorRowCount(numCur)
If numRows > 0 Then
For row=1 To numRows
txt = $DBCursorGetValue(numCur, "ID")
txt = txt & $Asc2Str(9) & $Format("#.###",$DBCursorGetValue(numCur, "Qtd_Horas"))
$FileWrite("C:\Relatorios\teste.txt", txt,1)
$DBCursorNext(numCur)
Next
End If
$DBCursorClose(numCur)
End Function

If anyone helps me I will enjoy it.

  • *** Apparently the FileWrite function does not like the tab character.
    *** Add the following lines to the beginning of your function. Use OpenTextFile instead of CreateTextFile to append to a file.

    Dim MyFSO, MyStream
    Set MyFSO = CreateObject("Scripting.FileSystemObject")
    Set MyStream = MyFSO.CreateTextFile("C:\Relatorios\teste.txt", True, False)

    *** Replace $FileWrite(… with the following

    MyStream.WriteLine (txt)

    *** Add the following to the end of your function

    MyStream.Close
    Set MyStream = Nothing
    Set MyFSO = Nothing