Solved

Advice: Exporting Query Results (VB/SQL)

Posted on 2001-06-14
4
159 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now