Setting control properties through code

K

Kevin Hughes

Greetings all:

Having an issue with updating a long list of properties through code. When
done, this form is going to have about 80 command buttons, where the
captions for each are going to be supplied from fields in a database where
each of a few hundred users have customized the records, so obviously I must
do a dynamic update for each one.

Trouble is, I can't seem to figure out how to address the property to
update. I have a string expression that evaluates to the property name, but
Access has defied all my efforts to change the property's value. I've set
individual properties through code before, but how does one use a string to
represent the property name, so that one can update multiple properties
through a loop??

Here's the code stub I'm working with, just wanted to get it to change the
caption on 20 command buttons to the numbers 1-20 for now, the rest of the
code I'll be fine with:

Private Sub Form_Open(Cancel As Integer)
Dim strcount As String
Dim str1 As String
Dim str2 As String
Dim str3 As String

For x = 1 To 20
str1 = "Forms!Form1!Command"
str2 = Trim(Str(x))
str3 = ".Caption"
strcount = str1 + str2 + str3
MsgBox (strcount) <== this expression is correct
Eval(strcount) = (Str(x)) <== assignment fails
Next x
End Sub

This is in 2K, but I need a 97 answer as well, some customers won't/don't
change or update.

Thanks in advance for rescuing an infrequent programmer from one of access'
lovely quirks:
Kevin
 
R

Rodrigo

try this.

assuming you have 20 command buttons. Their names begin with Btn and are all
numeric (btn1, btn2, btn3, etc). This will go from 1 to 20, and will change
their caption to test for i (with i the current number on the loop). The
rest I think you understand

If this is not what you are looking for, let me know.

Rodrigo.

sub changesomeproperties()
Dim ctl As Control
dim i as long
dim j as long

for i = 1 to 20
Set ctl = Me("btn" & i )
ctl.Properties("caption") = "test for " & i
clt.properties("Value") = "Value for " & i
clt.properties("Visible") = True ' or false
clt.properties("tag") = "somevalue for your tag" & i
Set ctl = Nothing
next i

end sub
 
D

dan artuso

Hi,
Why do you want the property to be a variable?
Will you be updating other properties as well as the Caption?
If this is to update 20 captions on a form:

For x = 1 To 20
Me("Command" & x).Caption = x
Next x
 
T

TC

Thanks in advance for rescuing an infrequent programmer from one of
access'
lovely quirks

There are no "quirks" involved here - lovely or otherwise! You have written
code that doesn't make sense. Access quite properly refuses to run it.

just wanted to get it to change the caption on 20 command buttons
to the numbers 1-20 for now

If the command button controls are named cmd1 to cmd20, this will do that
job:

dim n as integer
for n = 1 to 20
me("cmd" & n).caption = n
next

HTH,
TC
 
T

TC

clt.properties("Value") = "Value for " & i

Command button values are booleans - not strings!

TC
 
M

Marshall Barton

Kevin said:
Greetings all:

Having an issue with updating a long list of properties through code. When
done, this form is going to have about 80 command buttons, where the
captions for each are going to be supplied from fields in a database where
each of a few hundred users have customized the records, so obviously I must
do a dynamic update for each one.

Trouble is, I can't seem to figure out how to address the property to
update. I have a string expression that evaluates to the property name, but
Access has defied all my efforts to change the property's value. I've set
individual properties through code before, but how does one use a string to
represent the property name, so that one can update multiple properties
through a loop??

Here's the code stub I'm working with, just wanted to get it to change the
caption on 20 command buttons to the numbers 1-20 for now, the rest of the
code I'll be fine with:

Private Sub Form_Open(Cancel As Integer)
Dim strcount As String
Dim str1 As String
Dim str2 As String
Dim str3 As String

For x = 1 To 20
str1 = "Forms!Form1!Command"
str2 = Trim(Str(x))
str3 = ".Caption"
strcount = str1 + str2 + str3
MsgBox (strcount) <== this expression is correct
Eval(strcount) = (Str(x)) <== assignment fails
Next x
End Sub

This is in 2K, but I need a 97 answer as well, some customers won't/don't
change or update.

Thanks in advance for rescuing an infrequent programmer from one of access'
lovely quirks:


No Quirks here. The full syntax of a property reference is:

Forms(strformname).Controls(strcontrolname).Properties(strpropname)
 
T

TC

No it wasn't! :)

You said: "assuming you have 20 command buttons. Their names begin with Btn
and are all numeric (btn1, btn2, btn3, etc)." Your code then said:

"clt" is clearly just a typo. Your code will try to set the Value property
of command button btn1 (for example) to: "Value for 1".

Cheers,
TC
 
R

Rodrigo

Yes I know. I forgot to add the ;-) to my last post....
I just copy pasted some code from my form. I don't have any command buttons,
just labels and text boxes. I forgot to add that there. Thats what happens
when i start typing without drinking coffe yet.

Rodrigo.
 
K

Kevin H

dan artuso said:
Hi,
Why do you want the property to be a variable?
Will you be updating other properties as well as the Caption?
If this is to update 20 captions on a form:

For x = 1 To 20
Me("Command" & x).Caption = x
Next x

Dan:

Thanks. Was one of those forest for the trees deals. Was building expression
for the control name as a text string ... then wanting to have it evaluated
as literal, whereas building it literal in the first place would have worked
just fine. Decided, as usual, to make it a harder problem than it needed to
be:

Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top