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

Need help please with Checkboxes on Userform w/VBA

Status
Not open for further replies.

Technyc2003

IS-IT--Management
Feb 10, 2004
39
US
I've created a template form for my co-workers to be able to fill out easier by using VBA. The textfields work ok it's just the checkboxes. The form is also protected from the little lock on the forms toolbar. I created an OK command button so once they fill out all the boxes and text boxes it will insert the info into the form. Here's the code for the OK command button.

I have 4 check boxes that are not placed in tables. They are:

1) New Admission
2) Referral
3) Transfer
4) Discharged

I also named the Bookmarks for each checkbox according to those names.


------------------------------------
Private Sub CmdOK_Click()

' Unprotect doc to enter patient data

Call pToggleProtectDoc

Application.ScreenUpdating = False

With ActiveDocument


If NewAdmission = True Then
ActiveDocument.Bookmarks("NewAdmission").Result = True
' I get an error message on the .Result when I run this macro.


.Bookmarks("txtPatientName").Range.Text = txtPatientName.Value
.Bookmarks("txtPickup").Range.Text = txtPickup.Value
.Bookmarks("txtDosage").Range.Text = txtDosage.Value
.Bookmarks("txtIncDecDate").Range.Text = txtIncDecDate.Value
.Bookmarks("txtIDNO").Range.Text = txtIDNO.Value
.Bookmarks("txtDOA").Range.Text = txtDOA.Value
.Bookmarks("txtDatePrep").Range.Text = txtDatePrep.Value

End With

Application.ScreenUpdating = True

' Additional option code snippet
Call pToggleProtectDoc

' gets rid of this form
UserForm1.Hide

Unload Me

End Sub

-----------------------------------

Please any help is greatly appreciated. I've been stuck on this for a few days now.

Note: I'm using the checkboxes from the Forms toolbar because this is a complex form and am utilizing table designs for other checkboxes I have in place.

I cannot utilized the checkbox from the control toolbox since it distored my form's layout.
---------------------------------
 
If it is checkbox from the Forms toolbar, the syntax is:

ActiveDocument.FormFields("NewAdmission").Result = True

While it is true that formfields are also bookmarks (that is, they show up as bookmarks), they are independent objects.

In fact if you are using text formfields, it is better to use:

ActiveDocument.FormFields("txtPatientName").Result =....
..oh, wait a minute...are these textboxes on a UserForm???

And are you taking the info from the UserForm, and putting them into the bookmark ranges? That will work, but are you aware that replacing the Range.Text will, in fact insert the text, but destroys the bookmark?

Gerry
 
It destroys the bookmark but it creates a new document which is what I want to happen since they are working from the original .DOT template form.

for some reason the code you provided still does not add a checkmark in the New Admission checkbox.

ActiveDocument.FormFields("NewAdmission").Result = True

All the textboxes do fill in correctly though just not the check boxes.

Here's the entire code for the Userform 1.
-----------------------------------------
Private Sub cmdCancel_Click()

Unload Me

ActiveDocument.Close SaveChanges:=False

End Sub
----------------------------------------------
Private Sub cmdClear_Click()

' NewAdmit.Value = True


End Sub
----------------------------------------------

Private Sub CmdOK_Click()

' Unprotect doc to enter patient data (OLD CODE)
' Call pToggleProtectDoc (OLD CODE)
' Application.ScreenUpdating = False (OLD CODE)

With ActiveDocument


.FormFields("NewAdmission").Result = True
.Bookmarks("txtPatientName").Range.Text = txtPatientName.Value
.Bookmarks("txtPickup").Range.Text = txtPickup.Value
.Bookmarks("txtDosage").Range.Text = txtDosage.Value
.Bookmarks("txtIncDecDate").Range.Text = txtIncDecDate.Value
.Bookmarks("txtIDNO").Range.Text = txtIDNO.Value
.Bookmarks("txtDOA").Range.Text = txtDOA.Value
.Bookmarks("txtDatePrep").Range.Text = txtDatePrep.Value

End With


pToggleProtectDoc
UserForm1.Hide
Unload Me

' Application.ScreenUpdating = True (OLD CODE)

' Additional option code snippet
' Call pToggleProtectDoc (OLD CODE)

' gets rid of this form
' UserForm1.Hide (OLD CODE)

' Unload Me

End Sub
------------------------------------------------------
Private Sub UserForm_Initialize()

NewAdmit.Value = True

End Sub
-----------------------------------------------------

Sub pToggleProtectDoc()

' courtesy code from the net, thank you!
' this code toggles the form protection.
' If it's on, it turns it off.
' It it's off, it turns it on. But
' rather than using the menu option,
' this code relocks the form without
' clearing the form fields (.NoReset),
' as they normally would if you
' clicked protect manually while testing!

' first I declare the word DOC
' to represent the active doc cos'
' I'm just too lazy to keep typing
' ActiveDocument and check for
' form protection

Set Doc = ActiveDocument
If Doc.ProtectionType = wdNoProtection Then

' if the form is password protected, then
' you also need to add the argument below
' Password:="myPasswordName",

Doc.Protect Password:="xp50", NoReset:=True, Type:=wdAllowOnlyFormFields
Else
Doc.Unprotect Password:="xp50"

' remember to add the password argument
' in the above line, too, if there is one.

End If

End Sub
-------------------------------------------------------

Any other options that I can try?
 
After several hours searching I finally got it working. I didn't realize I had to create a code for a checkbox command click. Well this is what I used and it finally places checkmarks in the boxes. This worked while the form is unprotected. I haven't tried it yet with the form protected.
--------------------------------
Private Sub NewAdmit_Click()

If NewAdmit.Value = True Then
With ActiveDocument.FormFields("NewAdmit")
With .CheckBox
.Default = True
End With
End With

End If

End Sub
-------------------------------
 
Well I am glad you have it working. I have to say this though...having users working with the original template file is very bad form. It totally defeats the purpose of making a .dot file (a template). If you have users opening the template file itself, then you may as well not bother with a .dot file. It is completely pointless. You may as well use a .doc file.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top