What is the best approach for generating a Word document based on a user selection via checkboxes? - vba

I am trying to create a form template to use at work that can be customized via check boxes.
So far, I have been thinking about adding check boxes before every paragraph and at the end a button.
When I click the button, my intention is to delete all the paragraphs that don't have the checkbox activated.
The problem is that I don't know which is the most user friendly approach to this.
I am thinking about making bookmarks for each paragraph and use an IF formula for each of the check boxes.
The most user friendly check boxes are the content control ones, but I don't know how to reference them in VBA code.
All I can find is about form field check boxes, but I don't know how to make them clickable.
Before I go studying about each of these two options, I am interested in finding out which of these two alternatives is more appropriate to fulfill my requirements.
Thank you!

I have managed to do what I intended.
First of all, I've put content control checkboxes before every paragraph and I've set a unique tag for each one.
Then, for each checkbox, I've selected the paragraph, including the checkbox, and added a bookmark named exactly like the checkbox. Next, I've selected only the checkbox and added a bookmark name hide_nameofthecheckbox.
I've added an ActiveX button, with the following vba Code on click:
Private Sub btnSubmit_Click()
Dim bookmark As String
Dim ctl As ContentControl
For Each ctl In ActiveDocument.ContentControls
If ctl.Type = wdContentControlCheckBox Then
If ctl.Checked = False Then
bookmark = ctl.Tag
Bookmarks(bookmark).Range.Font.Hidden = True
Else
bookmark = "hide_" & ctl.Tag
Bookmarks(bookmark).Range.Font.Hidden = True
' DO NOTHING
End If
End If
Next
End Sub
Basically, when I click the Submit button, the code verifies each checkbox for value True or False. If it is checked, it hides the checkbox, If it is unchecked, it hides the entire paragraph, including the checkbox.
This way, after I click the submit button, there will be no checkboxes visible, so the document is print-ready.

Related

Conditional visibility on MS Access Form - how to write in VBA or Macro

I have some very (very) basic MS Access knowledge. I'm trying to expand a bit into either VBA or macros as I'd like to put in some conditional visibility for my form. Basically, I have a checkbox. If it's checked, I want three or four more fields to pop up. Someone was able to point me to a basic VBA formula of if (this checkbox) = true then, (fieldx).visible = true, else, (fieldx).visibility = false, end if.
But I'm so new to this that I need more help and explanation. I tried putting it in but couldn't get it to work (no error message, just nothing changed at all).
Specific questions:
-Does this formula seem right?
-If I want multiple fields to be visible, can I combine them into one formula or should I create a new "if" statement for all?
-Where do I enter this code? I'm running the Office 365 version. For all I know, I'm not even putting it in the right place.
-How do I determine the field names to replace the (this checkbox) and (fieldx) in the formula? I tried entering the name I title the fields as, but with the spaces in the name I got an error message, and without the spaces nothing happened. Is there a specific naming convention to turn the field names into formula-appropriate titles? Is the name listed somewhere?
-Once I get the formula entered, is there something I have to do to get it to run/take effect? I tried saving, closing and reopening with no changes.
-Is this the best way to go about this?
If there's anything else you think I should know, I would love to hear it - but please keep in mind I'm very new to this so if you could keep it at "dummy" or ELI5 levels of explanation, I'd appreciate it!
after creating a form with 4 textboxes and a checkbox put the form in design mode (lower right corner has design mode selected, select a textbox and hit property sheet on the ribbon (or f4).
On the property sheet note the visible property. set the visible property to false. Now the textbox will be invisible when the form starts.
Tip you can select all the textboxes at the same time and set their properties all at once.
Every control on the form and even the various parts of the form have properties you can set and play with. For instance you can give any name you want to any control. On the property sheet go to the other tab and set the name property.
Tip: choose a name you you will remember without having to look it up and describes the controls function.
Next select the checkbox (not the checkbox's label). On the property sheet go to the event tab and select the on click event. hit the ellipsis and choose code builder. Access is Event Driven. We want the textboxes to appear when the checkbox is selected so we put that code in the checkbox click event.
after choosing code builder we get the code window where we can browse among all the events for all our forms. for now all you should see is something like:
Private Sub mycheckbox_Click()
End Sub
So insert some code to handle the checkboxes like:
Private Sub mycheckbox_Click()
If mycheckbox = True Then
txtbox1.Visible = True
txtbox2.Visible = True
txtbox3.Visible = True
txtbox4.Visible = True
Else
txtbox1.Visible = False
txtbox2.Visible = False
txtbox3.Visible = False
txtbox4.Visible = False
End If
End Sub
now when the checkbox is not checked no textboxes are visible.
but when the checkbox is checked they appear

Test if Controls/fields are on the visible tab ms Access

I have a MS Access Form with various controls organised into tabs.
There are some text fields among them, and I need the text field to update (that is, the vba to update them) when their tab is opened. The text fields aren't bound, the updating is a bit more customised.
I've tried the below code but it returns True for text fields that aren't on the visible tab
Private Sub TabCtl2_Change()
Dim Ctl As Control
For Each Ctl In Me.Form.Controls
If Ctl.ControlType = acTextBox Then
'Want to do stuff here
Debug.Print Ctl.Visible 'This is returning 'True' for all text fields
'not just the ones on the active tab.
Debug.Print Ctl.Name
End If
Next
End Sub
As far as I can see the Tab objects don't have a Controls collection or whatever you call it. Can someone help me how to loop through controls on a tab? I don't want to code it all in manually if I can help it.
The Page object in the Pages collection in the tabcontrol does have a Controls property.
This means, if you want to do something for all controls on the active page, you can use the following:
For Each control In Me.MyTabcontrol.Pages(Me.MyTabcontrol.Value).Controls
'Do Stuff
Next

Microsoft Word VBA tab key to make textbox visible

Longtime viewer, first time question asker.
I'm currently working with UserForms within MS Word and have a particular form that can have up to 20 different labels and accompanying textboxes with varying texts. I have all but the first hidden while not in use, however I would like the next label and text box to become visible following input in the previous textbox. So if you enter data (anything) in the first textbox, the next label and text box will become visible. Does this make sense? I've seen other responses here suggest using AfterUpdate() rather than Change() or Click() but can't figure out how to use any of them. I would share my code but at this point I don't have any code to share, other than my labels and textboxes are lblField1 txtField1, lblField2 txtField2...
Any suggestions?
I would suggest using Change event, when using AfterUpdate you need to leave you TextBox for a while to fire the event. If you have only one TextBox visible there is nothing to move to. If you have more TextBoxes you would need to move back to fire AfterEvent and I don't think this is what you expect.
So, double click wherever on your userform and add the following code in code area:
Private Sub txtField1_Change()
txtField2.Visible = True
lblField2.Visible = True
End Sub
Next, add next portion for next textbox:
Private Sub txtField2_Change()
txtField3.Visible = True
lblField3.Visible = True
End Sub
And so on, if only you have an order in controls name you just need to change numbers in the end of control names.

Placing Label Over Combo-box in Ms-Access

Background:
I am trying to place a label in Ms-Access that is being used as a button over a combo box. The user will select from the combo-box list and if they want to erase their selection they can click on the label to clear the selection.
Question:
Is it possible to get the label to show above the combo? It seems like the label is being sent behind the combo even though I specifically send it to the from etc...
Instead of creating the overlaying label to clear the selection, I would create an actual button. You will stack the button and the combo box on top of eachother in design mode, but set the button visibility to "No" in the format properties. Let's say you call the combo box "cmbSelect" and the clear button "cmdClear", use the following to show the button after the combo box is selected:
Private Sub cmbSelect_AfterUpdate
Me.cmdClear.Visible = True
End Sub
The following code will then clear the data from your table after the button is clicked:
Private Sub cmdClear_Click()
DoCmd.SetWarnings False
'Deletes record from your table
Dim Delete As String
Delete = "DELETE * FROM [TableName] WHERE (([TableName].KeyField)='" & KeyField & "')"
DoCmd.RunSQL Delete
DoCmd.SetWarnings True
End Sub
You could also have the form requery by running a requery on each field instead of using the delete string. Then you could build code for the button "after update" that makes the button invisible again and allows you to select a new entry from the combo box. There's a lot of possibility, but this should get you started. Let me know if you need any more explanation or help.

How to get selected text in VBA

I have a macro that changes the selected text, and I have it assigned to a button.
It works perfectly when i run it directly from visual basic, but when I click the button, the button gets the focus and my text is no longer selected so the macro change the selected element to (button).
How can I select the text and run the macro by clicking on the button and still have the text selected?
The way to do this is to set the set the TakeFocusOnClick property of the CommandButton to False. Here are is the code I use.
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
Is the button embedded in the document? You may need to put it on a form that loads on top of the Word window or in a menu/toolbar, so that clicking it does not affect the selection in the document itself.
Edit:
I think you can use Application.Selection.Previous to get at what you need. You could use this to restore the selection after the click event, or to act upon that section of the document, or both.
I assume that this is available in previous versions of Word, but have only confirmed its presence in 2007.
You need to change TakeFocusOnClick to "False" in the Button Preferences.