Please forgive me if I’m doing something wrong as this is my first posting, I am completely new to using forums for advice.
I am using the following code to export the result of a query to an excel sheet and would like to pass a parameter to the query “Query2”. If the query contains the parameter everything works fine but if iI try to get it to read the parameter from a textbox on a form I get the message:
Too few parameters. Expected 1.
This is the line that seem to be the problem:
Set rs = db.OpenRecordset("Query2", dbOpenSnapshot)
I got the original code (shown below) from a thread on this forum for which I’m truly grateful
Dim db As DAO.Database
'Set db = DAO.DBEngine.Workspaces(0).OpenDatabase( _
"C:\database.mdb")
Set db = CurrentDb
Set rs = db.OpenRecordset("Query2", dbOpenSnapshot)
Dim mysheet As Object, xlApp As Object
' Set object variable equal to the OLE object.
Set xlApp = CreateObject("Excel.Application")
Set mysheet = xlApp.Workbooks.Open("c:\Access2000\Samples\Names.xls").Sheets(2)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
mysheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next
'Add the data starting at cell A2
mysheet.Range("A2").CopyFromRecordset rs
'Format the header row as bold and autofit the columns
With mysheet.Range("a1").Resize(1, iNumCols)
.Font.Bold = True
.EntireColumn.AutoFit
End With
xlApp.Visible = True
xlApp.UserControl = True
'Close the Database and Recordset
rs.Close
db.Close
End Sub
I am using the following code to export the result of a query to an excel sheet and would like to pass a parameter to the query “Query2”. If the query contains the parameter everything works fine but if iI try to get it to read the parameter from a textbox on a form I get the message:
Too few parameters. Expected 1.
This is the line that seem to be the problem:
Set rs = db.OpenRecordset("Query2", dbOpenSnapshot)
I got the original code (shown below) from a thread on this forum for which I’m truly grateful
Dim db As DAO.Database
'Set db = DAO.DBEngine.Workspaces(0).OpenDatabase( _
"C:\database.mdb")
Set db = CurrentDb
Set rs = db.OpenRecordset("Query2", dbOpenSnapshot)
Dim mysheet As Object, xlApp As Object
' Set object variable equal to the OLE object.
Set xlApp = CreateObject("Excel.Application")
Set mysheet = xlApp.Workbooks.Open("c:\Access2000\Samples\Names.xls").Sheets(2)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
mysheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next
'Add the data starting at cell A2
mysheet.Range("A2").CopyFromRecordset rs
'Format the header row as bold and autofit the columns
With mysheet.Range("a1").Resize(1, iNumCols)
.Font.Bold = True
.EntireColumn.AutoFit
End With
xlApp.Visible = True
xlApp.UserControl = True
'Close the Database and Recordset
rs.Close
db.Close
End Sub