Runtime Error 9 Subscript out of range

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to add code to a report module. This is a new imported report
without any prior modules in it.

The following line of code:
Modules("Report_" & ReportName).AddFromString str
Fails with Runtime Error 9 Subscript out of range.

If I open the report, add a dummy sub routine and then run the code it works
fine.

Here is the complete function:

Function AddCode(ReportName As String)

Dim str As String

str = "some_code"

DoCmd.OpenReport ReportName, acViewDesign
Application.Reports(ReportName).Visible = False
Modules("Report_" & ReportName).AddFromString str
Reports(ReportName).OnClose = "[Event Procedure]"
DoCmd.Close acReport, ReportName, acSaveYes

End Function
 
Do you have spaces or special characters in the Report name?

If you do, you have a naming conflict between Access and VBA ...

Check Access Help topics:

* Guidelines for naming fields, controls and objects.
*Guidelines for naming Visual Basic procedures, variables and constants.
 
Just read your question again and noted that there is a possibility that the
Property "Has Module" of your report might have been set to No / False. Try
setting this to Yes / True.
 
I do not have any special characters or spaces in my report.
Also when I set the HasModule property to True and ran the code I got the
following error: Run Time Error 7961. MS Access can't find the module
'Report_testreport' referred to in a macro expression or VB code.


Van T. Dinh said:
Just read your question again and noted that there is a possibility that the
Property "Has Module" of your report might have been set to No / False. Try
setting this to Yes / True.

--
HTH
Van T. Dinh
MVP (Access)



James said:
I am trying to add code to a report module. This is a new imported report
without any prior modules in it.

The following line of code:
Modules("Report_" & ReportName).AddFromString str
Fails with Runtime Error 9 Subscript out of range.

If I open the report, add a dummy sub routine and then run the code it
works
fine.

Here is the complete function:

Function AddCode(ReportName As String)

Dim str As String

str = "some_code"

DoCmd.OpenReport ReportName, acViewDesign
Application.Reports(ReportName).Visible = False
Modules("Report_" & ReportName).AddFromString str
Reports(ReportName).OnClose = "[Event Procedure]"
DoCmd.Close acReport, ReportName, acSaveYes

End Function
 
It still sounds like VBA is not picking up the Class container behind the
Report "testreport".

Try adding some code in the module (e.g. some silly function), copile the
code, save the Report and close the database. Open the database again and
see if you pick up the Class module.

Do you regularly compact & repair the database?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top