Error 3265 - Item not found in collection (1 Viewer)

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
I am getting this error everytime I run this script. Can anyone tells what is causing the error?
To give you a brief description of my database -

I have got a database that consists of a query which calculates monthly inventory data (MIOH) A cross tab query then creates a cross tab such that employee number is the row heading, month (mm/yyyy) , YTD (yyyy) are column headings and MIOH is the value. There are two parameters in this query which are entered by the user using a form. These are start date and end date. The SQL of the cross tab query is given below.

CrossTab query SQL
Code:
[FONT=Calibri][SIZE=3]TRANSFORM Sum(Union.METRIC) AS SumOfMETRIC[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]SELECT Union.ABUYR, Union.TYPE[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]FROM [Union][/SIZE][/FONT]
[FONT=Calibri][SIZE=3]WHERE (((Union.PERIOD1) Between [Forms].[Form1]![StartDate] And [Forms].[Form1]![EndDate]))[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]GROUP BY Union.ABUYR, Union.TYPE[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]PIVOT Union.PERIOD1;
[/SIZE][/FONT]

The code I am using is:
Code:
Option Compare Database
'  Constant for maximum number of columns CrossTab query would
    Const conTotalColumns = 14
'  Variables for Database object and Recordset.
    Dim dbsReport As DAO.Database
    Dim rstReport As DAO.Recordset
'  Variables for number of columns and row and report totals.
    Dim intColumnCount As Integer
    Dim lngRgColumnTotal(1 To conTotalColumns) As Long
    Dim lngReportTotal As Long
Private Sub InitVars()
Dim intX As Integer
' Initialize lngReportTotal variable.
    lngReportTotal = 0
' Initialize array that stores column totals.
    For intX = 1 To conTotalColumns
    lngRgColumnTotal(intX) = 0
Next intX
End Sub
Private Function xtabCnulls(varX As Variant)
' Test if a value is null.
    If IsNull(varX) Then
' If varX is null, set varX to 0.
        xtabCnulls = 0
    Else
' Otherwise, return varX.
        xtabCnulls = varX
    End If
End Function
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    ' Put values in text boxes and hide unused text boxes.
    Dim intX As Integer
    '  Verify that you are not at end of recordset.
        If Not rstReport.EOF Then
    '  If FormatCount is 1, put values from recordset into text boxes
    '  in "Detail" section.
            If Me.FormatCount = 1 Then
            For intX = 1 To intColumnCount
    '  Convert Null values to 0.
            Me("Col" + Format(intX)) = xtabCnulls(rstReport(intX - 1))
        Next intX
    '  Hide unused text boxes in the "Detail" section.
        For intX = intColumnCount + 2 To conTotalColumns
            Me("Col" + Format(intX)).Visible = False
        Next intX
    '  Move to next record in recordset.
        rstReport.MoveNext
            End If
        End If
End Sub
Private Sub Detail_Retreat()
' Always back up to previous record when "Detail" section retreats.
rstReport.MovePrevious
End Sub
Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
    Dim intX As Integer
'  Put column headings into text boxes in page header.
        For intX = 1 To intColumnCount
        Me("Head" + Format(intX)) = rstReport(intX).Name
        Next intX
'  Hide unused text boxes in page header.
    For intX = (intColumnCount + 1) To conTotalColumns
    Me("Head" + Format(intX)).Visible = False
    Next intX
End Sub
Private Sub Report_Close()
On Error Resume Next
'  Close recordset.
rstReport.Close
End Sub
Private Sub Report_NoData(Cancel As Integer)
MsgBox "No records match the criteria you entered.", vbExclamation, "No Records Found"
rstReport.Close
Cancel = True
End Sub
Private Sub Report_Open(Cancel As Integer)
'  Create underlying recordset for report using criteria entered in
'  Form1 form.
Dim intX As Integer
Dim qdf As QueryDef
Dim frm As Form
'  Set database variable to current database.
Set dbsReport = CurrentDb
Set frm = Forms!Form1
'  Open QueryDef object.
Set qdf = dbsReport.QueryDefs("CrossTab")
' Set parameters for query based on values entered
' in Form1 form.
qdf.Parameters("[Forms].[Form1]![StartDate]") _
= frm!StartDate
qdf.Parameters("[Forms].[Form1]![EndDate]") _
= frm!EndDate
'  Open Recordset object.
Set rstReport = qdf.OpenRecordset()
'  Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
End Sub
Private Sub ReportHeader_Format(Cancel As Integer, FormatCount As Integer)
'  Move to first record in recordset at the beginning of the report
'  or when the report is restarted. (A report is restarted when
'  you print a report from Print Preview window, or when you return
'  to a previous page while previewing.)
rstReport.MoveFirst
'Initialize variables.
InitVars
End Sub

I get error 3265 - item not found in collection and the debugger highlights the line

Code:
Me("Head" + Format(intX)) = rstReport(intX).Name
When I hover my mouse over the rstReport(intX).Name part, the pop up shows "rstReport(intX).Name = <item not found in this collection>
Any help is appreciated!
Thanks!
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:47
Joined
Aug 30, 2003
Messages
36,133
Is it getting part way through? The Fields property is zero based, so I'd guess it's starting at the second field and failing at the end.
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
Sorry I forgot I had already posted it. I will delete the other post. :)

I have no clue what is happening. What do you mean by it is starting at the second field? How do I make out where it's failing? Any clue on what might be causing it to fail at the end?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:47
Joined
Aug 30, 2003
Messages
36,133
When it fails and takes you into debug mode, hover over intX and note what it's value is. Then note how many columns you have in the recordset. I'm guessing they'll be the same, and like I said, the fields property is zero based, so try:

For intX = 0 To intColumnCount - 1
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
uhh...now it leaves out the first column...has that got something to do with the zero based property? how can it be corrected?
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:47
Joined
Aug 30, 2003
Messages
36,133
Sure; I'm surprised it doesn't error on the first one (assuming there's no control named "Head0"). Try this:

Me("Head" + Format(intX + 1)) = rstReport(intX).Name

You have to account somehow for the potential differences between the fields (0 - 9 let's say) and the control names (presumably 1-10).
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
Here is the modified code:
Now it leaves out the first and last columns
Code:
'  Constant for maximum number of columns CrossTab query would create is 14 scale_type+12months+Year
   Const conTotalColumns = 14
   '  Variables for Database object and Recordset.
   Dim dbsReport As DAO.Database
   Dim rstReport As DAO.Recordset
   '  Variables for number of columns and row and report totals.
   Dim intColumnCount As Integer
   Dim lngRgColumnTotal(1 To conTotalColumns) As Long
   Dim lngReportTotal As Long
Private Sub InitVars()
      
   Dim intX As Integer
   ' Initialize lngReportTotal variable.
   lngReportTotal = 0
    
   ' Initialize array that stores column totals.
   For intX = 1 To conTotalColumns
      lngRgColumnTotal(intX) = 0
   Next intX
End Sub

Private Function xtabCnulls(varX As Variant)
     
   ' Test if a value is null.
   If IsNull(varX) Then
      ' If varX is null, set varX to 0.
      xtabCnulls = 0
   Else
      ' Otherwise, return varX.
      xtabCnulls = varX
   End If
End Function

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
   ' Put values in text boxes and hide unused text boxes.
    
   Dim intX As Integer
   '  Verify that you are not at end of recordset.
   If Not rstReport.EOF Then
      '  If FormatCount is 1, put values from recordset into text boxes
      '  in "Detail" section.
      If Me.FormatCount = 1 Then
         For intX = 1 To intColumnCount - 1
            '  Convert Null values to 0.
            Me("Col" + Format(intX + 1)) = xtabCnulls(rstReport(intX))
         Next intX
    
         '  Hide unused text boxes in the "Detail" section.
         For intX = intColumnCount To conTotalColumns
            Me("Col" + Format(intX)).Visible = False
         Next intX
         '  Move to next record in recordset.
         rstReport.MoveNext
      End If
   End If
    
End Sub
Private Sub Detail_Retreat()
   ' Always back up to previous record when "Detail" section retreats.
   rstReport.MovePrevious
End Sub

Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
    
   Dim intX As Integer
    
   '  Put column headings into text boxes in page header.
   For intX = 1 To intColumnCount - 1
      Me("Head" + Format(intX + 1)) = rstReport(intX).Name
   Next intX
   '  Hide unused text boxes in page header.
   For intX = (intColumnCount) To conTotalColumns
      Me("Head" + Format(intX)).Visible = False
   Next intX
End Sub

Private Sub Report_Close()
    
   On Error Resume Next
   '  Close recordset.
   rstReport.Close
    
End Sub

Private Sub Report_NoData(Cancel As Integer)
   MsgBox "No records match the criteria you entered.", vbExclamation, "No Records Found"
   rstReport.Close
   Cancel = True
End Sub
Private Sub Report_Open(Cancel As Integer)
   '  Create underlying recordset for report using criteria entered in
   '  EmployeeSalesDialogBox form.
    
   Dim intX As Integer
   Dim qdf As QueryDef
   Dim frm As Form
   '  Set database variable to current database.
   Set dbsReport = CurrentDb
   Set frm = Forms!Form1
   '  Open QueryDef object.
   Set qdf = dbsReport.QueryDefs("CrossTab")
   ' Set parameters for query based on values entered
   ' in Form1.
   qdf.Parameters("[Forms].[Form1]![StartDate]") _
     = frm!StartDate
   qdf.Parameters("[Forms].[Form1]![EndDate]") _
     = frm!EndDate
   '  Open Recordset object.
   Set rstReport = qdf.OpenRecordset()
   
   '  Set a variable to hold number of columns in crosstab query.
   intColumnCount = rstReport.Fields.Count
    
End Sub

Private Sub ReportHeader_Format(Cancel As Integer, FormatCount As Integer)
   '  Move to first record in recordset at the beginning of the report
   '  or when the report is restarted. (A report is restarted when
   '  you print a report from Print Preview window, or when you return
   '  to a previous page while previewing.)
   rstReport.MoveFirst
   'Initialize variables.
   InitVars
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:47
Joined
Aug 30, 2003
Messages
36,133
Which part? It doesn't look like you've made similar adjustments to the other parts of the code that reference both fields and controls. Or they're done wrong, like:

For intX = 1 To intColumnCount - 1

Note that I posted

For intX = 0 To intColumnCount - 1
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
Ok. I have corrected that. This is what I have.
Code:
'  Constant for maximum number of columns CrossTab query would create is 14 scale_type+12months+Year
   Const conTotalColumns = 14
   '  Variables for Database object and Recordset.
   Dim dbsReport As DAO.Database
   Dim rstReport As DAO.Recordset
   '  Variables for number of columns and row and report totals.
   Dim intColumnCount As Integer
   Dim lngRgColumnTotal(1 To conTotalColumns) As Long
   Dim lngReportTotal As Long
Private Sub InitVars()
      
   Dim intX As Integer
   ' Initialize lngReportTotal variable.
   lngReportTotal = 0
    
   ' Initialize array that stores column totals.
   For intX = 1 To conTotalColumns
      lngRgColumnTotal(intX) = 0
   Next intX
End Sub

Private Function xtabCnulls(varX As Variant)
     
   ' Test if a value is null.
   If IsNull(varX) Then
      ' If varX is null, set varX to 0.
      xtabCnulls = 0
   Else
      ' Otherwise, return varX.
      xtabCnulls = varX
   End If
End Function

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
   ' Put values in text boxes and hide unused text boxes.
    
   Dim intX As Integer
   '  Verify that you are not at end of recordset.
   If Not rstReport.EOF Then
      '  If FormatCount is 1, put values from recordset into text boxes
      '  in "Detail" section.
      If Me.FormatCount = 1 Then
         For intX = 0 To intColumnCount - 1
            '  Convert Null values to 0.
            Me("Col" + Format(intX + 1)) = xtabCnulls(rstReport(intX))
         Next intX
    
         '  Hide unused text boxes in the "Detail" section.
         For intX = intColumnCount To conTotalColumns
            Me("Col" + Format(intX)).Visible = False
         Next intX
         '  Move to next record in recordset.
         rstReport.MoveNext
      End If
   End If
    
End Sub
Private Sub Detail_Retreat()
   ' Always back up to previous record when "Detail" section retreats.
   rstReport.MovePrevious
End Sub

Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
    
   Dim intX As Integer
    
   '  Put column headings into text boxes in page header.
   For intX = 0 To intColumnCount - 1
      Me("Head" + Format(intX + 1)) = rstReport(intX).Name
   Next intX
   '  Hide unused text boxes in page header.
   For intX = (intColumnCount) To conTotalColumns
      Me("Head" + Format(intX)).Visible = False
   Next intX
End Sub

Private Sub Report_Close()
    
   On Error Resume Next
   '  Close recordset.
   rstReport.Close
    
End Sub

Private Sub Report_NoData(Cancel As Integer)
   MsgBox "No records match the criteria you entered.", vbExclamation, "No Records Found"
   rstReport.Close
   Cancel = True
End Sub
Private Sub Report_Open(Cancel As Integer)
   '  Create underlying recordset for report using criteria entered in
   '  EmployeeSalesDialogBox form.
    
   Dim intX As Integer
   Dim qdf As QueryDef
   Dim frm As Form
   '  Set database variable to current database.
   Set dbsReport = CurrentDb
   Set frm = Forms!Form1
   '  Open QueryDef object.
   Set qdf = dbsReport.QueryDefs("CrossTab")
   ' Set parameters for query based on values entered
   ' in Form1.
   qdf.Parameters("[Forms].[Form1]![StartDate]") _
     = frm!StartDate
   qdf.Parameters("[Forms].[Form1]![EndDate]") _
     = frm!EndDate
   '  Open Recordset object.
   Set rstReport = qdf.OpenRecordset()
   
   '  Set a variable to hold number of columns in crosstab query.
   intColumnCount = rstReport.Fields.Count
    
End Sub

Private Sub ReportHeader_Format(Cancel As Integer, FormatCount As Integer)
   '  Move to first record in recordset at the beginning of the report
   '  or when the report is restarted. (A report is restarted when
   '  you print a report from Print Preview window, or when you return
   '  to a previous page while previewing.)
   rstReport.MoveFirst
   'Initialize variables.
   InitVars
End Sub
The good new is --> first column is back. Bad news is--> last column is still not being displayed :)
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:47
Joined
Aug 30, 2003
Messages
36,133
Can you post the db, or does it contain sensitive data?
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
I could post it, but the tables are on the network so I cannot post them. I will try to make dummy tables and post the db.
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
Here is the db...
 

Attachments

  • DynamicCrossTabReport.mdb
    228 KB · Views: 170

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:47
Joined
Aug 30, 2003
Messages
36,133
I have to run out for a while, but will look at it later. It would help to know what criteria to use, and what you're not seeing that you should.
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
The period column in the cross tab query contains the month and year in the format mm/yyyy. The criteria I have used is Between [Forms].[Form1]![StartDate] And [Forms].[Form1]![EndDate]
StartDate and EndDate are specified by the user in the form Form1.
What I don’t see is is the last month in the date range provided by the user. For example if the range was specified as 01/2009 to 08/2009 the report contains data only upto 07/2009. I am guessing it is hiding the last column (08/2009) in the report.
Another issue independent from this is that I have a YTD (aggregate) number in the same column. That is, I have records which are “2009” instead of 01/2009, 02/2009….etc. I would like this column to show up as a column on the report but what is happening is, because of the criteria used – Between StartDate And EndDate – the year falls out of the criteria and is not displayed. Is there a way by which the criteria can be modified so that it reads – Between StartDate And EndDate And Year?
Appreciate your help!
Thanks!
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
I got to work. I had to play around with the operators on the intX and conTotalCol variables. It works like I want it to. Thank you so much for the help!
Also, I mentioned about the criteria in my previous reply – I will put that up on a separate thread because it is really not connected with error 3265. However, I did have a question related to the current topic of discussion - I wanted to know if I moved the criteria (which currently applied to the crosstab query that report is based off of) to a query that the cross tab query is based off of – For example, consider the following sequence of queries.
Union Query1àUnion Query 2à Cross tab Queryà Report.
Currently the criteria is applied to the cross tab query, if I were to remove it and apply it to Union Query1 would the code need to change at all? If yes, what would need to change?
Thanks!
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 17:47
Joined
Aug 30, 2003
Messages
36,133
Sorry, I got involved with switching Exchange servers and had trouble with my fax software working with Exchange and SQL Server. Glad you sorted it out.

I'm not sure I'm understanding the question, and something got goofed up in the format of that post. I'm assuming that code is there because otherwise you get an error opening the recordset?
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
No problem! J Appreciate your help so far!
I need the report to display all the months and year-to-date. Months and Year-to-date are in the same column – Period1 of the cross tab query which is also the column heading of the cross tab query.
I have a criteria applied to the Period 1 column by which the user can specify the range of months that they want to view. I have used the syntax Between [StartDate] And [EndDate] . However this criteria never catches the year-to-date therefore year-to-date never appears in the cross tab query. I think I have two options to tackle this issue:
1. Specify criteria so that it displays all the months between start date and end date and also displays whatever record that matches the year part of the end date with suffix as YTD e.g. EndDate(“yyyy”, &” YTD”) that the user entered. This way, if the user entered 01/2009 to 08/2009 it will show all the columns that correspond to 01/2009, 02/2009, 03/2009 …..to 08/2009 and it will also show 2009 YTD which is essentially the year-to-date calculation. However, I do not know the syntax for defining such a criteria.I am thinking it will look something like Between [StartDate] And [EndDate] And Like EndDate(“yyyy”,&” YTD”)
2. The second option is to apply the criteria earlier on in the sequence of queries. Since YTD and monthly data is calculated in separate queries and joined together using a union query before the cross tab query I thought, applying the criteria to the two separate queries (one for monthly date (range) and the other for YTD (specific year extracted from the EndDate+(suffix) YTD) might work. However, I am not sure what changes I need to make to the above code if I move the parameters from the crosstab query to the union query.
 

accessaspire219

Registered User.
Local time
Yesterday, 19:47
Joined
Jan 16, 2009
Messages
126
Pbaldy, do you know how this code could be modified if I wanted it to ignore the first two columns of the query and start from the third column? That is, the third column of the crosstab query will be the first column of the report. Also the first two columns of the query are supervisor and a team member. I want the report to be printed such that there is one page for each team member. (name of the team member and the supervisor will appear at the top of the page) Any suggestions on how this could be done?
Thanks!
 

Users who are viewing this thread

Top Bottom