3265 - Item Not Found In This Collection
I'm using MS Access 2003. Through VBA I'm attempting to fill in an excel spreadsheet with a query based on a table. Here is my code (setting the QueryDef is causing the error, see text in bold/italics):
Expand|Select|Wrap|Line Numbers
- Dim MyExcel As Excel.Application
- Dim MyWorkbook As Excel.Workbook
- Dim MyWorksheet As Excel.Worksheet
- Dim strMyWorkbook As String
- Dim strSQL As String
- Dim dbs As Database
- Dim qdf As QueryDef
- Dim rst As Recordset
- ' Open the MonthlyBreakdown spreadsheet
- strMyWorkbook = GetPathOrName(GetDatabasePathAndName()) & "\Copy of MonthlyBreakdown.xls"
- Set MyExcel = New Excel.Application
- Set MyWorkbook = MyExcel.Workbooks.Open(strMyWorkbook)
- Set dbs = CurrentDb()
- strSQL = "SELECT DISTINCTROW Left([tblTransaction].[Treaty],6) AS Treaty6, " & _
- "Sum(tblTransaction.txnPremium1) AS BasePrem, " & _
- "Sum(tblTransaction.txnAllowance1) AS BaseAllow, " & _
- "Sum(tblTransaction.txnPremium2) AS ADBPrem, " & _
- "Sum(tblTransaction.txnAllowance2) AS ADBAllow, " & _
- "Sum(tblTransaction.txnPremium3) AS WaiverPrem, " & _
- "Sum(tblTransaction.txnAllowance3) AS WaiverAllow, " & _
- "Sum(tblTransaction.txnPremium4) AS XtraPrem, " & _
- "Sum(tblTransaction.txnAllowance4) AS XtraAllow " & _
- "FROM tblTransaction " & _
- "WHERE (((Left([tblTransaction].[txnBillingDate], 6)) = [Forms]![frmDates]![cmbYear] & [Forms]![frmDates]![cmbMonth])) " & _
- "GROUP BY Left([tblTransaction].[Treaty],6) " & _
- "ORDER BY Left([tblTransaction].[Treaty],6), Sum(tblTransaction.txnPremium1)"
- Set qdf = dbs.QueryDefs(strSQL)
- qdf("[Forms]![frmDates]![cmbYear]") = [Forms]![frmDates]![cmbYear]
- qdf("[Forms]![frmDates]![cmbMonth]") = [Forms]![frmDates]![cmbMonth]
- Set rst = qdf.OpenRecordset()
Any help you can give me would be appreciated. Thanks.