?
Solved

Exporting Query into tab delimited text format

Posted on 2006-04-20
12
Medium Priority
?
246 Views
Last Modified: 2010-05-01
Hello all,

I've been reading code for about a half an hour and understand very little.  Still new at vb.  I need to export a select query as a tab delimited txt file and store it in a specifice folder and I need this file to erase and replace any existing file in that folder.  Is this possible.  Any information would be helpful.  And remember I am new to this, so assume I know nothing, and walk me through it.  Thank you in advance.
0
Comment
Question by:JPDuval
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 4

Accepted Solution

by:
g_johnson earned 500 total points
ID: 16501013
here's a walk through, but you'll have more research to do:

1.  Is this a one time thing?  If so, use DTS in SQL Enterprise Manager
2.  If it must be written in code:
     a.  run a select query, e.g., rs1.open "SELECT * FROM yourtable"
     b.  build your tab-delimited string, e.g.
             for i = 0 to rs1.fields.count - 1
                  strNew = strNew & rs1.fields(i).value & chr(9)
             next 'i
     c.  you can simply open the file for output to write to it and over-write anything that used to be in it
              open "c:\path\filename" for output as #1
              print #1, strNew
              close #1

That's the basics!  
0
 
LVL 12

Expert Comment

by:jkaios
ID: 16503251
I assume that you've already declared your variables and set the connection to the database as follows:

  Dim your_ADO_Conn As New ADODB.Connection
  Dim rs1 As New ADODB.Recordset
  Dim strNew As String

Step1: Open/run the select query.
--------
  rs1.Open "SELECT * FROM yourtable", your_ADODB_Conn

Step2: Create and open a new text file to export your data into.
--------
  Open "C:\temp\mydata.txt" For Output As #1

Step3: Loop thru the recordset and output all field values.
--------
  Do While Not rs1.EOF    
    For i = 0 To rs1.Fields.Count -1
      strNew = strNew & rs1.Fields(i).Value & vbTab
    Next
    Print #1, strNew      'output the current record to text file
    strNew = ""             'clear variable before the next record
    rs1.MoveNext           'go to the next record
  Loop
0
 
LVL 12

Expert Comment

by:jkaios
ID: 16503286
>> I need to export a select query as a tab delimited txt file and store it in a specifice folder and I need this file to erase and replace any existing file in that folder.  Is this possible. <<

When you use the "Output" keyword on the "Open" statement, any existing file will be replaced automatically with the new file.

With your "strNew" variable, better declare it as of type Variant to be flexible to hold any type of data.

I use  the "vbTab" built-in constant which is equivalent to Chr(9) to make your code more self-documented.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

by:daniel_j_c
ID: 16505984
@jkaios - wouldn't recommend using variants - the file is going to get outputted as a string anyway and variant's are incredibly slow.

also, try this for the inner loop:

dim fld   as field
dim sTemp1 as string
dim sTemp2 as string

  Do While Not rs1.EOF    
    For each fld  rs1.fields
      sTemp1 = sTemp1 & fld.value & vbtab
    next fld

    if len(sTemp1) > 2000 then
      sTemp2 = sTemp2 & sTemp1
      sTemp1 = ""
   
      if len(sTemp2)>64000 then
        print #1, sTemp2
       sTemp2 = "'
      end if
    end if

    rs1.movenext
  loop

print #1, sTemp2 & sTemp1

there are 3 optimisations here - if you are dealing with a large numbers of records, you'll notice a massive performance increase:

a) using for each fld in... means that the fld object has already been resolved and an additional object lookup does not have to be done for each field

b) vb's string handling is appaling so when a string gets over 2000 chars or so it's much more efficient to pack those 2000 chars onto another sttring and start the first one again

c) dumping a small amount of data (ie. 1 record) to a file at a time has a big overhead - the most efficient size is between 32K and 64K of data at a time.

0
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 500 total points
ID: 16506493
You can get your database to do more than just return rows and columns:

rs1.Open "SELECT col1 & chr(9) & col2 & chr(9) & col3 & chr(9) & col4 & chr(9) & col5 As TabDelimitedRow FROM yourtable", your_ADODB_Conn

Note: the chr(9) is the tab character which is being concatenated with the column data.

Your VB code simplifies into
Open "C:\temp\mydata.txt" For Output As #1
Do While Not rs1.EOF    
    Print #1, strNew      'output the current record to text file
    rs1.MoveNext           'go to the next record
Loop

0
 
LVL 2

Expert Comment

by:daniel_j_c
ID: 16506633
good one aikimark - just remember if any of the columns could be null, you need to do this (and in sqlserver its char not chr):

rs1.Open "SELECT isnull(col1,'') & char(9) & isnull(col2,'') & char(9) & isnull(col3,'') & char(9) & ...
0
 
LVL 46

Expert Comment

by:aikimark
ID: 16507039
I assumed this was an MSAccess database.  As such, the & operator takes care of handling Null value concatenations.  I don't see a need to use the IsNull() function.  Furthermore, the IsNull() function in VB and MSAccess SQL environments only has one parameter.  You might be thinking of the NZ() function.

If SQLServer, then we need to use the + character to concatenate strings.
0
 
LVL 2

Expert Comment

by:daniel_j_c
ID: 16507184
ok - thought this was sql server. in sql if 1 element of an expression is null, the whole expression evaluates to, hence the use of isnull(). isnull in vb has 1 param but sql server has two - 'value' and 'new value'.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 16507287
The comments about string concatenation speed are true.  If you must do a lot of string concatenations (which you don't in this instance), I'd recommend something like VB.Net's StringBuilder class.  There are several fast concatenation routines available for free download from various sites.  I also wrote my own StringBuilder class to accomodate lots of concatenations, which I will post if anyone is interested in this side topic.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

621 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