Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

item not found in this collection

Posted on 2014-10-18
7
Medium Priority
?
206 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 51

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 51

Accepted Solution

by:
Gustav Brock earned 2000 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 1

Author Closing Comment

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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 …

715 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