Selecting Checkboxes programmatically?!

W

Webtechie

Hello,

I want to save a lot of options into an access database. I then want to
select certain checkboxes depending on a field value.

I tried to record a macro selecting the checkboxes, but couldn't.

How do you select a checkbox via VBA? How do you set it to be
checked/unchecked?

Thanks.


Tony
 
G

Gordon Bentley-Mix on news.microsoft.com

Something like this - which is a Public procedure that I have in a 'Tools'
module that I include in almost every project that I do that involves form
fields - could probably be adapted for use:

Public Sub SetCheckboxValue(BkmkName As String, Optional myValue As Boolean
= True)
With ActiveDocument
If .Bookmarks.Exists(BkmkName) Then
With .FormFields(BkmkName)
If .Type = wdFieldFormCheckBox Then .CheckBox.Value = myValue
End With
End If
End With
End Sub

(Just be careful of the line breaks that the NG software introduces.)

Calling it is then just a matter of using something like:

SetCheckboxValue "Check1", cBool(Checkbox1.Value)

where "Check1" is the name of the bookmark associated with a checkbox form
field and "Checkbox1" is the name of a CheckBox control on a UserForm.

Note that the use of the cBool function isn't absolutely necessary as the
compiler is pretty good about doing the conversion of the .Value property of
a CheckBox control to a Boolean value, but I'm a "belt-n-braces" kinda guy...
;-P
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top