Log in

View Full Version : Creating a variable with strings?



Feetd
11-21-2007, 01:52 AM
Hi,

What I'm trying to do is to create a variable, consisting of strings or partly of strings.

e.g. intAmountX = txtAmountX

has to be changed to code in which the string strFig replaces the "X" of the integer, so we can feed the integer with different values of strFig.

Ofcourse every possible value of strFig, gives a separately declared integer, such as

intAmountA
intAmountB etc. etc.

Does anyone have any idea if this is possible at all or any other suggestion?

Greetings
Feetd

asingh
11-21-2007, 04:26 AM
Hi,

Please give a few examples..of what you want...can try it out...

Feetd
11-21-2007, 01:11 PM
Hi, next message hopefully explains enough.

Feetd
11-21-2007, 01:23 PM
Hi,

To explain further, the following is what I would like to accomplish :

On my Form are five similar textboxes, txtAmountA till txtAmountE.
Every textbox has the same code for the GotFocus_Event

intAmountA = txtAmountA

If intAmountA = intCurrentValueAmountA Then
.......Code
Else
.......Code
End If

Both integers are declared in a Module.

What I would like to do is place this code in a procedure in a module which is called for in all five textbox GotFocus_Events. The only difference between all five Events are the characters "A" till "E". So I thought maybe it is possible to make use of a string such as strFigure, which is fed into the module with either "A" or "B" etc. and replace the integers by a newly build integer, including the string strFigure, with hopefully the same result.

Maybe this all clarifies my idea a bit.

Thx.

DarkSprout
11-21-2007, 02:10 PM
All variables A-E will now equal each other:=

Dim i As Integer

For i = 65 To 69
Eval("intAmount" & Chr(i) & " = txtAmount" & Chr(i))
Next i

Feetd
11-22-2007, 01:09 AM
All variables A-E will now equal each other:=

Dim i As Integer

For i = 65 To 69
Eval("intAmount" & Chr(i) & " = txtAmount" & Chr(i))
Next i



When I run the code it gives me a runtime error '2482':

Can't find the name 'intAmountA' you entered in the expression.

So within the Eval(), the declared integer intAmountA isn't recognized.

DarkSprout
11-22-2007, 05:24 AM
Ok, after a bit of finding out, for myself.

You Can't Evaluate a Variable.

But you could use an array to equal a loop of text boxes
i.e.


Private intAmount(5) As Integer

Private Sub TestEval()
Dim i As Integer, strCtl As String
For i = 1 To 5
strCtl = "Forms!Form1!txtAmount" & Chr(i + 64) ' Build Textbox Ref and Create A-e Chars
intAmount(i) = Val(Eval(strCtl))
Next i
End Sub



Sorry you can't use your intAmountA[-E]
Hope this helps.

OTWarrior
11-23-2007, 02:00 AM
Why not use an array and call the names of the textboxes?

are they formfield textboxes? if so you can use the index number:



Dim i As Integer
dim k as integer
dim txtResult as variable

txtResult = array("txtAmountA", "txtAmountB", "txtAmountC")
k = 0

For i = 65 To 69 'index values of textbox formfields

activedocument.formfields(i).result = txtResult(k)
k = k + 1
Next i


is that kinda what you are after?

TonyJollans
11-23-2007, 04:29 PM
You can get details of the textBox from ActiveControl ...

Private Sub txtAmountA_GotFocus()
Generic_GotFocus
End Sub
Private Sub txtAmountB_GotFocus()
Generic_GotFocus
End Sub
Private Sub Generic_GotFocus()
MsgBox ActiveControl.Name & vbTab & ActiveControl.Value
End Sub

but that is only half the battle :)

If you really can't use an array or some hard coding you *can* do the rest of what you want with some module level variables and CallByName but it's pretty inefficient.


Public intAmountA
Public intAmountB
' ...
Private Sub Generic_GotFocus()
MsgBox CallByName(Me, "intAmount" & Right(ActiveControl.Name, 1), VbGet)
End Sub