Export query results to text file 
Author Message
 Export query results to text file

Is there are way to export the results from a query to a
comma delimited text file?  I would like to create this
type of text file that could then be used by MS Word for
mail merge purposes.  I don't want to automate MS Word
through my app.  I want users to be able to go later and
create a mail merge or labels or whatever with the text
file.  Seems pretty basic, but I can't figure it out.  I
am working off of an mdb database, not SQL Server.  Thank
you for any help.


Tue, 02 Mar 2004 02:39:33 GMT  
 Export query results to text file
I'll assume also that you don't want to automate Access (Access has a
particularly handy macro called TransferText that does this quite well.)
But because you posted this in a VB/ADO newsgroup, I'll give you the ADO
code.

Here's but one approach:

'--Begin Sample Code
  Dim oCn As New ADODB.Connection
  Dim oRs As New ADODB.Recordset

  oCn.Open "provider=sqloledb;data source=(local);user id=me;initial
catalog=northwind;"

  oRs.Open "SELECT * FROM CATEGORIES", oCn

  Open "c:\categories.txt" For Output As #1   ' Open file for input.

  Do Until oRs.EOF
    Write #1, oRs("CategoryID").Value, oRs("CategoryName").Value  '<more
can be added, separated by commas
    oRs.MoveNext
  Loop

  oRs.Close
  oCn.Close

  Set oRs = Nothing
  Set oCn = Nothing
'--End Sample Code

Hope this helps!

Steven Bras, MCSD
Microsoft Developer Support/Visual Basic WebData
This posting is provided AS IS with no warranties, and confers no rights.
You assume all risk for your use. ? 2001 Microsoft Corporation. All rights
reserved.



Tue, 02 Mar 2004 04:49:38 GMT  
 Export query results to text file
Hello,

You can use the getString method of the ADO recordset.

Private Sub example(sSQL As String, sConn As String, sFile As String)
Dim oRS As ADODB.Recordset
Dim oFSO As Scripting.FileSystemObject
Dim oTSoutput As Scripting.TextStream
Dim sTemp As String

    Set oRS = New ADODB.Recordset
    oRS.Open sSQL, sConn, adOpenForwardOnly, adLockReadOnly
    sTemp = oRS.GetString(adClipString, -1, ",", vbCrLf, "")
    oRS.Close
    Set oRS = Nothing

    Set oFSO = New Scripting.FileSystemObject
    Set oTSoutput = oFSO.OpenTextFile(sFile, ForWriting, True,
TristateFalse)
    oTSoutput.Write sTemp
    oTSoutput.Close
    Set oTSoutput = Nothing
    Set oFSO = Nothing
End Sub


Quote:
> Is there are way to export the results from a query to a
> comma delimited text file?  I would like to create this
> type of text file that could then be used by MS Word for
> mail merge purposes.  I don't want to automate MS Word
> through my app.  I want users to be able to go later and
> create a mail merge or labels or whatever with the text
> file.  Seems pretty basic, but I can't figure it out.  I
> am working off of an mdb database, not SQL Server.  Thank
> you for any help.



Tue, 02 Mar 2004 06:41:20 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. export a report result into a text file**

2. Use parameter Query to export to multiple text files

3. Exporting Acces2K table (or query) to separate text files

4. Export Access97 query to a text file

5. Exporting queries results from Access to Excel

6. exporting the result of one query in VB5

7. Export Database Query Results to Excel?

8. Exporting report results to rich-text format

9. Run Query based on Listbox results - text parsing?

10. Populating a text box with the results of a select query

11. How to write an asp script storing the result from a query in a CSV text

12. How to write an asp script storing the result from a query in a CSV text

 

 
Powered by phpBB® Forum Software