Solved

item not found in this collection

Posted on 2014-10-18
7
173 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now