Is it possible to use a variable in a set report statement?

I have the following code:

   Dim rpt As Report
   prpReportName = Application.CurrentObjectName
   Set rpt = Reports!prpReportName.Report

Open in new window


I get the error: 'The report name prpRepaortName is misspelled or doesn't exist... etc'

Thanks in advance,

CRB
Clive BeatonAccess DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

als315Commented:
You should change syntax:
   prpReportName = Application.CurrentObjectName
   Set rpt = Reports(prpReportName)
   Debug.Print rpt.Name
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PatHartmanCommented:
Without context it's hard to answer the question.  For example, when I write code that will be used with multiple forms (same for reports), I define the code in a standard code module so it will be available from any form that needs it.  When I call the code I pass in the current form.

Call SomeProcedure(Me)

Using the "Me" object passes the current object which is either a form or report to the called procedure.  Then at the receiving end, here's an example of a common procedure.
Public Sub LockControls(frm As Form, bLock As Boolean)
    Dim ctl As Control
    For Each ctl In frm.Controls
    Select Case ctl.ControlType
        Case acTextBox, acComboBox, acListBox, acCheckBox
            Select Case ctl.Tag
                Case "NoLock"
                    ctl.Locked = False
                Case "Lock"
                    ctl.Locked = True
                Case Else
                    ctl.Locked = bLock         'toggle locks
            End Select
        Case acOptionGroup, acOptionButton, acCheckBox      ''not working
             Select Case ctl.Tag
                Case "NoLock"
                    ctl.Locked = False
                Case "Lock"
                    ctl.Locked = True
                Case Else
                    ctl.Locked = bLock         'toggle locks
            End Select
        Case acCommandButton
            Select Case ctl.Tag
                Case "NoLock"
                    ctl.Enabled = True
                Case "Lock"
                    ctl.Enabled = False
                Case Else
                    ctl.Enabled = Not bLock         'toggle locks
            End Select
    End Select
Next ctl
Set ctl = Nothing
End Sub

Open in new window

0
Clive BeatonAccess DeveloperAuthor Commented:
ALS, thanks very much.  Worked perfectly.

Thanks Pat.  Useful Routine.

CRB
0
PatHartmanCommented:
You're welcome.  It's one of the true universal routines I use.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.