Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

Advice: Exporting Query Results (VB/SQL)

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
setfocus
Asked:
setfocus
  • 2
1 Solution
 
chandukbCommented:
Try Recordset's SAVE method, see msdn for syntax.

Chandu
0
 
setfocusAuthor Commented:
can I use semi colon delimitted fields??  I need this to be the case.

if it was that simple...it would be great!
0
 
chandukbCommented:
0
 
andyclapCommented:
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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now