Solved

item not found in this collection

Posted on 2014-10-18
7
199 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

630 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