Listbox on a form - how to detect which item is highlighted?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
I have a form with a combibox, a listbox and a textbox.

The user selects a category from the combibox and I have some logic that populates the listbox with subcategories relevant to the category.

The user then highlights one of the items in the listbox and enters a new value for it in the textbox. This drives a SQL query that will update the record in a database. Except I can't manage to get the code to detect which item in the listbox has been selected. Consequently, my SQL UPDATE statement looks like this
Code:
UPDATE Reporting_Test SET Subcategory = 'ValueFromTextBoxHere' WHERE Category = 'ValueFromCombiBoxHere' AND Subcategory = 'ValueFromListBoxHere'

where the last bit of the code has nothing in......so it's not picking up the highlighted item.

Many thanks.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Except I can't manage to get the code to detect which item in the listbox has been selected.
What code have you tried? Because if it's just a default / single-column listbox, you should be able to get the selected item by using the Value property = ListBox1.Value
 
Upvote 0
What code have you tried? Because if it's just a default / single-column listbox, you should be able to get the selected item by using the Value property = ListBox1.Value
Ok, my bad.

I had a variation of this commented out
Code:
For x = 0 To ListBox1.ListCount - 1

      If ListBox1.Selected(x) = True Then
         MsgBox ListBox1.List(x)
      End If

   Next x

Apologies, all working now.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,126
Messages
6,123,200
Members
449,090
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top