I want to create an Access form cointaining 50 command buttons and I want to iterate through those buttons. Of course creating buttons by hand (using the tools in the ribbon) is not a solution. I want to create those buttons through a loop using vba code. How I can do that?
To add a form control you need just CreateControl(form.name, typeOfcontrol, ....)
so, the code would be similar to this:
Sub Add50Forms()
Dim db As DAO.Database
Dim frm As Form
Dim newBt As Control
Dim i As Long, j As Long
Set frm = Application.Forms(1)
For i = 0 To 1
For j = 1 To 25
Set newBt = CreateControl(frm.Name, acCommandButton, Left:=100 + 3000 * i, Top:=500 * j)
Set newBt = Nothing
Next j
Next i
End Sub
I have propesed two nested loops for simplicity, but it could be made with one and some fancy expressions for left:= and top:= named arguments. Of course, you would like to add some captions to those codes and so on. Positions of controls, their size and colours are also up to you.