Rolling Constants

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have several constants in my app.

Public Const Piece1_X as Integer
Public Const Piece2_X as Integer
Public Const Piece3_X as Integer
Public Const Piece4_X as Integer
Public Const Piece5_X as Integer

How can I call these in a form using a For..Next statment?

For intX = 1 to 5
Me("bxBox" & intX).Top = bxMajorBox.Top + ???????
Next intX
 
constants should be...

Public Const Piece1_X as Integer = 1
Public Const Piece2_X as Integer = 2
Public Const Piece3_X as Integer = 3
Public Const Piece4_X as Integer = 4
Public Const Piece5_X as Integer = 5
 
Hi Jeff,

I don't think VBA offers a remotely elegant way of doing this. Instead,
I'd replace the constants with a public array variable, one element for
each of the existing constants.

Or maybe with entries in a "settings" table, retrieved by opening and
iterating through a recordset.

On Mon, 11 Jul 2005 21:11:01 -0700, Jeff R <Jeff
 
In my example I can roll thru objects on a form or an array. Just wish there
was a way to roll thru constants with similar names. Will have to ponder this
further for the current work around seems like alot of wasted time.

Reading each constant thru a Select Case statement into an array then
rolling thru that array. Seems like it would be easier to just call the
constant.

ie. bxMyBox(intX).Left = bxMajorBox.Left + conBox(intX)Left

or something like that.

T.I.A.
 
Seems like it would be easier to just call the
constant.

Yes, it would be easier if it were possible. However, I agree with John
in that I don't think it can be done, at least not in any
straightforward way.

The Eval() function does something similar to what you describe, but it
does not return a constant (or variable) value if the string evaluates
to the corresponding name. IOW,

Eval("Const Piece" & intX & "_X)

does not work. Simply changing your constant declarations to array
assignments, as in

Piece_X(5) = 5

seems like a perfectly reasonable workaround.
 
This is a clear misuse of constants. Two possibliities come to mind. As
John suggested, an array may do what you want. Also, a function to return
the value you need could do it.
 
Back
Top