Export an ADO Recordset as CSV or XML
In lieu of my typical witty banter I'm just going to post some code. Way too tired to put any effort forth. Tried for days to get Access to export my ADO Recordset as something, anything. I tired:
- Loading it into a table - nope
- Making a temporary table on the database server and linking to it - nope
- Tried using getRows and manually creating a CSV - nope
- Conjured up the ancient spirits of BASIC - nope
Dim ADOrs As ADODB.Recordset Set ADOrs = Me.RecordsetClone ADOrs.Save "export.xml", adPersistXML ADOrs.Close
Dim ADOrs As ADODB.Recordset
Set ADOrs = Me.RecordsetClone
Dim csv As String
csv = ADOrs.GetString(, , """,""", """" _
& vbCrLf & """", "")
csv = Left$(csv , Len(csv) - 1)
Dim h As String ' Header row
For X = 1 To ADOrs.fields.Count - 1
h = h & """" & ADOrs.fields(X).Name & """"
If X < ADOrs.fields.Count - 1 Then
h = h & ","
End If
Next X
Open "export.csv" For Output As #1
Print #1, h
Print #1, """", csv
Close 1