Hi Tim,
If you want to create a textfile containing the contents of a textbox on a
form, you can use the function I've pasted at the end of this message. You
can call it with something like this:
Dim lngRetVal As Long
Dim strFileSpec As String
strFileSpec = "C:\Folder\Filename.txt"
lngRetVal = WriteToFile(Me.XXX.Value, strFileSpec)
If lngRetVal <> 0 Then
MsgBox "Error " & lngRetVal & " creating " & strFileSpec & ".", _
vbExclamation + vbOkOnly
End If
Tim said:
I have a table in Access database. There is a field in this table that I
would like to export it and save as a text file. How can I do that?
Thanks!
Function WriteToFile(Var As Variant, _
FileSpec As String, _
Optional Overwrite As Long = True) _
As Long
'Writes Var to a textfile as a string.
'Returns 0 if successful, an errorcode if not.
'Overwrite argument controls what happens
'if the target file already exists:
' -1 or True (default): overwrite it.
' 0 or False: append to it
' Any other value: abort.
Dim lngFN As Long
On Error GoTo Err_WriteToFile
lngFN = FreeFile()
'Change Output in next line to Append to
'append to existing file instead of overwriting
Select Case Overwrite
Case True
Open FileSpec For Output As #lngFN
Case False
Open FileSpec For Append As #lngFN
Case Else
If Len(Dir(FileSpec)) > 0 Then
Err.Raise 58 'File already exists
Else
Open FileSpec For Output As #lngFN
End If
End Select
Print #lngFN, CStr(Nz(Var, ""));
Close #lngFN
WriteToFile = 0
Exit Function
Err_WriteToFile:
WriteToFile = Err.Number
End Function