Setting report control values via OpenArgs

sparky961

Soaper Extraordinaire
Local time
Today, 08:05
Joined
May 7, 2009
Messages
28
First, here's my code that doesn't work:
Code:
Private Sub Report_Open(Cancel As Integer)
    Dim Args() As String
    Dim ControlName As String
    Dim Property As String
    Dim Value As String
    Dim ThisArg() As String
    
    If Not IsNull(Me.OpenArgs) Then
        Args = Split(Me.OpenArgs, ";")
        
        For Each Arg In Args
            ThisArg = Split(Arg, "|")
            ControlName = ThisArg(0)
            Property = ThisArg(1)
            Value = ThisArg(2)
            [B]Me.Controls(ControlName).Property = Value[/B]
        Next Arg
    End If
End Sub

The OpenArgs string would be built like this:
Code:
strOpenArgs = "lblReportTitle|Caption|Test;"

The highlighted line shows what I'm trying to do, but this doesn't work as written. With my current OpenArgs string, it should execute something like:
Code:
lblReportTitle.Caption = "Test"

Can someone tell me how I can do something similar, or perhaps an even better way of accomplishing the same thing?

Thanks..
-Sparky
 
Try putting your code in the ReportHeader_Format event.
 
Thanks for the suggestion, RuralGuy.

Forgive me if I'm a bit naive (when it comes to Access and VBA, I definitely am), but how does this help to programmatically access the properties of various controls by name?

Thanks..
-Sparky
 
With Forms, the Open event is too early to reference a control on the form. To do this you need to use the OnLoad event instead. A Report does not have an OnLoad event so I pick an event that occurs once and is later in the sequence than the OnOpen event.
 
You're welcome. Let us know if your issue is resolved.
 
The original issue isn't yet completely resolved. I'm still looking for a way to set a property using a string for that property name.

For example, if I wanted to do this...
Code:
cboMyCombo.Text = "my string"

Except that I was using the string "Text" to reference that property...

Is this possible in VBA/Access?
 
You know how to use a variable to reference a control. Now you would like to use a variable to reference a property of that control as well? Are you aware you are using reserved words as names for objects?
 
What properties did you want to reference with a variable, if you don't mind saying?
 
My immediate need is to set the title of a report based on how it's called from code.

As far as I can tell, the best way to do this is through the use of the OpenArgs parameter of DoCmd.OpenReport. The problem is doing something intelligent with the OpenArgs from the report's Format (or other) method.

Good coding practice would indicate against using something like a comma-separated list, then stuffing each argument into a specific variable. What I'm trying to emulate here is passing in a three-dimensional array containing the name of the control, the property of that control, and a value to assign to it.

I really hope there's an easier way to do this. It's turning out to be a real pain! :)

-Sparky
 

Users who are viewing this thread

Back
Top Bottom