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

How to create an array of button?

Status
Not open for further replies.

mingichu

MIS
Apr 12, 2002
29
US
I'd like to change several command buttons' captions based on the values in the tables. I'd like to create an array of buttons, 'cause the count of the values are dynamic. When I copied the first created button named "cmd(1)" and pasted to the form, it didn't create something named like "cmd(2)", but a non-array "CommandButton4". Does anyone know how to do it?

Thanks!!

 
I think you can create an array of controls but I don't think Access will do it for you automagically like Visual Basic. JHall
 
That is correct. You can not create control arrays in Access as you can in VB.
Pat B
 
While you can't create a true control array like VB, you can do something like this:


Dim cmd(1 To 4) As Control
Dim i As Integer

Set cmd(1) = Me.Command0
Set cmd(2) = Me.Command1
Set cmd(3) = Me.Command2
Set cmd(4) = Me.Command3

For i = 1 To 4
cmd(i).Caption = "Test" & (i)
Next i Jim Lunde
compugeeks@hotmail.com
We all agree your theory is crazy, but is it crazy enough?
 
Thanks for your answer!!! I tried "Me" solution and it worked well in Access 97.
However, there is another issue in Access 2000. I recreated this database in Access 2000 and copied the same codes to the new mdb. I got runtime error "Type mismatch" in this statement "Set MyRs = CurrentDb().OpenRecordset(MySql) ". Access 2000 doesn't have "Database" datatype. How am I going to open the recordset in Access 2000. Thanks again!!!

Below is my codes:
=========================
Private Sub Form_Load()
Dim MyRs As Recordset
Dim MySql As String
MySql = "SELECT Menutable.category FROM Menutable;"
Set MyRs = CurrentDb().OpenRecordset(MySql)
MyRs.MoveFirst
x = 1
y = 30 'maximun 30 buttons
Do While (MyRs.BOF = False And MyRs.EOF = False) And x < y + 1
Me(&quot;command&quot; & x).Caption = MyRs!Category
x = x + 1
MyRs.MoveNext
Loop
End Sub
 
Explicitly define Dim MyRs As DAO.Recordset

Make sure you have a reference set to the DAO library.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top