Solved

Advice: Exporting Query Results (VB/SQL)

Posted on 2001-06-14
4
162 Views
Last Modified: 2012-05-04
I have a reporting tool that was created for users to generate criteria (sql statement).  For one reporting option the user needs to export the results of the sql statement they create(graphically) to be exported to a .txt file in a certain format - ; seperated.

Right now I am using a word merge and saving it as .txt...works fine...but its a little slower than I would like and there should be a more efficient way.

Can I do this at the SQL end....BCP? etc...
I have the sql string in a variable so I can use it however I want...

What would you do??
0
Comment
Question by:setfocus
  • 2
4 Comments
 
LVL 2

Expert Comment

by:chandukb
ID: 6192057
Try Recordset's SAVE method, see msdn for syntax.

Chandu
0
 

Author Comment

by:setfocus
ID: 6192105
can I use semi colon delimitted fields??  I need this to be the case.

if it was that simple...it would be great!
0
 
LVL 2

Expert Comment

by:chandukb
ID: 6192210
0
 
LVL 6

Accepted Solution

by:
andyclap earned 60 total points
ID: 6194683
Unfortunately the .save method only supports XML and a MS propriety format.
However, with a bit of simple code, you can very quickly save the recordset to a text file.

Chandu's link is good, although it's not the most efficient way of doing it (doing a field.value on each element can be a little slow). There's probably even more efficient ways too, but heres my 2c worth.

Note this sample uses implicit string conversion for the recordset's contents - it is probably a good idea to explicitly conver each element depending on the vartype so that you know the output formats.

Private Sub SaveRS(ByVal rs As Recordset, ByVal sFile As String)

    'save the recordset to the file semicolon delimited
    Dim vaRows As Variant
    Dim lRow As Long
    Dim lCol As Long
    Dim iFile As Integer
   
    vaRows = rs.GetRows()
   
    iFile = FreeFile
    Open sFile For Output As iFile
   
    For lRow = LBound(vaRows, 2) To UBound(vaRows, 2)
        For lCol = LBound(vaRows, 1) To UBound(vaRows, 1)
            If lCol <> 0 Then
                Print #iFile, ";";
            End If
            'this will implicitly convert to a string
            'so it might be best to write some explicit conversion code here
            Print #iFile, vaRows(lCol, lRow);
        Next
        If lRow <> UBound(vaRows, 2) Then
            Print #iFile,
        End If
    Next
    Close iFile
           
   
End Sub

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now