Variable Name Referencing

grinnZ

Registered User.
Local time
Today, 03:47
Joined
Jun 29, 2011
Messages
38
I recently posted a question about control name referencing. The answer given was...
Code:
me.controls("string" & textboxvalue)
Now I'm wondering if I can't do the same referring to the variable itself...
Code:
Sub Blahblah()

Dim intDur48 as Integer
Dim intDur24 as Integer
Dim intDur12 as Integer

("intDur" & !Duration) = ("intDur" & !Duration) + 1
Thanks for the help!
 
this does not make sense:

Code:
Dim intDur12 as Integer

("intDur" & !Duration) = ("intDur" & !Duration) + 1

and the reason why is two fold:

1) in general, you can't concat two different data types
2) "intdur" is a hard coded string, whereas INTDUR12 is a variable.

if you want to concat two string together and one of the values is inside an INTDUR12 integer variable, you have to convert that variable like so:

Code:
Dim intDur12 as Integer

(cstr(intDur12) & !Duration) = (cstr(intDur12) & !Duration) + 1
 
OK, let me make a bit more sense of this for you. Yes, intDur12 is a variable as is intDur24 and intDur48. Duration is a combobox or a table field where valid values are 12, 24, or 48. I'm doing a counting operation where the 12hr duration count is stored in intDur12, the 24hr duration in intDur24, and so on. Therefore what I want to do is to build refer to the proper variable based on the duration. Much like a control reference is built with

Code:
me.controls("txtDur" & cboDuration)
I would like to do the same only referring to the already declared variable name on the fly in the same fashion. Therefore my variable names intDur12, intDur24, and intDur48.. I would like to build them on the fly and use them in a counting operation.

"intDur" & me.cboDuration ... I would like the result to be one of my three variable names

I would like to use it in a statement like this...

Code:
("intDur" & cstr(me.cboDuration)) = ("intDur" & cstr(me.cboDuration)) + 1
I hope my intent is now a bit clearer. Is what I want to do possible?
 
of course you can do this:

Code:
"intDur" & cstr(me.cboDuration)) = ("intDur" & cstr(me.cboDuration)) + 1

so do it. combo box or variable, it doesn't matter. the value ends up in the statement either way. what else do you want to know?? HOW to do the counting operation on the form? if that's what you want, you're not providing enough info for help on that. sorry sir!
 
The counting operation I can handle. Thank you for your assistance.
 
>>1) in general, you can't concat two different data types.<<

A correct starting premise would have been nice:-
Code:
Sub TestIt()
    Dim MyString As String
    Dim Duration As Long
    Dim MyDate   As Date

    MyString = "intDur"
    Duration = 48
    MsgBox MyString & Duration      [color=green]' << intDur48[/color]

    MyString = "intDur"
    MyDate = #1/26/2011#
    MsgBox MyString & MyDate        [color=green]' << intDur26/01/2011[/color]

End Sub

Chris.
 
Code:
("intdur" & cstr(rstauctions!Duration)) = ("intdur" & cstr(rstauctions!Duration))+ 1
OK... here is the exact line of code. To it i'm getting a compile error with the message... Expected: Line number or label or statement or end of statement. In the help for that error it speaks about invalid parens to call a sub and restricted words as part of a variable.
 
Maybe...
Code:
Dim intDur(1 To 4) As Integer


Sub Blahblah()
    Dim Duration As Integer
    
    Duration = 48
    
    intDur(Duration \ 12) = intDur(Duration \ 12) + 1
    
    MsgBox intDur(Duration \ 12)

End Sub

Chris.
 
This is cleaner and probably consumes less memory overall:-
Code:
Dim intDur(12 To 48) As Integer


Sub Blahblah()
    Dim Duration As Integer
    
    Duration = 12
    
    intDur(Duration) = intDur(Duration) + 1
    
    MsgBox intDur(Duration)

End Sub

Chris.
 
Chris...

Just what the doctor ordered! Thank you!!!
 

Users who are viewing this thread

Back
Top Bottom