How do I create buttons in VBA

A

Adam Thwaites

I have a table with a list of Agent states in it; Available, On The Phone,
Data Input, Post, Toilet, Break etc etc.

I want for my form to generate a toggle button for each of these when it's
loading;

recordset.movefirst
do until me.recordset.eof = true
'CREATE BUTTON
recordset.movenext
loop

So everytime a new Agent state is added, the form will automatically change.
How do I create a button in VBA?
 
D

Douglas J. Steele

In general, it's not a good idea to dynamically generate forms.

This is because forms have a limit of 754 controls and sections that can be
added over the lifetime of the form. If you're constantly adding controls
through code, at some point your form will cease to be able to add
additional controls. Far better would be to create a reasonable number of
buttons, and control their visibility through VBA.

If you're determined to create the buttons, take a look at the CreateControl
function in the Help file.
 
S

storrboy

You can only create controls in Access while the form or report is in
design view - which is not what you want to be doing at runtime.
Adding a bound option group to the form is probably closest to what
you are describing, but that would require the 'state' field to be
numerical, not text. If you can't afford to change the table so that
the 'state' is a number and not a string, then an option group would
still work, but it would need to be unbound and updated through code.

However to cover the addition of new states without a form design
change, a list box or combobox would be the most practical.
 
Top