Digger
Digger

Reputation: 1

Excel vba, unable to select row in listbox

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

Upvotes: 0

Views: 2460

Answers (1)

Inarion
Inarion

Reputation: 608

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:

  1. 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.)
  2. 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.

Upvotes: 1

Related Questions