Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Importing / Linking / August 2004

Tip: Looking for answers? Try searching our database.

Heading line in comma separated txt file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mikael Branting - 24 Aug 2004 01:21 GMT
I want to export data as a comma separated txt file. The required output
includes a heading with with a fixed content.

Which is the easiest way to append the heading at the top of the lines of
the database ouput?
Ken Snell [MVP] - 24 Aug 2004 01:55 GMT
Use TransferText method. It includes an argument that allows you include
header or not.

Signature

       Ken Snell
<MS ACCESS MVP>

> I want to export data as a comma separated txt file. The required output
> includes a heading with with a fixed content.
>
> Which is the easiest way to append the heading at the top of the lines of
> the database ouput?
John Nurick - 24 Aug 2004 07:36 GMT
Hi Mikael,

If the heading needs to contain something other than a list of field
names, the simplest way is usually to write the header to a temporary
file, export the data to another, and then concatenate the two.

The function at the end of this message will write a string to a file,
and you can use something like this air code:

 Dim strFolder As String
 Dim strHdrFile As String
 Dim strTempFile As String
 Dim strTextFile As String
 Dim strHeader As String
 Dim lngRetVal As Long

 strFolder = "D:\Folder\"
 strHdrFile = "HDRTEMP.$$$"
 strTempFile = "TXTTEMP.$$$"
 strTextFile = "My Export.txt"

 'write header
 strHeader = "This is the Header"
 lngRetVal = WriteToFile(strHeader, _
     strFolder & strHdrFile)

 'export text
 DoCmd.TransferText blah blah, _
     strFolder & strTempFile, blah

 'concatenate
 Shell "COPY """ & strFolder & strHdrFile & """ + """ _
     & strFolder & strTempFile & """ """ _
     & strFolder & strTextFile & """"

 'delete temp files
 Kill strFolder & strHdrFile
 Kill strFolder & strTempFile
     

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

>I want to export data as a comma separated txt file. The required output
>includes a heading with with a fixed content.
>
>Which is the easiest way to append the heading at the top of the lines of
>the database ouput?

--
John Nurick [Microsoft Access MVP]

Please respond in the newgroup and not by email.
Mikael Branting - 27 Aug 2004 01:21 GMT
Thank you for a very useful answer. My geading is not the same as a field
name list,so your apporach was exactly what I was looking for.

> Hi Mikael,
>
[quoted text clipped - 85 lines]
>
> Please respond in the newgroup and not by email.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.