
Exporting text w/o a specification
Hi Shawn,
Here is something I did a while ago.
Steve
Sub ExportDelim(strTable As String, strExportFile As String, strDeliminator As String, Optional blnHeader As Boolean)
'strTable is the table or query name
'strExportFile is the full path and name of file to export to
'strDelimiinator is the field deliminator to use like Chr(9) for Tab
Dim fld As Field
Dim varData As Variant
Dim rs As Recordset
Dim intFileNum As Integer
'set recordset on table or query
Set rs = CurrentDb.OpenRecordset(strTable, dbOpenSnapshot)
'get file handle and open for output
intFileNum = FreeFile()
Open strExportFile For Output As #intFileNum
If blnHeader Then
'output the header row if requested
varData = ""
For Each fld In rs.Fields 'traverse the fields collection
varData = varData & fld.Name & strDeliminator
Next
'remove extra last deliminator
varData = Left(varData, Len(varData) - 1)
'write out the header row
Print #intFileNum, varData
End If
'now your data
Do While Not rs.EOF
varData = ""
'concatenate the data row
For Each fld In rs.Fields
varData = varData & fld.Value & strDeliminator
Next
'remove extra last deliminator
varData = Left(varData, Len(varData) - 1)
'write out data row
Print #intFileNum, varData
rs.MoveNext
Loop
Close #intFileNum
Set rs = Nothing
End Sub
Quote:
> I have 7 different tables that I want to export with tabs as delimiters and
> O quotes around strings.
> I am using TransferText from VBA, but the default spec does not export as
> described above.
> How can I do this w/o having to build a specification for each table?
> --
> Thanks
> Shawn