Solved

Advice: Exporting Query Results (VB/SQL)

Posted on 2001-06-14
4
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 …
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 Month10 days, 14 hours left to enroll

631 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