Search code examples
ms-accessvbams-access-2013

Error Writing Data To Text File


I am using access to write data to a text file, but I get this error

invalid procedure call or argument

What do I need to alter in this procedure so that data can be succesfully written? I have access to the path, and the file already exists and is spelled properly in my code.

 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)
  Fileout.Write fulllocale & "," & tblName & "," & CString & vbCrLf
  Fileout.Close
End Function

Solution

  • ForAppending isn't declared, and since you've apparently late bound your Scripting.FileSystemObject, it's getting initialized to a local Variant with it's default value.

    You can prevent this in the future by putting Option Explicit at the top of all of your modules.

    Either add the reference the Microsoft Scripting Runtime (there is absolutely no reason not to), or use the integer value of the IOMode. In this case, ForAppending, which is 8:

    Set Fileout = fso.OpenTextFile("C:\Test.txt", 8, True)