Setting checkbox properties with code?

  • Thread starter Thread starter Stephen Glynn
  • Start date Start date
S

Stephen Glynn

I'm shortly going to have to create a form that contains a lot of
unbound check boxes. Each box will have its Tag property set to a
particular RecordID in a lookup table and both its Name and Label based
on the RecordDescription in that table. The user can then click on the
appropriate boxes and then run an append query to add the selected
values (along with things like CustomerID and so on) to another table.

It does have to be check boxes.

Is there a way to set these properties automatically before I start
positioning the boxes on my form or do I have to create all the boxes
manually and then manually set their tag properties etc one by one?

Steve
 
Hi Stephen,

You can write code that opens the form in design view and manipulates or
adds controls. This is emphatically not recommended as part of the
functioning of an application, but is fine as a one-off design-time
operation. Basic example (you'd save and close the form manually
afterwards):

Dim C As Access.CheckBox

DoCmd.OpenForm "frmTemp1", acDesign
Set F = Forms("frmTemp1")
Set C = CreateControl("frmTemp1", acCheckBox, acDetail, , , 100, 100)
C.Name = "ck1"
C.Tag = "ckOne"

To create multiple controls you just need write a loop that calculates
their names, positions and tags.
 
Thanks, John. Could you give me a bit more guidance, please?

I'm going to have to select the RecordID and RecordName from a table and
use them to set C.Tag and C.Name. How do I do that and loop it round?

Steve
 
Open a recordset (rsR) on the table and do something like this to loop
through it:

Const TOP_INCREMENT = "200"
Dim lngTop As Long
Dim lngLeft As Long
Dim lngCounter As Long
'other declarations assumed

lngCounter = 0
Do Until rsR.EOF
Set C = CreateControl("frmTemp1", acCheckBox, _
acDetail, , , 200, 200)
C.Tag = rsR.Fields("RecordID").Value
C.Name = rsr.Fields("RecordName").Value
C.Top = C.Top + lngCounter * TOP_INCREMENT
lngCounter = lngCounter + 1
.MoveNext
Loop
Set C = Nothing

If there are too many controls to fit in a vertical column on your form,
you'll need to do something else to adjust the Top and Left property of
each so they end up in positions from where you can drag them to where
you need them. Or - if your table contains x and y coordinates for where
you want each checkbox to go - you could calculate the corresponding Top
and Left values in your code to potisiton them automatically.
 
Back
Top