VBA to Export recordset

tuktuk

Well-known Member
Joined
Nov 13, 2006
Messages
856
hey there,

here is my code that i use to export to excel.

i know that it steps through each cell to paste the header values and then pastes the entire recordset for the dataset.

what is the best way to alter the code to ensure it does not break if the recorset is null?

thanks
tuk

Code:
Set rst = dbs.OpenRecordset(strExport, dbOpenDynaset, dbReadOnly)

If rst.EOF = False And rst.BOF = False Then

      rst.MoveFirst

      If blnHeaderRow = True Then
            For lngColumn = 0 To rst.Fields.Count - 1
                  xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Name
            Next lngColumn
            Set xlc = xlc.Offset(1, 0)
      End If

xlc.CopyFromRecordset rst

End If
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I use this code with a database when exporting into Excel for this specific project it is using ADO and works fine.

Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim ws As Excel.Application
Dim strSQL As String
Dim i As Long

strSQL = "SELECT * FROM qryNametoExport"
Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open strSQL, cnn, adOpenKeyset, adLockOptimistic, adCmdTableDirect
rst.MoveFirst
MsgBox rst.RecordCount

Set ws = CreateObject("Excel.Application")
ws.Workbooks.Add
ws.Columns("A:A").Select
ws.Selection.NumberFormat = "0"
ws.Selection.ColumnWidth = 18.14
ws.Columns("A:A").EntireColumn.AutoFit
For i = 0 To rst.Fields.Count - 1
ws.ActiveCell.Offset(0, i).Value = rst.Fields(i).Name
Next
ws.Range("a2").Select
Do Until rst.EOF
For i = 0 To rst.Fields.Count - 1
ws.ActiveCell.Offset(0, i).Value = rst.Fields(i).Value
Next
ws.ActiveCell.Offset(1, 0).Select
rst.MoveNext
Loop
rst.Close
ws.Columns("A:v").EntireColumn.AutoFit
ws.Visible = True
 
Upvote 0
put
Code:
if rst is not nothing 
    If rst.EOF = False And rst.BOF = False Then
    end if
end if
or it may be
if not rst is nothing
I forget
 
Upvote 0

Forum statistics

Threads
1,215,156
Messages
6,123,338
Members
449,098
Latest member
thnirmitha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top