Excel vba, unable to select row in listbox - vba

I have a page in a multi page form which takes input from a text box and check box and adds this into a multi column list box below it when a submit button I clicked. I then want the user to be able to select a row in the list box and display the contents in the text box and check box for editing.
I can add multiple text box and check box values into the list box using the submit button, but it does not select a row when I click on the list box item. It just does nothing. It looks like the focus is not being transferred.
Would really appreciate it if someone could give me some ideas or guidance on how I may solve this? The code for the two actions are:
Private Sub cmdaddcontrol_Click()
If Not txtbox_Ctrl_Desc = "" Then 'check for input into text box
ctrl_listbox.AddItem 'add items to list box
ctrl_listbox.List(ctrl_list_count, 0) = ctrl_list_count + 1 'add row number in list box column
ctrl_listbox.List(ctrl_list_count, 1) = txtbox_Ctrl_Desc.Value 'set list box column to text box contents
If Not chkbox_ctrl = False Then 'check if check box is selected then insert appropriate value into listbox column
ctrl_listbox.List(ctrl_list_count, 2) = "Y"
Else
ctrl_listbox.List(ctrl_list_count, 2) = "N"
End If
ctrl_list_count = ctrl_list_count + 1 'increment listbox row counter
txtbox_Ctrl_Desc = "" 'reset text box to blank
chkbox_ctrl = False 'reset check box to blank
Else
MsgBox "You have not enetered anything in the control box" 'error message if no control description in text box
End If
End Sub
Private Sub ctrl_listbox_Click()
Dim i As Integer
'find the selected list item
i = ctrl_listbox.ListIndex
ctrl_listbox.Selected(i) = True
'add the values to the text and check boxes above the list box
txtbox_Ctrl_Desc.Value = ctrl_listbox.Column(1, i)
If Not ctrl_listbox.Column(3, i) = " Y" Then
chkbox_ctrl.Value = True
Else
chkbox_ctrl.Value = False
End If
End Sub

After finding the cause of the issue in the comment chain of the question, I'd like to persist the solution here as an answer for the people that might come across the question in the future:
Make sure that the .Enabled and .Locked properties of the misbehaving ActiveX control are set correctly. (True if the control should allow manipulation/activation by the user at runtime. False if you want to deny the user interaction with the control.)
Verify that the event procedure has the correct name. Maybe you changed a controls name? The correct naming scheme for event procedures is controlName_eventName(), e.g. ListBox1_Click(). An empty _click() procedure is generated when you select Show code in the control's context menu in Design Mode.
Although seemingly trivial, sometimes simple details escape our attention - even more so in complex projects. It's often helpful to have someone from outside the project have look at your problem. In their innocent ignorance of your project's inner workings, they might just be able to ask the right questions that lead to a quick solution.

Related

Word VBA SelectContentControlsByTag collection out of order?

I'm working on a Word template form using ContentControl fields for drop down lists. I made a macro to take each content control tagged "required" and check if it's showing placeholder text. If it is, it throws up a message box asking the user to fill in the required fields, and IDEALLY jumps to the bookmark for that content control box.
After a lot of iteration and troubleshooting, it can in fact do that, but I've noticed that the Collection of required fields is in..... a random order. I tested this by adding another content control dropdown named "jimmy" at the bottom of the Word document and then looked in the collection and saw it was in the middle of all the other required fields.
Does anyone know how the "SelectContentControlsByTag" method decides on its order, and if there's a robust way to make it use the order that they're placed in the document? I have a decent amount of coding experience but I'm entirely self-taught as far as VBA goes, so I'd appreciate the help!
Here is my current code (Doc is set in Document_New, and the bookmark jump isn't in currently, I'm just checking the order in VBA/the message box)
Set reqFields = Doc.SelectContentControlsByTag("required")
For Each iField In reqFields
If iField.ShowingPlaceholderText Then
Dim Msg, Style, Title, Response
Msg = "The dropdowns marked with * cannot be left blank. Do you want to select a response before exiting? Box: " & iField.Title ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton1 ' Define buttons.
Title = "Warning: Empty fields" ' Define title.
' Display message.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
Doc.Saved = False: SendKeys "{ESC}"
'iField.Range.Select
Exit Sub
End If
'Exit For
End If
'Exit Sub
Next iField
Sorry for being 10 months late in answering!
When you use .selectcontentcontrolsbytag VBA will loop through the existing content controls by .ID to pull out the tag name. So if you want your ("tag").item(1) to be ordered you first need to put your content controls in ascending order by .ID number.

Make the Text box disappear for the current selected record

I have a checkbox in my main form which causes a text box and a button to disappear and appear when it is not checked and checked respectively. The code below works correctly:
Private Sub Check288_Click()
If Me.Check288.Value = True Then
MsgBox "Please Enter Reference Number and fill in the name", , "Database"
Text293.SetFocus
Me.MTM_Signature.Visible = True
Me.btn_8DGen.Visible = True
Else
Me.MTM_Signature.Visible = False
Me.btn_8DGen.Visible = False
End If
End Sub
My problem is this code is affecting the whole list of records instead of the current chosen record. I have an auto numbered primary key (ID) for identifying each records. Is there any way for me to make the text box and button disappear only for the current record. I am new to access VBA. Thank you.
In the event of Check288_Click() display a modal form and let the user enter the Reference Number. When closing the pop-up form update the database. You can use conditional formatting to hide (blend) the text box if you do not want to show it when empty.

Searchable textbox and vba highlights row from continuous form and brings record to top with rest of records to follow

I have a form I want to use as a guide to search for a record then pull the record up to the top form as a highlighted line with the rest of the records to follow. Additionally, I want the searchable text box that I'm searching in to automatically jump to the next record without hitting enter or clearing the textbox. I want that part to be automated. So I would be continuously entering in numbers for thousands of records and looking up their information.
Some other info, my form is non-editable but the text box is.
I've tried using a form with some code but can't get it gives me an error saying my private sub is not recognized as a valid field name or expression.
Private Sub Recsch_AfterUpdate()
On Error GoTo Err_ErrorHandler
Dim ItemSelected As String
ItemSelected = Forms![Inventory_Status].Recsch
Recsch = Null
Me.Filter = "[RecId] = '" & ItemSelected & "'"
Me.FilterOn = True
Err_ErrorHandler:
Exit Sub
End Sub
I just want my code to take me to the next line of the form without having to click anywhere. So it would enter in the data on the box, search in my form, highlight the line, bring it to the top, then highlight my box again so I can enter the next number without pressing enter.

How to use VBA to show / hide multiple combinations of tables in MS Word? - explained further within

I have a word 2010 document with some basic VBA code attached to a number checkboxs that show / hide a sections throughout the document.
There are a number of these throughout the document however they all share the same basic code which appears to be working well enough (example below)
Private Sub PlanningBox_Click()
If PlanningBox.Value = False Then
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = False
End If
End Sub
The issue i am having is that some of these sections then have checkboxes within them that show / hide further section which is fine, however if the user unchecks the first initial checkbox (hiding all sections) then re-checks it, it will open up the initial section again but not the ticked subsections - in order to do this they have to uncheck/check the subsection again. I know this seems minor but i need to ensure the form is as fluid and user acessible as possible..
For example let's say there is a single row table with the text 'Have you spoken to anyone for feedback?' (checkbox1) - the user will tick the check box which will unhide a section of text below giving directions, asking some questions, general blurb etc. At the bottom of this is another question 'Who did you speak to?" - and then multiple checkboxes 'mother','father', 'Child' (checkbox A,B,C,etc). Checking any of these boxes opens up a table with additional questions for each one selected, the user can check any number / combination of the checkboxes.
Now if the user unchecks the initial checkbox 1. all sections will be hidden no problem, but then if they then check it to reveal the section again the mother, father etc boxes remain ticked but will not reveal the sections without needing to be unticked/ticked again. Is there a way so that ticking the intial box will also reveal any previously unhidden sections again?
My knowledge of VBA is very limited, i have considered using If + ElseIF statements but my understanding is that i would need an ElseIf for every potential combination. Is there a more sophisticated way around this at all?
Hopefully i have articulated this well enough but happy to provide further information. Thank you for any assistance given
You could create a common routine that evaluates every checkbox and sets the corresponding section accordingly. Call that one routine whenever a checkbox is clicked.
Private Sub PlanningBox_Click()
SetRangeVisibility
End Sub
Private Sub SomeOtherBox_Click()
SetRangeVisibility
End Sub
Public Sub SetRangeVisibility()
If PlanningBox.Value = False Then
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("Planning").Range.Font.Hidden = False
End If
If SomeOtherBox.Value = False Then
ActiveDocument.Bookmarks("SomeOtherRange").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("SomeOtherRange").Range.Font.Hidden = False
End If
' Etc...
End Sub
Further, if you use an array of check boxes you'd only need to write a single SomeBox_Click event procedure. Here is some information on Control Arrays in VBA.

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.