Solved

item not found in this collection

Posted on 2014-10-18
7
179 Views
Last Modified: 2014-10-22
Hi,
I cannot find anything wrong, but there obviously is as I keep getting "item not found in this collection" error.
I am trying to export the results of a totals query to Excel but changing a parameter or two beforehand.
All the fields exist and are spelled right.
Can someone please look at the code and tell me where I'm going wrong?
Thanks in advance.
Private Sub btnExp2Excel_Click()
On Error GoTo ErrHandler
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("qryReturnsAll")

If Me.txtFilterSum = 1 Then
    qdf.Parameters("CollFrom").Value = Me.cboCustFilter.Value
End If
If Me.txtFilterSum = 2 Then
    qdf.Parameters("RecMth").Value = Me.cboMonthFilter.Value
End If
If Me.txtFilterSum = 4 Then
    qdf.Parameters("fkPackTypeID").Value = Me.cboPackFilter.Value
End If

Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim strFileName As String
strFileName = (GetMyPath() & "Returns_" & Format$(Now(), "ddmmyyhhnn") & ".xlsx")
    DoCmd.TransferSpreadsheet acExport, , "qryReturnsAll", strFileName, True
    Set objXL = New Excel.Application
    With objXL
        Set objWkb = .Workbooks.Open(strFileName, , False)
        .Visible = True
        AppActivate objXL.Caption
        objWkb.Activate
    End With
    Set objWkb = Nothing
    Set objXL = Nothing
    Set qdf = Nothing

Exit_Sub:
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbOKOnly, "An Error has occurred, please inform S.Byrom"
    Resume Exit_Sub

End Sub

Open in new window

0
Comment
Question by:Stephen Byrom
  • 3
  • 3
7 Comments
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40389726
You will always have to define all parameters:

qdf.Parameters("CollFrom").Value = varDefaultValueCollFrom
qdf.Parameters("RecMth").Value = varDefaultValueRecMth
qdf.Parameters("fkPackTypeID").Value = varDefaultValuefkPackTypeID
If Me.txtFilterSum = 1 Then
    qdf.Parameters("CollFrom").Value = Me.cboCustFilter.Value
End If
If Me.txtFilterSum = 2 Then
    qdf.Parameters("RecMth").Value = Me.cboMonthFilter.Value
End If
If Me.txtFilterSum = 4 Then
    qdf.Parameters("fkPackTypeID").Value = Me.cboPackFilter.Value
End If

Also, with "item not found in this collection" you probably have misspelled something.

/gustav
0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 40389737
Thanks Gustav
I tried that to no avail.
It's not a parameter query in as much as the query will open without setting any except the year. It's a totals query.
see below

SELECT tblReturns.ID, tblReturns.RecDate, tblReturns.Tnum, tblReturns.CollFrom, tblOrg.OrgName, tblReturns.Carrier, tblReturns.GRN, Sum(tblReturns.Qty) AS SumOfQty, tblReturns.fkPackTypeID, tlkpPackType.PackType, Month([recdate]) AS RecMth
FROM tblOrg INNER JOIN (tblReturns INNER JOIN tlkpPackType ON tblReturns.fkPackTypeID = tlkpPackType.pkPackTypeID) ON tblOrg.pkOrgID = tblReturns.CollFrom
WHERE (((Year([recdate]))=[forms]![frmPackShipt]![cboYear]))
GROUP BY tblReturns.ID, tblReturns.RecDate, tblReturns.Tnum, tblReturns.CollFrom, tblOrg.OrgName, tblReturns.Carrier, tblReturns.GRN, tblReturns.fkPackTypeID, tlkpPackType.PackType, Month([recdate]);

I tried removing the base Where clause from the query but still no joy.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40389757
First, you can only set parameters that do exist in the query.
fkPackTypeID is not a parameter but a fieldname. Thus, you are told: "item not found in this collection". That collection is the collection of parameters.

Second, any parameter in the query must be specified.
"[forms]![frmPackShipt]![cboYear]" is a parameter you didn't specify.
When called from code, the query has no access to open forms.

/gustav
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 1

Author Closing Comment

by:Stephen Byrom
ID: 40389860
thanks a lot for the advice Gustav, explains a lot.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40389870
You are welcome!

/gustav
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 40396827
Instead of referencing controls on forms, I like to save the values from form controls to custom database properties.  Then they can be retrieved even if the form is closed.  Here is some sample code for working with custom database properties:

Option Compare Database
Option Explicit

Private dbs As DAO.Database
Private prp As DAO.Property
Private prps As DAO.Properties
Private strPropertyName As String
Private strPropertyValue As String
Private lngDataType as Long
Private varPropertyValue As Variant


Public Sub SetProperty(strName As String, lngType As Long, _
   varValue As Variant)
'Created by Helen Feddema 2-Oct-2006
'Modified by Helen Feddema 2-Oct-2006
'Called from various procedures

On Error GoTo ErrorHandler

   'Attempt to set the specified property
   Set dbs = CurrentDb
   Set prps = dbs.Properties
   prps(strName) = varValue

ErrorHandlerExit:
   Exit Sub

ErrorHandler:
    If Err.Number = 3270 Then
      'The property was not found; create it
      Set prp = dbs.CreateProperty(Name:=strName, _
         Type:=lngType, Value:=varValue)
      dbs.Properties.Append prp
      Resume Next
   Else
   MsgBox "Error No: " & Err.Number _
      & " in SetProperty procedure; " _
      & "Description: " & Err.Description
      Resume ErrorHandlerExit
   End If

End Sub

Public Function GetProperty(strName As String, strDefault As String) _
   As Variant
'Created by Helen Feddema 2-Oct-2006
'Modified by Helen Feddema 2-Oct-2006
'Called from various procedures

On Error GoTo ErrorHandler
   
   'Attempt to get the value of the specified property
   Set dbs = CurrentDb
   GetProperty = dbs.Properties(strName).Value

ErrorHandlerExit:
   Exit Function

ErrorHandler:
   If Err.Number = 3270 Then
      'The property was not found; use default value
      GetProperty = strDefault
      Resume Next
   Else
      MsgBox "Error No: " & Err.Number _
         & " in GetProperty procedure; " _
         & "Description: " & Err.Description
      Resume ErrorHandlerExit
   End If

End Function

Public Function ListCustomProps()
'Created by Helen Feddema 3-Oct-2006
'Modified by Helen Feddema 3-Oct-2006
'Lists DB properties created in code (as well as built-in properties)

On Error Resume Next
   
   Set dbs = CurrentDb
   Debug.Print "Database properties:"
   
   For Each prp In dbs.Properties
      Debug.Print vbTab & prp.Name & ": " & prp.Value
   Next prp

End Function

==================================
Usage examples:

Private dbs As DAO.Database
Private prp As DAO.Property
Private prps As DAO.Properties
Private lngDataType As Long
Private strPropertyName As String
Private strPropertyValue as String
Private varPropertyValue As Variant

Date
====
   If IsDate(Me![txtStartDate].Value) = False Then
      strTitle = "Invalid date"
      strPrompt = "Please enter a valid start date"
      GoTo ErrorHandlerExit
   Else
      dteStart = CDate(Me![txtStartDate].Value)
   End If
   
   strPropertyName = "PropName"
   lngDataType = dbDate
   Call SetProperty(strPropertyName, lngDataType, dteStart)

   GetStartDate = CDate(GetProperty("PropName", #12:00:00 AM#))

Text
====
   strPropertyName = "PropName"
   strPropertyValue = CStr(cbo.Value)
   lngDataType = dbText
   Call SetProperty(strPropertyName, lngDataType, _
      strPropertyValue )

   strDocsPath = GetProperty("PropName", "")

Long
====
   strPropertyName = "PropName"
   lngDataType = dbLong
   Call SetProperty(strPropertyName, lngDataType, lngID)

   lngID = CLng(GetProperty("PropName", ""))

Integer
=======
   strPropertyName = "PropName"
   lngDataType = dbInteger
   Call SetProperty(strPropertyName, lngDataType, intMonth)

   intID = CInt(GetProperty("PropName", ""))

Boolean
=======
   strPropertyName = "PropName"
   lngDataType = dbBoolean
   Call SetProperty(strPropertyName, lngDataType, "True")

   blnValue = CBln(GetProperty("PropName", ""))


Saving to a custom property from a control's AfterUpdate event
==============================================================
Private Sub txtDate_AfterUpdate()
'Created by Helen Feddema 2-Sep-2009
'Last modified 2-Sep-2009

On Error GoTo ErrorHandler
   
   If IsDate(Me![txtDate].Value) = True Then
      dteSingle = CDate(Me![txtDate].Value)
      strPropertyName = "SingleDate"
      Call SetProperty(strName:=strPropertyName, _
         lngType:=dbDate, varValue:=dteSingle)
   End If
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & _
      Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window


Apart from this, you may need to create a make-table query and then use the table created by it for the export.  This can all be automated in code.
0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 40396847
Thanks for the great advice Helen and the code samples. I will definitely take advantage of it
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question