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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assigning captions to buttons from a name field in a table

Status
Not open for further replies.

lewie

Technical User
Jan 17, 2003
94
US
I numbered my buttons btn_1 btn_2 ect.
I was wondering how to loop through them using a for loop.
i have tried the below but keep getting errors:
Private Sub Form_Open(Cancel As Integer)
Dim rst As ADODB.Recordset
Dim ID As Integer
Dim big As Field
' Dim endoffile As Integer
' Dim x As Integer
' On Error GoTo HandleErrors
Set rst = New ADODB.Recordset
rst.Open "main", CurrentProject.Connection, adOpenKeyset, options:=adCmdTableDirect
' rst.MoveLast
' endoffile = rst.RecordCount - 1
' rst.MoveFirst

For ID = 1 To 3
big.name.Value = "btn_" & ID
Form_Form1!big.Caption = rst.Fields("name")
Next

rst.Close
Set rst = Nothing
End Sub

as you can see my syntax needs work. I need to concatonate the btn_ and the ID to bring up each button.
Any help will be greatly appreciated.
Thanks Lewie
 
Something like this ?
Form_Form1("btn_" & ID).Caption

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thats where i got to but it doesn't like the quotes.
is there a type of variable i could assign it to so the quotes wouldn't be there. form_form1.btn_1.caption gets me what i want but when i make a string var and assign
dim var as string
var = "btn_"&ID
when i sub the var in form!var.caption the quotes stay with it and screw it up. form!"btn_1".caption
do i have to make var a field type variable?
Thanks
Lewie
 
Have you read my post ?
Instead of this:
form!var.caption
Try this:
form(var).caption

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
The parens didn't help. I ended up assigning a ctl var.
Dim ctl As Control
Dim big As String
Set frm = Forms("form1")
' Dim endoffile As Integer
' Dim x As Integer
' On Error GoTo HandleErrors
Set rst = New ADODB.Recordset
rst.Open "main", CurrentProject.Connection, adOpenKeyset, options:=adCmdTableDirect
' rst.MoveLast
' endoffile = rst.RecordCount - 1
' rst.MoveFirst
For ID = 1 To 3
big = ("btn" & ID)
Set ctl = frm.Controls(big)
Do While Not rst.EOF
If rst.Fields("btn_name") = big Then
ctl.Caption = rst.Fields("name")
rst.MoveLast
End If
rst.MoveNext
Loop
rst.MoveFirst
Next
rst.Close
Set rst = Nothing
End Sub
Thanks for all your help
Lewie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top