Question on Variables

Trachr

Registered User.
Local time
Today, 13:45
Joined
Jun 1, 2014
Messages
90
Ok this is probably a stupid question and not something Id want to do everyday but Ill be reusing some snippets of code a lot with a few key things changed here and there, so I was considering just making the parts that change variables... This in itself isint hard however theres 1 spot Im not sure if its possible.

Lets see if I can explain it right.... Can you make a variable that points to another variable... Im using my code numerous times but each uses a differnt variable that is set on form load

So say I have Var1 Var2 Var3 ....

Can I make a variable that would allow me to define which variable Im messing with once in the code

So in otherwords it would read like this:

Code:
strOriginalName = strNameLvlOne
strNameLvlOne = DLookup("[" & tblField1 & "]", "[" & tblName & "]", "[" & tblID & "] = 1")
Me.NameLvlOne.Caption = strNameLvlOne

But if I wanted to make a variable like: strvartmp = "strNameLvlOne"

Is there a way to substitue strvartmp into that code easily so it reads like my code above when the code is executed.

Sorry if that was unclear, its kinda hard to explain, the idea is so I can reuse that code many times with only having to mess with 1 word at the beginning instead of having to pick through the code and find each instance of a variable and change it.
 
Set up arguments on the functions or subs and pass the variables to it.
 
hmm time to learn something new lol

Never done this before :)
 
It's an interesting problem :)

In C++ you can do this:
Code:
int A;
int& B = A;
In doing so, B refers to A meaning they share the same address and whatever is assigned to B will change A, either. I don't know how to do it in VBA.

What I know is you can define arguments in Sub/Function that will take a copy of an argument or refer to it. Ignore me if you know that already!!! E.g.

Code:
Option Compare Database
Option Explicit


Public Sub testOfVariables(ByVal varA As Integer, ByRef varB As Integer)
    varA = 20
    varB = 10
End Sub

Public Sub whichChange()
    Dim A As Integer
    Dim B As Integer
    
    A = 1
    B = 2
    
    Debug.Print A, B
    
    Call testOfVariables(A, B)
    
    Debug.Print A, B
End Sub
gives you this result:
Code:
 1             2 
 1             10
A is not changed because it is passed to Sub using ByVal which tells VBA to take a copy of the variable A. Any changes inside the Sub don't affect A. On the other hand, B is passed by reference, or its address is passed if you like. Any changes to varB inside the Sub will affect B.

Interestingly, VBA uses ByRef by default :)
 
I don't think I understand what Trachr is trying to do. Can you explain the full picture? Why do you need to know the name of a variable?
 
its not really needed just was trying to find out if it was possible for both my learning and cause it would be convenient

Im reusing some snippets of code a few times and the only thing that would change from time to time would be the variable name

So I was trying to find out if one of two things were possible.... Set up a exchange the variable names with a different variable that I could set at the onset of the snippet that the variable in the snippet could point to that variable, which would point to a different variable ... if that makes sense

The other Idea I had which I know some languages allow but from what Ive found so far VB dosent is a variable in a variable name For instance Example + strVarA = Something
so that it would splice the 2 together so that I could change StrVarA to different values allowing me to set a completly different variable in that example line.

But from all Ive read dynamic variables are impossible in VB


Lol dunno if I explained myself any better this time
 
Set up a exchange the variable names with a different variable that I could set at the onset of the snippet that the variable in the snippet could point to that variable,

Yes. This is done with function or sub arguments.

The other Idea I had which I know some languages allow but from what Ive found so far VB dosent is a variable in a variable name For instance Example + strVarA = Something
so that it would splice the 2 together so that I could change StrVarA to different values allowing me to set a completly different variable in that example line.

The nearest thing to this in VBA is the Collection.

An item in a Collection can be referred to as:
CollectionName(StringVariable)

Another alternative would be an array:
ArrayName(IntegerVariable)

Either of these would be more appropriate than concatening strings to make variable names.
 
which way would you reccomend learning first? I'll be honest Im releativly new with VB so Im learning as I go... I know a few other languages so I pick things up as I go but I still need to learn lol

I of course plan on learning both methods since Im sure it will help me later but just curious which you think I should start with.

Learning VB is partly why I am doing so much in VB for my database, if any of you have seen any of my other posts... I always try to learn as much as possible as I go.
 

Users who are viewing this thread

Back
Top Bottom