1

I am attempting to write data from VBA to a text file. My syntax shows no debug or compile errors, but when the method completes and I open the text file it is full of nothing readable. It all appears to be in Chinese. This is my syntax, what is it that is throwing the output off so that it is illegible?

Function WriteData(fulllocale As String, tblName As String, CString As String)
  Dim fso As Object
  Set fso = CreateObject("Scripting.FileSystemObject")

  Dim Fileout As Object
  'Set Fileout = fso.OpenTextFile("C:\Test.txt", ForAppending, True)
  Set Fileout = fso.OpenTextFile("C:\Test.txt", 8, True)
  Fileout.Write fulllocale & "," & tblName & "," & CString & vbCrLf
  Fileout.Close
End Function

Edit - additional info
These are the values being passsed (not the bold text that is a description)
Database Name:C:\Test\db1.accdb
Table Name:Logs
Connection Info:ODBC;DRIVER=SQL Server;SERVER=servername;UID=userid;PWD=password;APP=Microsoft Office 2013;DATABASE=database



Edit 2
This is the syntax I am using to call

Sub Testing()
Dim Fileout As Object
Dim fso As Object
Dim objFSO As Object
Dim accapp As Access.Application
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim colFiles As Collection
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objRegExp As Object
Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.pattern = ".accdb"
objRegExp.IgnoreCase = True

Set colFiles = New Collection

RecursiveFileSearch "C:\Databases\", objRegExp, colFiles, objFSO

For Each f In colFiles
    Set accapp = New Access.Application
    accapp.OpenCurrentDatabase (f)
    On Error Resume Next
    accapp.Visible = False
    Set db = accapp.CurrentDb
    For Each tdf In db.TableDefs
      If Not (tdf.Name Like "MSys*") Then
        WriteData CStr(f), tdf.Name, tdf.Connect
      End If
    Next
    Set tdf = Nothing
    Set db = Nothing
Next
Set objFSO = Nothing
Set objRegExp = Nothing

End Sub
  • 1
    What is in the strings that you're passing it? Note that the last parameter of `OpenTextFile` isn't a `Boolean` - it's a `Tristate`, none of which is `-1` (which is what you're passing it after the implicit cast). It should be 0 if you need ASCII. [See the documentation here](https://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx). – Comintern Oct 29 '16 at 01:00
  • Changing the last value to a 0 still produces the foriegn characters. I am editing my post to show values being passed, that print appropriately with Debug.Print – PinkSmelly BlueSocks Oct 29 '16 at 01:08
  • Can't replicate. Are you *starting* with an empty file? Keep in mind you're opening it in append mode. – Comintern Oct 29 '16 at 01:14
  • I have created a blank database and copied/pasted the code in, as well as created a blank file to append the data to and still get this behaviour – PinkSmelly BlueSocks Oct 29 '16 at 01:16
  • Can you post the calling code? – Comintern Oct 29 '16 at 01:18
  • Still can't replicate. Can you post the `RecursiveFileSearch` code? – Comintern Oct 29 '16 at 01:35
  • f is not declared, so try doing so (String) and use: `WriteData f, tdf.Name, tdf.Connect`. – Gustav Oct 29 '16 at 08:01
  • any chance you found a solution or reason why this is happening? I just wrote to a file and it came out as CHINESE and I am at a loss as to why! – CoSpringsGuy Apr 24 '18 at 16:43

0 Answers0