Multiple Clicks on a Button in MS Word - Visual Basic - vba

I am creating a document in word with buttons that the user clicks to add text into a text box.
For example - clicking button 1 will add "You've clicked button 1" to text box 1.
Clicking button 2 will add "You've clicked button 2" to the same box. (Exciting stuff I know).
I'm using TextBox1.Text = TextBox1.Text + "You've clicked button 1" within a Click event in Visual Basic.
I'd like the user to be able to click the buttons again to be able to remove (or undo?) the text. Or, indeed, click a third time to add the text back in again.
I guess I want the user to be able to toggle the text in the box or not. Maybe even change the button colour based on whether the text is on/off or in/out of the text box.
Any suggestions?

The way that I would probably do this is to have a simple IF statement that first determines the state of the text. If it's empty, then populate it. If it's not empty, then make the value equal to "".
The code would look something like this (but not exactly - this is just theoretical)
If textbox1.text = "" Then
textbox1.text = "You've clicked button 1"
Else
textbox1.text = ""
End If
Apologies if I haven't understood the requirement correctly.

Related

Change the color of a selected record in an Access REPORT

My Access REPORT has a text box with the Record ID that looks like a button with an on click event to go to a form for that specific record. This works great, but when I return to the report I cannot see which record was clicked. I want to temporarily change ONLY the record that was clicked until another record is selected.
The reason I want this on a report and not a form is because I want the user to have a quick way to proof read in the format needed to print, and make a change or check a detail if necessary, then update the report AFTER all proof reading and updates are completed and before final print. But with many records on the screen it is easy to lose track of which record you were checking when returning from the form.
I tried:
Private Sub btn_txt_GoToTransaction_Click()
Dim vColor
vColor = RGB(51, 204, 51) 'green
Me.btn_txt_GoToTransaction.BackColor = vColor
DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub
But this does not work because every button turns color not just the selected record.
Any suggestions? Thanks.
This is a great question because there are many benefits to highlighting a row or item in an Access Report. You are not able to just change the button color in one row only, but you can highlight the whole row so the user knows where they were.
Here are two methods to accomplish this:
Method 1 - Click on a Label
This works great in newer versions of MS Access when using Report View. Use a Label Control instead of a Button. You could make the label look like a button if you format it that way. I prefer to stretch an invisible Label across the whole row on top of all the other controls in that row. Then if you click anywhere in the row, it automatically selects that row and then runs whatever code you have in the OnClick Event. This works best if the Label is not linked to a Text Box.
This picture shows an example of how this method looks. You can click anywhere in the row and it highlights that row with the red outline and grey background.
This is very simple and works well but there are a couple disadvantages:
1- You can not change the color of the highlight.
2- If any of the text boxes CanGrow, the row height may be higher then the Label and create areas where the invisible label doesn't capture your click.
3- Clicking on a Text box does not work for this method.
Method 2 - Change Color of a Text Box
In order to just highlight one row or one piece of data in a report, we can use the "FormatConditions" property. This is the same as Conditional Formating from the MS Access design interface but we are going to change it programmatically on the fly. You can't do this with a button or label - it needs to be a Text Box with unique data, such as your TransactionID.
This picture shows an example of how this method looks. You can set the color of the highlight if you follow the steps below.
STEP 1) I recommend that you add a text box to your report that stretches from the left to the right, set the Back Color and Fore Color to White, set the Control Source to TransactionID, and set the Name to TransactionID. Then right click on this text box and select Position > Send To Back. This works best if the other text boxes and labels on the report have a transparent background.
STEP 2) Add this code:
Private Sub HightlightRow(intRowID As Integer)
With Me.TransactionID.FormatConditions
.Delete
With .Add(acFieldValue, acEqual, intRowID)
.BackColor = vbGreen
.ForeColor = vbGreen
End With
End With
End Sub
STEP 3) Also change your button code to call this subroutine like this:
Private Sub btn_txt_GoToTransaction_Click()
HightlightRow Me.TransactionID.Value
DoCmd.OpenForm "Account_frm", acNormal, , "[TransactionID]=" & Me.TransactionID
End Sub
STEP 4) I like to set it up so if the user clicks anywhere in the row, it will pop up with a modal with more detail regarding that row. Also, the user can't make any changes to the data in the Report View, so I use the pop up modal to allow changes. To accomplish this, I do a couple more things:
First, we need to add the code to the OnClick event for every control in that row. Ofcourse, each OnClick event will simply can that subroutine HightlightRow Me.TransactionID.Value
Second, if the user clicks on a Text Box, the Text Box gets the focus and hides the highlight. Therefore, I like to set the focus to something else. In your case, you could set the focus to the button by adding this line to the end of the HighlightRow subroutine: btn_txt_GoToTransaction.SetFocus
In my case, I am not using a button, so I set up a tiny Text Box with = " " (just an equal sign a space in quotation marks) as the Control Source. Then I position this tiny Text Box to the far right. And in the HighlightRow subroutine, I set the focus to this textbox.
STEP 5) You may also want a button or method of removing the highlight. To do that simply have the code run this line:
Me.TransactionID.FormatConditions.Delete

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

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.

VB.NET add text to the front of text

I am trying to add text to current text that is in a textbox using a checkbox. Once the checkbox is checked, it will "add" the text to the textbox before the text already in the textbox.
example:
if the textbox said "Jake", it would say "Hello Jake" once the checkbox was checked.
EDIT: sorry for the quick question. I'm in a hurry. But the only method I could think of was concatenating and appending text. As far as I know append() adds only to the end, and concatening isn't the logical approach. I don't have a coded example because I dont even know how to approach this issue. thanks.
It's getting a thumbs down because its so simple, but not. I'm using multiple checkboxes. So one needs to be in the very front, one in the center, etc. Each checkbox injects text into the textbox a certain way. I can do this with nested if statements, but then we got a mess.
Try using this code :
if chbHello.Checked then
txtName.Text = chbHello.Text + " " + txtName.Text
Note : if the checkbox is the trigger put this code in the checkbox.
All it does is see if the checkbox was checked or not.
When its checked just concatenate the text of the textbox and the checkbox with the
checkbox first.

Update a line in a text file from another form

I have a form that has MenuScripts (top-levels and second-levels).
The problem that I am having is one of the second-levels is "Add" which brings you to another form. This form has a button 'Record' and textboxes. This form allows the user to input data and when the record button is clicked, the inputted data is written to a text file.
Back to the first form. Another second-level MenuScript is "Update" which brings the user to the same form as "Add"; but first, the user has to click an item within a listbox to proceed.
How do I get the data from the selected item to appear in the appropriate textboxes and how do I get the 'Record' button to update data instead of thinking it is only an add-data button?
Also, if someone can give me some pointers on making sure the user selects an item within the listbox would definitely be a plus!
Unfortunately, I cannot add images since my reputation is too low.
Here is a visual representation of my ultimate goal
To access the control in another form, you could prefix the formname to the control name(name of the control in the other form).
Example:
Form2.TextBox1.Text = "Hey, this is the second form"
To check whether an item is selected, you could do this way:
If ListBox1.SelectedItems.Count = 0 Then
MessageBox.Show("Please select an item")
Else
MessageBox.Show("Thanks for selecting an item")
End If
Or, this way:
If ListBox1.SelectedIndex = -1 Then
MessageBox.Show("Please select an item")
Else
MessageBox.Show("Thanks for selecting an item")
End If

VB.net - Issues with reading radio buttons between forms

I have a basic math test program, and 2 forms to allow the user to choose which function to test on.
The first form has a list of radio buttons with the 4 basic math functions and a button to load the next form.
The second form contains this code on Load...
'Change function sign to reflect chosen option
If frmOptions.rdoAdd.Checked Then
lblFunc.Text = "+"
ElseIf frmOptions.rdoSub.Checked Then
lblFunc.Text = "-"
ElseIf frmOptions.rdoMult.Checked Then
lblFunc.Text = "x"
ElseIf frmOptions.rdoDiv.Checked Then
lblFunc.Text = "÷"
End If
If I change the Checked field in the properties in form 1, it passes to form 2.
However if I use the radio buttons to choose a different option, form 2 sticks with the default choice from form 1.
EDIT
Changed a few lines based from input, but still having this issue.
I'll run through it again.
frmOptions has 4 radio buttons, addition is default chosen.
After selecting one, the user clicks a button to open frmTest.
The above code runs upon first load to change a label to reflect what was chosen on frmOption above.
On step 3, the label on frmTest that is supposed to be changed based on the Checked radio button from frmOption never changes. It doesn't even change on the first load of the form, so I think something is wrong with the Checked property of the radio buttons.
Hopefully this clears things up a bit.
It seems like there's not enough information here to know for sure.
It sounds like your first form is loading the second form with values the first time, but then on subsequent calls it doesn't load the second form because it's already loaded. Does that sound correct?
If so, then that you are probably setting values on the first form which get passed to the second form. Instead, create properties on the second form and load it first. Then have the first form manipulate those properties.
Try firing an event when the radio button changes instead of when form2 loads.
Try using the Form2.Shown event instead of Form2.Load.
A form is loaded only once, but it can be shown numerous times.
Have you tried
If My.Forms.Form1.Radiobutton_Name.Checked = True Then
lblFunc.Text = "+"
ElseIf My.Forms.Form1.Radiobutton_Name.Checked = True Then
lblFunc.Text = "-"
ElseIf My.Forms.Form1.Radiobutton_Name.Checked = True Then
lblFunc.Text = "x"
ElseIf My.Forms.Form1.Radiobutton_Name.Checked = True Then
lblFunc.Text = "÷"
End If