Solved

item not found in this collection

Posted on 2014-10-18
7
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 50

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 50

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Closing Comment

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

756 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