I have a form with 2 subforms.
There are various dropdowns on the the first subform, which set the recordsource for the form and can be changed continually.
The recordource is determined from some queries which have parameters like
Forms![TabsReport]!.[Units1].Form.combo_A
All the drop downs work correctly and data is changed accordingly so the queries must be picking up the parameters from the form correctly.
However, when I try to export this to Excel, I get a "too few parameters. Expected 1" error even though it is trying to use excatly the same recordsource.
Any ideas why ?
Private Sub cmd_Export_Click()
Dim iCount As Integer
Dim strCell As String
iCount = DCount("
There are various dropdowns on the the first subform, which set the recordsource for the form and can be changed continually.
The recordource is determined from some queries which have parameters like
Forms![TabsReport]!.[Units1].Form.combo_A
All the drop downs work correctly and data is changed accordingly so the queries must be picking up the parameters from the form correctly.
However, when I try to export this to Excel, I get a "too few parameters. Expected 1" error even though it is trying to use excatly the same recordsource.
Any ideas why ?
Private Sub cmd_Export_Click()
Dim iCount As Integer
Dim strCell As String
iCount = DCount("
Code:
", Form.RecordSource)
'--------------------------------------
'Main recordset with level codes
'--------------------------------------
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(Form.RecordSource, dbOpenSnapshot)
'Start a new workbook in Excel
Dim oApp As New Excel.Application
Dim oBook As Excel.workBook
Dim oSheet As Excel.workSheet
Set oBook = oApp.Workbooks.Add
Set oSheet = oBook.Worksheets(1)
'Add the field names in row 1
Dim i As Integer
Dim iNumCols As Integer
iNumCols = rs.Fields.Count
For i = 1 To iNumCols
oSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
Next
'Add the data starting at cell A2
oSheet.Range("A2").CopyFromRecordset rs
'Format the header row as bold and autofit the columns
With oSheet.Range("a1").Resize(1, iNumCols)
.Font.Bold = True
.EntireColumn.AutoFit
End With
With oSheet
'now delete all data for previous months as it's not needed on the spreadsheet
.Columns("Z").Delete Shift:=xlToLeft
.Columns("R:X").Delete Shift:=xlToLeft
.Columns("O").Delete Shift:=xlToLeft
.Columns("G:M").Delete Shift:=xlToLeft
End With
'--------------------------------------
'Top 10 recordset
'--------------------------------------
Set rst = db.OpenRecordset(Forms!TabsReport!Top10Debtors_BU.Form.RecordSource, dbOpenSnapshot)
'Add the field names in row 1
iNumCols = rst.Fields.Count
For i = 1 To iNumCols
oSheet.Cells(iCount + 16, i).Value = rst.Fields(i - 1).Name
Next
'Format the header row as bold and autofit the columns
strCell = "A" & iCount + 16
With oSheet.Range(strCell).Resize(1, iNumCols)
.Font.Bold = True
.EntireColumn.AutoFit
End With
'Add the data starting at cell A?
strCell = "A" & iCount + 17
oSheet.Range(strCell).CopyFromRecordset rst
'------------------------------------------
'Format the sheet as per Finance template
'------------------------------------------
With oSheet
.Rows("1:1").Insert Shift:=xlDown
.Rows("1:1").Insert Shift:=xlDown
.Rows("1:1").Insert Shift:=xlDown
.Rows("1:1").Insert Shift:=xlDown
.Range("A3").Value = "(1) Debt Summary (£ 000's)"
.Range("A3").Font.Underline = xlUnderlineStyleSingle
.Range("A" & iCount + 7).Value = "Totals"
.Range("A" & iCount + 8).Value = "Totals Last Month"
.Range("A" & iCount + 9).Value = "Variance"
.Range("A" & iCount + 7 & ":A" & iCount + 9).Font.Bold = True
.Range("A" & iCount + 11).Value = "(2) Percentage Ageing"
.Range("A" & iCount + 11).Font.Underline = xlUnderlineStyleSingle
.Range("A" & iCount + 13).Value = "Current Month"
.Range("A" & iCount + 14).Value = "Ageing Last Month"
.Range("A" & iCount + 15).Value = "Variance"
.Range("A" & iCount + 18).Value = "(3) Top 10 Bad Debt Provisions"
.Range("A" & iCount + 18).Font.Underline = xlUnderlineStyleSingle
.Columns("A:A").EntireColumn.AutoFit
End With
'--------------------------------------
'Tidy up
'--------------------------------------
oApp.Visible = True
oApp.UserControl = True
'Close the Database and Recordset
rs.Close
rst.Close
db.Close
Set rs = Nothing
Set rst = Nothing
Set db = Nothing
Set oBook = Nothing
Set oSheet = Nothing
Set oApp = Nothing
End Sub