PC Review


Reply
Thread Tools Rate Thread

Delimited Export Specification - independent of table

 
 
Tokyo Alex
Guest
Posts: n/a
 
      15th Apr 2010
Dear all,

Is it possible in Access 2007 to create a delimited-data export
specification that is independent of the source table structure?

For example, if I want to export to a text file with a delimiter | and a
text qualifier of # (instead of the default , and "), I can create a spec to
allow automation of the task with TransferText.

However, if the table I use to create the spec has 7 fields and I try to
apply the spec to a different table with 8 fields I get a "The Microsoft
Office Access database engine could not find the object 'Filename#csv'. Make
sure the object exists and that you spell its name and the path name
correctly," error. (Aside: Least helpful Access error message *ever*)

Is there anyway to bypass this so that I can use code to cycle through an
array of export tables without having to specify a different spec for each
table?

Thanks,
Alex.
 
Reply With Quote
 
 
 
 
Roger Carlson
Guest
Posts: n/a
 
      15th Apr 2010
Well, here's a little routine that will export a table or query to a a text
file, with whatever delimiter, text qualifier, and with or without field
names:

'-----------------------------
Sub ExportTextFileDelimited(FileName As String, _
DataSet As String, _
Delimiter As String, _
TextQualifier As String, _
WithFieldNames As Boolean)
On Error GoTo ExportTextFile_Err

Dim cnn As ADODB.Connection
Dim rst As New ADODB.Recordset
Dim Directory As String
Dim MyString As String, strSQL As String
Dim strDS As String
Dim I As Integer

Open FileName For Output As #1
Set cnn = CurrentProject.Connection

rst.Open DataSet, cnn, adOpenForwardOnly, adLockReadOnly
If WithFieldNames Then
For I = 0 To rst.Fields.Count - 1
MyString = MyString & rst(I).Name & Delimiter
Next I
MyString = Left(MyString, Len(MyString) - 1)
Print #1, MyString
End If
rst.MoveFirst
Do While Not rst.EOF
MyString = ""
For I = 0 To rst.Fields.Count - 1
'check for text datatype (202)
If rst(I).Type = 202 Then
MyString = MyString & TextQualifier & _
rst(I) & TextQualifier & Delimiter
Else
MyString = MyString & rst(I) & "|"
End If
Next I
MyString = Left(MyString, Len(MyString) - 1)
Print #1, MyString
rst.MoveNext
Loop

ExportTextFile_Exit:
' Close text file.
Close #1
rst.Close
Set cnn = Nothing
Exit Sub
ExportTextFile_Err:
MsgBox Err.Description
Resume ExportTextFile_Exit
End Sub
'-----------------------------

Call it like this:

Call ExportTextFileDelimited2("C:\Table1.txt", "Table1", "|", "#", true)

Of course, you could put it in a loop and replace the table (or query) and
filename each time.


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L




"Tokyo Alex" <(E-Mail Removed)> wrote in message
newsD370AF4-660F-4B69-95CB-(E-Mail Removed)...
> Dear all,
>
> Is it possible in Access 2007 to create a delimited-data export
> specification that is independent of the source table structure?
>
> For example, if I want to export to a text file with a delimiter | and a
> text qualifier of # (instead of the default , and "), I can create a spec
> to
> allow automation of the task with TransferText.
>
> However, if the table I use to create the spec has 7 fields and I try to
> apply the spec to a different table with 8 fields I get a "The Microsoft
> Office Access database engine could not find the object 'Filename#csv'.
> Make
> sure the object exists and that you spell its name and the path name
> correctly," error. (Aside: Least helpful Access error message *ever*)
>
> Is there anyway to bypass this so that I can use code to cycle through an
> array of export tables without having to specify a different spec for each
> table?
>
> Thanks,
> Alex.



 
Reply With Quote
 
Roger Carlson
Guest
Posts: n/a
 
      15th Apr 2010
Whups. Couple of corrections in the main loop:

Do While Not rst.EOF
MyString = ""
For I = 0 To rst.Fields.Count - 1
'check for text datatype (202)
If rst(I).Type = 202 Then
MyString = MyString & TextQualifier & _
rst(I) & TextQualifier & Delimiter
Else
MyString = MyString & rst(I) & Delimiter '<----
End If
Next I
MyString = Left(MyString, Len(MyString) - 2) '<---
Print #1, MyString
rst.MoveNext
Loop


--
--Roger Carlson
MS Access MVP
www.rogersaccesslibrary.com
http://rogersaccessblog.blogspot.com/


"Roger Carlson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Well, here's a little routine that will export a table or query to a a
> text file, with whatever delimiter, text qualifier, and with or without
> field names:
>
> '-----------------------------
> Sub ExportTextFileDelimited(FileName As String, _
> DataSet As String, _
> Delimiter As String, _
> TextQualifier As String, _
> WithFieldNames As Boolean)
> On Error GoTo ExportTextFile_Err
>
> Dim cnn As ADODB.Connection
> Dim rst As New ADODB.Recordset
> Dim Directory As String
> Dim MyString As String, strSQL As String
> Dim strDS As String
> Dim I As Integer
>
> Open FileName For Output As #1
> Set cnn = CurrentProject.Connection
>
> rst.Open DataSet, cnn, adOpenForwardOnly, adLockReadOnly
> If WithFieldNames Then
> For I = 0 To rst.Fields.Count - 1
> MyString = MyString & rst(I).Name & Delimiter
> Next I
> MyString = Left(MyString, Len(MyString) - 1)
> Print #1, MyString
> End If
> rst.MoveFirst
> Do While Not rst.EOF
> MyString = ""
> For I = 0 To rst.Fields.Count - 1
> 'check for text datatype (202)
> If rst(I).Type = 202 Then
> MyString = MyString & TextQualifier & _
> rst(I) & TextQualifier & Delimiter
> Else
> MyString = MyString & rst(I) & "|"
> End If
> Next I
> MyString = Left(MyString, Len(MyString) - 1)
> Print #1, MyString
> rst.MoveNext
> Loop
>
> ExportTextFile_Exit:
> ' Close text file.
> Close #1
> rst.Close
> Set cnn = Nothing
> Exit Sub
> ExportTextFile_Err:
> MsgBox Err.Description
> Resume ExportTextFile_Exit
> End Sub
> '-----------------------------
>
> Call it like this:
>
> Call ExportTextFileDelimited2("C:\Table1.txt", "Table1", "|", "#", true)
>
> Of course, you could put it in a loop and replace the table (or query) and
> filename each time.
>
>
> --
> --Roger Carlson
> MS Access MVP
> Access Database Samples: www.rogersaccesslibrary.com
> Want answers to your Access questions in your Email?
> Free subscription:
> http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L
>
>
>
>
> "Tokyo Alex" <(E-Mail Removed)> wrote in message
> newsD370AF4-660F-4B69-95CB-(E-Mail Removed)...
>> Dear all,
>>
>> Is it possible in Access 2007 to create a delimited-data export
>> specification that is independent of the source table structure?
>>
>> For example, if I want to export to a text file with a delimiter | and a
>> text qualifier of # (instead of the default , and "), I can create a spec
>> to
>> allow automation of the task with TransferText.
>>
>> However, if the table I use to create the spec has 7 fields and I try to
>> apply the spec to a different table with 8 fields I get a "The Microsoft
>> Office Access database engine could not find the object 'Filename#csv'.
>> Make
>> sure the object exists and that you spell its name and the path name
>> correctly," error. (Aside: Least helpful Access error message *ever*)
>>
>> Is there anyway to bypass this so that I can use code to cycle through an
>> array of export tables without having to specify a different spec for
>> each
>> table?
>>
>> Thanks,
>> Alex.

>
>



 
Reply With Quote
 
Tokyo Alex
Guest
Posts: n/a
 
      16th Apr 2010
Thanks very much for the response.

I was hoping to be able to use TransferText, but it looks like I can't, and
your code will be very helpful.

Thanks again,
Alex.


"Roger Carlson" wrote:

> Whups. Couple of corrections in the main loop:
>
> Do While Not rst.EOF
> MyString = ""
> For I = 0 To rst.Fields.Count - 1
> 'check for text datatype (202)
> If rst(I).Type = 202 Then
> MyString = MyString & TextQualifier & _
> rst(I) & TextQualifier & Delimiter
> Else
> MyString = MyString & rst(I) & Delimiter '<----
> End If
> Next I
> MyString = Left(MyString, Len(MyString) - 2) '<---
> Print #1, MyString
> rst.MoveNext
> Loop
>
>
> --
> --Roger Carlson
> MS Access MVP
> www.rogersaccesslibrary.com
> http://rogersaccessblog.blogspot.com/
>
>
> "Roger Carlson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Well, here's a little routine that will export a table or query to a a
> > text file, with whatever delimiter, text qualifier, and with or without
> > field names:
> >
> > '-----------------------------
> > Sub ExportTextFileDelimited(FileName As String, _
> > DataSet As String, _
> > Delimiter As String, _
> > TextQualifier As String, _
> > WithFieldNames As Boolean)
> > On Error GoTo ExportTextFile_Err
> >
> > Dim cnn As ADODB.Connection
> > Dim rst As New ADODB.Recordset
> > Dim Directory As String
> > Dim MyString As String, strSQL As String
> > Dim strDS As String
> > Dim I As Integer
> >
> > Open FileName For Output As #1
> > Set cnn = CurrentProject.Connection
> >
> > rst.Open DataSet, cnn, adOpenForwardOnly, adLockReadOnly
> > If WithFieldNames Then
> > For I = 0 To rst.Fields.Count - 1
> > MyString = MyString & rst(I).Name & Delimiter
> > Next I
> > MyString = Left(MyString, Len(MyString) - 1)
> > Print #1, MyString
> > End If
> > rst.MoveFirst
> > Do While Not rst.EOF
> > MyString = ""
> > For I = 0 To rst.Fields.Count - 1
> > 'check for text datatype (202)
> > If rst(I).Type = 202 Then
> > MyString = MyString & TextQualifier & _
> > rst(I) & TextQualifier & Delimiter
> > Else
> > MyString = MyString & rst(I) & "|"
> > End If
> > Next I
> > MyString = Left(MyString, Len(MyString) - 1)
> > Print #1, MyString
> > rst.MoveNext
> > Loop
> >
> > ExportTextFile_Exit:
> > ' Close text file.
> > Close #1
> > rst.Close
> > Set cnn = Nothing
> > Exit Sub
> > ExportTextFile_Err:
> > MsgBox Err.Description
> > Resume ExportTextFile_Exit
> > End Sub
> > '-----------------------------
> >
> > Call it like this:
> >
> > Call ExportTextFileDelimited2("C:\Table1.txt", "Table1", "|", "#", true)
> >
> > Of course, you could put it in a loop and replace the table (or query) and
> > filename each time.
> >
> >
> > --
> > --Roger Carlson
> > MS Access MVP
> > Access Database Samples: www.rogersaccesslibrary.com
> > Want answers to your Access questions in your Email?
> > Free subscription:
> > http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L
> >
> >
> >
> >
> > "Tokyo Alex" <(E-Mail Removed)> wrote in message
> > newsD370AF4-660F-4B69-95CB-(E-Mail Removed)...
> >> Dear all,
> >>
> >> Is it possible in Access 2007 to create a delimited-data export
> >> specification that is independent of the source table structure?
> >>
> >> For example, if I want to export to a text file with a delimiter | and a
> >> text qualifier of # (instead of the default , and "), I can create a spec
> >> to
> >> allow automation of the task with TransferText.
> >>
> >> However, if the table I use to create the spec has 7 fields and I try to
> >> apply the spec to a different table with 8 fields I get a "The Microsoft
> >> Office Access database engine could not find the object 'Filename#csv'.
> >> Make
> >> sure the object exists and that you spell its name and the path name
> >> correctly," error. (Aside: Least helpful Access error message *ever*)
> >>
> >> Is there anyway to bypass this so that I can use code to cycle through an
> >> array of export tables without having to specify a different spec for
> >> each
> >> table?
> >>
> >> Thanks,
> >> Alex.

> >
> >

>
>
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
export a table as tab delimited from code? SirPoonga Microsoft Access 1 28th Feb 2005 08:27 PM
Re: Unable to export a table to a delimited or fixed-width text file Douglas J. Steele Microsoft Access External Data 0 19th Mar 2004 06:46 PM
Export a table to a text file pipes delimited Sally Microsoft Access Getting Started 1 9th Feb 2004 01:48 PM
Export table in comma delimited format - how to eliminate empty spaces Peter Afonin Microsoft Access ADP SQL Server 1 2nd Feb 2004 12:38 AM
Export/Import Table Specification Robin Chapple Microsoft Access Getting Started 13 26th Oct 2003 09:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:11 PM.