Solved

Export From Access Select Query to tab delimited Text file without quotation marks around fields

Posted on 2004-04-06
5
922 Views
Last Modified: 2013-12-25
Hello Experts,

I am using VB6 and Access 2000 with ADO.

I am queries to select data from an Access database and then create a tab delimited text file with this data.

It is working but I have no idea how to do this without the quotation marks around each field, other than using the getString method with recordsets which I would prefer not to do as I have a huge amount of records to write to these files and I also need to be able to update the textfile based on queries.

This is the code I am currently using,

strSQL = "SELECT cKH_NR AS KH_NR, cKalk_Zeitraum AS Kalc_Zeitraum, cEpiNum AS Fall_Nr " & _
" INTO [Text;Database=C:\Clino\;" & _
" HDR=YES;FMT=TabDelimited].[TEST.txt] " & _
" FROM " & gstrDataPath & "\clinotelexport.Clinotelexport  WHERE cKStGr = 'OP' "

db.execute(strSQL)

which is producing
"KH_NR"      "Kalc_Zeitraum"      "Fall_Nr"
"260590402"      "2002"      "1000070007"
"260590402"      "2002"      "1000070007"

what I need is
KH_NR      Kalc_Zeitraum      Fall_Nr
260590402                      2002      1000070007
260590402                      2002      1000070007

any help would really be appreciated

Cheers
0
Comment
Question by:Joym123
5 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 125 total points
ID: 10771912
Export to a text file with the GetString method
The GetString method of the Recordset object returns a formatted string that contains all the values in each field and each row in the recordset, with your choice of column and row delimited. You can therefore use it to quickly and easily export the recordset to a comma-delimited text file, for later importing it into another database:

Dim rs As New ADODB.Recordset

' open the recordset (use a valid connection string for your system)
rs.Open "authors", "DSN=pubs", , , adCmdTable
Open "c:\authors.dat" For Output As #1
' the last argument tells how Null should be rendered
Print #1, rs.GetString(, , ",", vbCrLf, "");
Close #1

The problem with this simplicistic approach, however, is that very large Recordset - with thousands of rows or more - generate very large strings, which impact on performance. A better way to create the file is to split the Recordset in smaller blocks of, say, one hundred rows:

Dim rs As New ADODB.Recordset

' open the recordset (use a valid connection string for your system)
rs.Open "authors", "DSN=pubs", , , adCmdTable
Open "c:\authors.dat" For Output As #1
Do Until rs.EOF
    Print #1, rs.GetString(, 100, ",", vbCrLf, "");
Loop
Close #1

Note that you can export to a tab-delimited text file simply by modifying the 3rd argument of the GetString method:

    Print #1, rs.GetString(, 100, vbTab, vbCrLf, "");

0
 

Author Comment

by:Joym123
ID: 10771946
Thanks for the reply ,

But as I stated in my original post, I am looking for a way of doing this directly to a text file not through recordsets using GetString.  I have alot (upwards of 800 000 ) records and therefore performance is an issue and I am trying to find the most direct efficient way.


0
 
LVL 18

Accepted Solution

by:
Sethi earned 125 total points
ID: 10771989
I have been working on a few projects lately that involve import and expoerts between Excel, Access and textfiles. This is what I have found:
1. You can not rely on any in built methods of Export from Access and they are not consistent with their output.
2. It is best to use a recordset and loop through each record and write or read that record in or from a text file.

So the best method that I discovered was looping through a recordset. This wont leave a quotation mark.
0
 

Author Comment

by:Joym123
ID: 10772122
Sethi,

Thanks for the input.  I will leave the question open until tomorrow and see if anyone has any other suggestions.  If not I will split the points between you and Dhaest.

It looks like I will have to do with getString anyway, as I am also using stored queries at some points and when I refer to these in the execute sql statement I don't even get a tab delimited output.  So I guess you are right about the output being inconsistent.

Cheers

joy
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10772365
Hi Joym123,

I agree with Sethi.
A loop like he proposes will probably take 5 lines of code, and the performance will not be much slower.
You have total control of what is happening, and if in the future you want any further changes, it will be easier to implement.

Dabas
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question