Export one record to text file

  • Thread starter Thread starter Jason Frazer
  • Start date Start date
J

Jason Frazer

I have a database with a form [Form_Bridge]. I would like
to export the current record to a text file. I want to be
promted for the location of the file. The text file
created will be a long single string. I have played
around with the transfertext command. How can i do this?

Thanks for your time and help

Jason Frazer
 
Have you tried docmd.outputTo?

--
HTH

Dale Fye


I have a database with a form [Form_Bridge]. I would like
to export the current record to a text file. I want to be
promted for the location of the file. The text file
created will be a long single string. I have played
around with the transfertext command. How can i do this?

Thanks for your time and help

Jason Frazer
 
Jason Frazer said:
I have a database with a form [Form_Bridge]. I would like
to export the current record to a text file. I want to be
promted for the location of the file. The text file
created will be a long single string. I have played
around with the transfertext command. How can i do this?

Thanks for your time and help

Jason Frazer


docmd.outputTo is the best and easiest way to go, as Dale mentioned.

If you want to have more control over the format of the output file you
could use
something like this:

Jack

Const TextDelimiter = vbTab ' using a tab as field delimiter , if required
Const ForWriting = 2, TristateUseDefault = -2

Dim fs As Object, a As Object, f As Object
Dim TheFileName As String, TheData As String

' now check every field on the form for data and store it in the string
If Not IsNull(Me.MyFirstField) Then
TheData = CStr(Me.MyFirstField)
End If

If Not IsNull(Me.MySecondField) Then
TheData = TheData & TextDelimiter & CStr(Me.MySecondField)
End If

If Not IsNull(Me.MyThirdField) Then
TheData = TheData & TextDelimiter & CStr(Me.MyThirdField)
End If

' do this for as many fields on the form that you want to save

' ok now you have all the fields stored in the string TheData


' prompt user for file name
TheFileName = InputBox("Enter File Name", "SAVE DATA TO FILE")

' create a file then open it for wriring
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile(TheFileName, True)
Set a = fs.GetFile(TheFileName)
Set f = a.OpenAsTextStream(ForWriting, TristateUseDefault)

' write the data to the file
f.Write TheData
f.Close
 
Back
Top