Setting a Variable on the Fly which is Unknown at Design-time

D

dch3

I have several module-level variables in a report that I'm designing. The
variables will be set via a routine that extracts the values from the
..OpenArgs property. I want to smoothly and gracefully extract the values and
set the variables in such a way that I can reuse the technique on any form
and on any report.

What I need to know is how you can pass the NAME of variable into a sub as a
string and then using the string set the variable that the string refers to?
The code's listed below. Report_Open works like a charm - no issues. Its the
setParameterVariable() sub that's not working. I was thinking Eval() but it
doesn't work.

The idea is that the .OpenArgs will contain a string where each
variable/value pair is delimited with a ";" and then subdelimited with a "="
as in
"mPrintLoadListHeaderInformation=True;mPrintDetail=True;mPrintFooter=True"

Option Compare Database
Option Explicit

Dim aOpenParameters()
Dim mPrintLoadListHeaderInformation As Boolean
Dim intNumberOfParameters As Integer

Private Sub Report_Open(Cancel As Integer)

'Break the string using ";" as the delimiter
Dim aOpenArgs
'Break the substrings using "=" as the delimiter
Dim aOpenArgsDetail
Dim i As Integer

aOpenArgs = Split(Me.OpenArgs, ";")

intNumberOfParameters = charactersInString(";", Me.OpenArgs)

If intNumberOfParameters > 0 Then
ReDim aOpenParameters(intNumberOfParameters, 2)
For i = 0 To intNumberOfParameters - 1
aOpenArgsDetail = Split(aOpenArgs(i), "=")
aOpenParameters(i, 0) = aOpenArgsDetail(0)
aOpenParameters(i, 1) = aOpenArgsDetail(1)
Next i
End If

Call setParameterVariable("mPrintLoadListHeaderInformation")

End Sub
Sub setParameterVariable(strParameter)

Dim i As Integer

For i = 0 To intNumberOfParameters
If aOpenParameters(i, 0) = strParameter Then
Eval(strParameter) = aOpenParameters(i, 1)
End If
Next i

End Sub
 
D

david

What you can do is have a Named Control - a text box, or a
label or a combo box or anything - on a form or report. Or you
can sometimes use a database property, or sometimes you use
a form or report property.

Or you can use the values without using variables by using the
generic function directly instead of copying to a variable:

'old code
'me.mycaption = mycaption
'set rs = db.openrecordset("select * from....

'new code
for n = 1 to 10 step 2
me.controls(form_string(n)) = form_string(n+1)
next

'or
'new code
me.mycaption = form_string("mycaption")
set rs = db.openrecordset( form_string("mySQL") )

(david)
 

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

Top