Asked by:
Run-Time Error 2465

Question
-
I'm getting a run-time 2465 error from Access 2016. It says it cannot find the "label_17". The section in bold and italicize is what Access highlights in yellow. Please assist. Here is the code:
Private Sub Report_Open(Cancel As Integer)
Dim SrcRs As DAO.Recordset
Set SrcRs = CurrentDb.OpenRecordset("Weekly_Value")'Assign Columns
For i = 2 To 16
Me.Controls("txt_" & i).ControlSource = ""
Me.Controls("txt_" & i).Visible = False
Me.Controls("TTL_" & i).Visible = False
Me.Controls("label_" & i).Caption = "_"
Me.Controls("label_" & i).Visible = False
DoEvents
Next
For i = 2 To SrcRs.Fields.Count - 1
Me.Controls("label_" & i).Caption = Right(SrcRs.Fields(i).Name, 6)
Me.Controls("label_" & i).Visible = True
DoEvents
Next
'Assign Fields
For i = 0 To SrcRs.Fields.Count - 1
Me.Controls("txt_" & i).ControlSource = CStr(SrcRs.Fields(i).Name)
Me.Controls("txt_" & i).Visible = True
If i > 0 Then
TempVars.Add "fld_" & i, SrcRs.Fields(i).Name
Me.Controls("TTL_" & i).Visible = True
End If
DoEvents
Next
Me.RecordSource = "Weekly_Value"
'Assign Totals
End Sub- Edited by Chrissy Anne Wednesday, June 21, 2017 1:13 PM
Wednesday, June 21, 2017 1:10 PM
All replies
-
I'm getting a run-time error 2465. Access cannot find "label_17" in the expression. Any assistance will be greatly appreciated.
Private Sub Report_Open(Cancel As Integer)
Dim SrcRs As DAO.Recordset
Set SrcRs = CurrentDb.OpenRecordset("Weekly_Value")'Assign Columns
For i = 2 To 16
Me.Controls("txt_" & i).ControlSource = ""
Me.Controls("txt_" & i).Visible = False
Me.Controls("TTL_" & i).Visible = False
Me.Controls("label_" & i).Caption = "_"
Me.Controls("label_" & i).Visible = False
DoEvents
Next
For i = 2 To SrcRs.Fields.Count - 1
Me.Controls("label_" & i).Caption = Right(SrcRs.Fields(i).Name, 6)
Me.Controls("label_" & i).Visible = True
DoEvents
Next
'Assign Fields
For i = 0 To SrcRs.Fields.Count - 1
Me.Controls("txt_" & i).ControlSource = CStr(SrcRs.Fields(i).Name)
Me.Controls("txt_" & i).Visible = True
If i > 0 Then
TempVars.Add "fld_" & i, SrcRs.Fields(i).Name
Me.Controls("TTL_" & i).Visible = True
End If
DoEvents
Next
Me.RecordSource = "Weekly_Value"
'Assign Totals
End Sub- Merged by Deepak Saradkumar PanchalMicrosoft contingent staff Thursday, June 22, 2017 3:14 AM same issue
Wednesday, June 21, 2017 1:17 PM -
On which line?
You iteratate For i = 2 To 16 so I'll assume the issue is originating from the Me.Controls("label_" & i).Caption = Right(SrcRs.Fields(i).Name, 6) line of code. This would therefore indicate that SrcRs.Fields.Count - 1 > 16. So how many columns are in your table/query Weekly_Value?
Daniel Pineault, 2010-2016 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.netWednesday, June 21, 2017 1:37 PM -
Thank you for your response. The line that is highlight is as follows:
Me.Controls("label_" & i).Caption = Right(SrcRs.Fields(i).Name, 6)
There are 17 columns.
Wednesday, June 21, 2017 2:07 PM -
Looks like this is a duplicate post of this thread.
Good luck!
Wednesday, June 21, 2017 3:02 PM -
Hi Chrissy,
And you're absolutely certain there is a label named "label_17" on your report, correct? Could there be a typo?
Wednesday, June 21, 2017 3:05 PM -
There is no label_17 on the report.Wednesday, June 21, 2017 4:34 PM
-
There is no label_17 on the report.
Ok, it explains the error. Now, what we need to find is the logic error causing it. For instance, you're using this in your loop:
For i = 2 To SrcRs.Fields.Count - 1
Have you verified the number of fields returned by SrcRs?
Just a thought...
Wednesday, June 21, 2017 4:39 PM -
No I have not. What would be the best way to determine that? The user will enter the dates they want to select, if I select a range that is within 16 weeks I do not get an error, however anything more than 16 weeks I get this run-time error.Wednesday, June 21, 2017 4:47 PM
-
You might be able to simply use a constant number: 16
If the user can select a date range less than 16 weeks, then you could add a test to, I assume, hide the unused columns.
For example:
For i = 2 To 16
If SrcRs.Fields.Count >= i Then
Me.Controls("label_" & i).Caption = Right(SrcRs.Fields(i).Name,6)
Else
Me.Controls("label_" & i).Visible = False
End If
Next
(untested)
Hope it helps...
- Edited by .theDBguy Wednesday, June 21, 2017 5:31 PM
- Proposed as answer by Edward8520Microsoft contingent staff Monday, June 26, 2017 3:18 AM
Wednesday, June 21, 2017 5:30 PM -
I will try it out and advise. Yes with the monthly, quarterly and yearly I was able to hide the unused columns.Wednesday, June 21, 2017 6:52 PM
-
I can see that you had created 2 threads for the same issue.
so I merge the both threads to avoid the duplication.
so did you try to implement the suggestion provided by the other community member?
if yes, let us know about the current status of your issue?
Regards
Deepak
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Thursday, June 22, 2017 3:22 AM