Re: export a field from a table to make a text file

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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" <Tim@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:0BDB6A14-9E2A-48CD-9F43-5AD31C244908@xxxxxxxxxxxxxxxx
>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



.



Relevant Pages

  • Re: write input values from users to a text file
    ... can do it with something like this, using the WriteToFile() procedure at ... 'Overwrite argument controls what happens ... Dim lngFN As Long ... 'Change Output in next line to Append to ...
    (microsoft.public.access.modulesdaovba)
  • Re: export a field from a table to make a text file
    ... >> Dim strFileSpec As String ... >> 'Overwrite argument controls what happens ... >> Dim lngFN As Long ...
    (microsoft.public.access.externaldata)
  • Re: export a field from a table to make a text file
    ... the contents of a textbox on a form. ... "John Nurick" wrote: ... > 'Overwrite argument controls what happens ... > Dim lngFN As Long ...
    (microsoft.public.access.externaldata)
  • Re: Exporting to email
    ... 'Writes Var to a textfile as a string. ... 'Overwrite argument controls what happens ... Dim lngFN As Long ...
    (microsoft.public.access.externaldata)