So far I have successfully exported my query data to a excel sheet by putting an arbitrary id number in the criteria of one of the columns of my query.
Now I am trying to pass that same id number from a form that I have open it is giving me a run-time error '3061': Too few parameters. Expedcted 1. at this line of my code:
Set rs = CurrentDb.OpenRecordset("qry_exportRS", dbOpenSnapshot)
Can anyone help?
This is the expression I am using in the criteria of my query [Forms]![frm_Contracts]![sfm_Contract_Details].[Form]![contract_detail_id]
and here is the complete code:
Private Sub Command9_Click()
'Send records to the first
'sheet in a new workbook
'
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim rs As Recordset
Dim intMaxCol As Integer
Dim intMaxRow As Integer
Dim oBookName As String
Set rs = CurrentDb.OpenRecordset("qry_exportRS", dbOpenSnapshot)
intMaxCol = rs.Fields.Count
If rs.RecordCount > 0 Then
rs.MoveLast: rs.MoveFirst
intMaxRow = rs.RecordCount
Set oExcel = CreateObject("Excel.Application")
With oExcel
.Visible = True
Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
With oSheet
.Range(.Cells(1, 1), .Cells(intMaxRow, _
intMaxCol)).CopyFromRecordset rs
End With
End With
End If
oBookName = oBook.Worksheets(1).Range("a1").Value
'Save the Workbook and Quit Excel
oBook.SaveAs "C:\Documents and Settings\cgibb\Desktop\itd_" & oBookName & ".xls"
oExcel.Quit
End Sub