export a field from a table to make a text file

T

Tim

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!
 
D

Douglas J. Steele

You could create a query that returns just the field in question and use the
TransferText method to save it in a file, or you could use VBA File I/O (See
Open Statement, Print Statement and Close Statement in the Help file)
 
T

Tim

Hi Douglas,
Can you please tell me more about VBA File I/O. If you can, give me a code
for example. I check the MS website and see something like,
Open pathname For mode As [#]filenumber
but I don't understand it because I just want to export a field in an Access
table and save it as a txt file.
Thank you.
 
J

John Nurick

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
 
T

Tim

Hi John,
Thank you for your help. But I would like to export a field from a table not
the contents of a textbox on a form. Do you know how to do that?
Thank you,
Tim
 
T

Tim

I have an idea trying to solve this issue. How about writing some codes that
create a query to extract the field I want and then use transfertext command
to export to save to a file? Can you show me how to do that?
Thank you
 
J

John Nurick

If you know the field you want to export, just create and save a query
in the usual way, and then export it with DoCmd.TransferText.

Otherwise, please try and explain more clearly just what it is you are
trying to do.
 
T

Tim

I can't do it in the usual way because I have to create a program for the
user who just clicks on the button and have the file already on the disk. In
other word, I have to do it programmatically.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top