Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing multiple label captions in VBA

Status
Not open for further replies.

telephoto

Technical User
Nov 3, 2002
210
GB
Hi guys. I am now retired and a bit rusty..

I am trying to set up a for:next loop to update several checkbox labels.

The following code should (I thought) change a set of 3 labels (named chklabel1 to chklabel3)to read one, two and three.

[tab]Dim var1 As String
[tab]Dim var2 As String
[tab]Dim var3 As String
[tab]Dim tempcontrol As String

[tab]var1 = "one"
[tab]var2 = "two"
[tab]var3 = "three"

[tab]For n = 1 To 3

[tab][tab]tempcontrol = "chklabel" & n

[tab][tab]Set tempcontrol = Label

[tab][tab]tempcontrol.Caption = var1

[tab]Next n


This refuses to compile at the line "Set tempcontrol = Label" and states that it wants an object.

Now I'm struggling. Anyone able to help on the correct syntax/code please?

(Access 2000, the form name is "frmtest")

Telephoto
 
I would suggest:

Code:
    Forms!frmF.chklabel1.Caption = "one"
    Forms!frmF.chklabel2.Caption = "two"
    Forms!frmF.chklabel3.Caption = "three"

There are several problems with your code, not least being:

tempcontrol.Caption = var1

If the rest of the code was correct, this would set all three controls to the same name.
 
Dim var(3) As String
var(1) = "one"
var(2) = "two"
var(3) = "three"
For n = 1 To 3
Me("chklabel" & n).Caption = var(i)
Next n

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks Remou for the prompt reply,

yes they would all be "one" - but I'm still working on the code (var & n)

Your three line reply is obviously correct, but the background to this is much wider and I wanted to set up an array and then step through multiple labels on multiple forms, using the for next loop, with n set to a record count from an SQL statement. I would give you the full background, but that would take too long.

This bit of code is a test before getting much larger
 
Thanks PHV, that's cracked that part of the project.

Should read Caption = var(n)
 
How are ya telephoto . . .

Here's an example without the array:
Code:
[blue]   Dim [purple][b]x[/b][/purple] As Integer

   For [purple][b]x[/b][/purple] = 1 To 3
      Me("chklabel" & [purple][b]x[/b][/purple]).Caption = Choose([purple][b]x[/b][/purple], "one", "two", "three")
   Next[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Aceman- that is a code option I did not know existed.

It works fine in this example (of course)

All I need to do now is sort out how to get the results of a select SQL into that format!

T
 
Have a look at the DLookUp function.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top