Assembling a variable in a procedure. 
Author Message
 Assembling a variable in a procedure.

Hello everyone. This is a problem I had for the longest time and was never
able to solve. Since it's harder to explain than illustrate the procedure
below show it clearly. In my VBA Class, I want to show an example of
For-Next loop. I would like to show this one, although definitively not
complete, there is enough here to illustrate my question.

I have four string variables. strQ1 to strQ4. There is a reason for this
sequence of variables. Each variable is assigned a message (see below).
Later in the program, I want to use a For-Next loop to display each message
from 1 to 4. So I use InputBox("strQ" & intLoop) thinking that it would turn
into InputBox(strQ1) which is the first variable name. Then it would go on
with the next on strQ2, etc. I know this could be done using an array or by
reading the question from a table. But for the purpose of the class, I would
really like if it could be done with the tools at hand here.

When I use it this way the input box appears with strQ1 displayed, I would
like the string assigned to that variable to appear instead. I tried using
Eval for some reason (not sure why but it made sense to me) but to no avail.
Something can be added here, I'm sure, but what. I've successfully did
something like this with objects on a form named sequentially. Instead of
variable, they were objects and it worked fine. Anybody with a suggestion?
Thanks in advance.

Sub Quiz()
' This function shows how to use the InputBox
' function to ask question to the user
' and report the number of correct answers.

    Dim strQ1 As String, strQ2 As String
    Dim strQ3 As String, strQ4 As String
    Dim UserFName As String
    Dim strAnswer As String, intCounter As Integer
    Dim intLoop As Integer

    strQ1 = "B.Bethoven was a:" & vbLf & "A) A politician." & vbLf & _
            "B) A composer." & vbLf & "C) A priest."
    strQ2 = "B.Wilbur Smith is a:" & vbLf & "A) A Chemist." & vbLf & _
            "B) A writer." & vbLf & "C) A Astronaut"
    strQ3 = "C.A tetrahedron is made of:" & vbLf & "A) 12 faces." & _
            "B) 6 faces." & vbLf & "C) 4 faces."
    strQ4 = "B.Miles Davis plays the:" & vbLf & "A) Piano." & vbLf & _
            "B) Saxophone." & vbLf & "C) Guitar."

    For intLoop = 1 To 4
        strAnswer = InputBox("strQ" & intLoop)
    Next intLoop
End Sub

Daniel Lamarche



Fri, 08 Nov 2002 03:00:00 GMT  
 Assembling a variable in a procedure.
Daniel,

There's no way you can refer to a variable name in this way within Access.
The Eval() function would be useful if you were using functions rather than
string variables but, as you already seem to be aware, the best code-only
solution would be to use an array.  If you're interested in the Eval
solution, it would be something like this:

Sub Quiz()

    Dim intLoop As Integer

    For intLoop = 1 To 4
        strAnswer = InputBox(Eval("Q" & intLoop & "()"))
    Next intLoop

End Sub

Function Q1()

    Q1 = "B.Bethoven was a:" & vbLf & "A) A politician." & vbLf & _
            "B) A composer." & vbLf & "C) A priest."

End Function

Function Q2()

    Q2 = "B.Wilbur Smith is a:" & vbLf & "A) A Chemist." & vbLf & _
            "B) A writer." & vbLf & "C) A Astronaut"

End Function

Function Q3()

    Q3 = "C.A tetrahedron is made of:" & vbLf & "A) 12 faces." & vbLf & _
            "B) 6 faces." & vbLf & "C) 4 faces."

End Function

Function Q4()

    Q4 = "B.Miles Davis plays the:" & vbLf & "A) Piano." & vbLf & _
            "B) Saxophone." & vbLf & "C) Guitar."

End Function


Quote:
> Hello everyone. This is a problem I had for the longest time and was never
> able to solve. Since it's harder to explain than illustrate the procedure
> below show it clearly. In my VBA Class, I want to show an example of
> For-Next loop. I would like to show this one, although definitively not
> complete, there is enough here to illustrate my question.

> I have four string variables. strQ1 to strQ4. There is a reason for this
> sequence of variables. Each variable is assigned a message (see below).
> Later in the program, I want to use a For-Next loop to display each
message
> from 1 to 4. So I use InputBox("strQ" & intLoop) thinking that it would
turn
> into InputBox(strQ1) which is the first variable name. Then it would go on
> with the next on strQ2, etc. I know this could be done using an array or
by
> reading the question from a table. But for the purpose of the class, I
would
> really like if it could be done with the tools at hand here.

> When I use it this way the input box appears with strQ1 displayed, I would
> like the string assigned to that variable to appear instead. I tried using
> Eval for some reason (not sure why but it made sense to me) but to no
avail.
> Something can be added here, I'm sure, but what. I've successfully did
> something like this with objects on a form named sequentially. Instead of
> variable, they were objects and it worked fine. Anybody with a suggestion?
> Thanks in advance.

> Sub Quiz()
> ' This function shows how to use the InputBox
> ' function to ask question to the user
> ' and report the number of correct answers.

>     Dim strQ1 As String, strQ2 As String
>     Dim strQ3 As String, strQ4 As String
>     Dim UserFName As String
>     Dim strAnswer As String, intCounter As Integer
>     Dim intLoop As Integer

>     strQ1 = "B.Bethoven was a:" & vbLf & "A) A politician." & vbLf & _
>             "B) A composer." & vbLf & "C) A priest."
>     strQ2 = "B.Wilbur Smith is a:" & vbLf & "A) A Chemist." & vbLf & _
>             "B) A writer." & vbLf & "C) A Astronaut"
>     strQ3 = "C.A tetrahedron is made of:" & vbLf & "A) 12 faces." & _
>             "B) 6 faces." & vbLf & "C) 4 faces."
>     strQ4 = "B.Miles Davis plays the:" & vbLf & "A) Piano." & vbLf & _
>             "B) Saxophone." & vbLf & "C) Guitar."

>     For intLoop = 1 To 4
>         strAnswer = InputBox("strQ" & intLoop)
>     Next intLoop
> End Sub

> Daniel Lamarche



Fri, 08 Nov 2002 03:00:00 GMT  
 Assembling a variable in a procedure.


: Hello everyone. This is a problem I had for the longest time and was never
:     For intLoop = 1 To 4
:         strAnswer = InputBox("strQ" & intLoop)
:     Next intLoop

    For intLoop = 1 To 4
        Debug.Print InputBox(fGetQuestion("strQ" & intLoop))
    Next

Function fGetQuestion(strVar as String) As String
    fGetQuestion = strVar
End Function

 should work...

 -- Dev



Fri, 08 Nov 2002 03:00:00 GMT  
 Assembling a variable in a procedure.


Quote:



> : Hello everyone. This is a problem I had for the longest time and was
never
> :     For intLoop = 1 To 4
> :         strAnswer = InputBox("strQ" & intLoop)
> :     Next intLoop

>     For intLoop = 1 To 4
>         Debug.Print InputBox(fGetQuestion("strQ" & intLoop))
>     Next

> Function fGetQuestion(strVar as String) As String
>     fGetQuestion = strVar
> End Function

>  should work...

>  -- Dev

I find this a really interesting problem. By the way, Dev's solution will
work not either - I just pasted his solution into a module and tested it. I
didn't think it would, which is why I tested it. I understand the reason it
is not working. I looked at the help file for input box. The prompt and the
title are both defined there as STRING EXPERESSIONS. This means it evaluates
whatever it sees and tries to interpret it as a string. Although not stated
anywhere that I know of, arguements to functions can also be expressions of
the type defined by the function (or subroutine). Which makes sense because
your own function obeys a lot (or all?) of the same rules in this regard as
do the built-in ones.

So, what do you get when you evaluate this expression: "strQ" & intLoop ?
The string "strQ1". That string is what is passed to the function.

What you really would like it to do is to parse it one more time and get the
VALUE of the variable strQ1, but it won't because there isn't any way to
tell it to do that. It stops when it ends up with a simple term that looks
like a string and I don't know how to tell it to iterpret that string as a
variable name. And I have messed with Eval in oh so many ways, and I haven't
been able to figure out how to get it to interpret "strQ1" as the name of a
variable and not an expression. This:

        Debug.Print Eval("'str' & '1'")

will print "str1". But then again, so will

        debug.print "str" & "1"

so I haven't gotten very far, but I did need that strange nesting of
single-quotes inside the double-quotes. It's like, you feel there just gotta
be a way to do it, but if there is, to find it, you'd have to be mighty
clever. Calling all clever programmers, calling all clever programmers!

But try this instead. It is a lot like what you are after, not to say that
the other ones suggested aren't also good solution, but why not have a
choice.

Public Sub test()

Dim intI As Integer

intI = 1
Debug.Print fGetQuestion("str intI)

End Sub

Function fGetQuestion(strVar As String) As String

Select Case strVar

    Case "str1"
       fGetQuestion = "String 1"

    Case "str2"
       fGetQuestion = "String 2"

End Select

End Function

I tested the above and it works.

But this is NOT "assembling a variable in a procedure". Give the same
answer, does about the same thing, but doesn't really do it the way you want
it to. The techique does work for controls:

    Set rs = ....
    x = rs("Column" & intI)

because what is inside the parenthesis is defined to be a string expression
and it interprets the way you'd want it. I know this works because I've had
to do it recently.

So, there you go. I haven't a clue, but I had a whale of a time trying to
figgur this one out.

-- Paul Strauss



Sat, 09 Nov 2002 03:00:00 GMT  
 Assembling a variable in a procedure.
Thanks to Nicole and Paul for your kind reply and help about my question.
With these solutions on hand, I will never forget how to deal with this
issue now. Again, thank you very much.

Daniel Lamarche


Quote:
> Hello everyone. This is a problem I had for the longest time and was never
> able to solve. Since it's harder to explain than illustrate the procedure
> below show it clearly. In my VBA Class, I want to show an example of
> For-Next loop. I would like to show this one, although definitively not
> complete, there is enough here to illustrate my question.

> I have four string variables. strQ1 to strQ4. There is a reason for this
> sequence of variables. Each variable is assigned a message (see below).
> Later in the program, I want to use a For-Next loop to display each
message
> from 1 to 4. So I use InputBox("strQ" & intLoop) thinking that it would
turn
> into InputBox(strQ1) which is the first variable name. Then it would go on
> with the next on strQ2, etc. I know this could be done using an array or
by
> reading the question from a table. But for the purpose of the class, I
would
> really like if it could be done with the tools at hand here.

> When I use it this way the input box appears with strQ1 displayed, I would
> like the string assigned to that variable to appear instead. I tried using
> Eval for some reason (not sure why but it made sense to me) but to no
avail.
> Something can be added here, I'm sure, but what. I've successfully did
> something like this with objects on a form named sequentially. Instead of
> variable, they were objects and it worked fine. Anybody with a suggestion?
> Thanks in advance.

> Sub Quiz()
> ' This function shows how to use the InputBox
> ' function to ask question to the user
> ' and report the number of correct answers.

>     Dim strQ1 As String, strQ2 As String
>     Dim strQ3 As String, strQ4 As String
>     Dim UserFName As String
>     Dim strAnswer As String, intCounter As Integer
>     Dim intLoop As Integer

>     strQ1 = "B.Bethoven was a:" & vbLf & "A) A politician." & vbLf & _
>             "B) A composer." & vbLf & "C) A priest."
>     strQ2 = "B.Wilbur Smith is a:" & vbLf & "A) A Chemist." & vbLf & _
>             "B) A writer." & vbLf & "C) A Astronaut"
>     strQ3 = "C.A tetrahedron is made of:" & vbLf & "A) 12 faces." & _
>             "B) 6 faces." & vbLf & "C) 4 faces."
>     strQ4 = "B.Miles Davis plays the:" & vbLf & "A) Piano." & vbLf & _
>             "B) Saxophone." & vbLf & "C) Guitar."

>     For intLoop = 1 To 4
>         strAnswer = InputBox("strQ" & intLoop)
>     Next intLoop
> End Sub

> Daniel Lamarche



Sat, 09 Nov 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. referencing a local variable in the calling procedure from a nested procedure

2. Pass variable from procedure to procedure

3. HELP il, commands, assembling to il and disassembling from

4. Masm assemble compiler

5. Bound Controls... Assembling a field

6. Drawing and assembling of elements of a network

7. Assemble a Double from 8 bytes?

8. Breaking down and re-assembling files for internet downloads

9. I am looking suited for companies assembling blinds

10. Procedure Variables

11. call procedure using variable

12. run procedure assigned to variable

 

 
Powered by phpBB® Forum Software