VBA - ListBox cannot populate

SofiaEd

omg, spent 4 hours trying to debug this, but to no avail. Please help? My code is below. The objective is straightforward: Search in the database under column B (employee's name), and then populate the ListBox in the UserForm. The error I keep getting is "Run Time Error '380': Could not set the value property." Do you have any idea what is wrong with my code?

Private Sub UserForm_Initialize()
    TextBox_Keywords.SetFocus                               '<<<<< Searh for employee's name.
    Worksheets("Outcome").Range("A2:D9777").ClearContents   '<<<<< Tab "Outcome" has 4 columns with headers.
End Sub

'-----------------------------

Private Sub ButtonSearch_Click()

    Application.ScreenUpdating = True
    Application.EnableEvents = True

    Dim RowNum As Long
    Dim SearchRow As Long

    RowNum = 11     '<<<< Data begins with Row 11 on Tab "MasterID"
    SearchRow = 2

    Worksheets("MasterID").Activate

    Do Until Cells(RowNum, 1).Value = ""
        If InStr(1, Cells(RowNum, 2).Value, TextBox_Keywords.Value, vbTextCompare) > 0 Then
            Worksheets("Outcome").Cells(SearchRow, 1).Value = Cells(RowNum, 1).Value
            Worksheets("Outcome").Cells(SearchRow, 2).Value = Cells(RowNum, 2).Value
            Worksheets("Outcome").Cells(SearchRow, 3).Value = Cells(RowNum, 3).Value
            Worksheets("Outcome").Cells(SearchRow, 4).Value = Cells(RowNum, 4).Value
            SearchRow = SearchRow + 1
        End If
        RowNum = RowNum + 1
    Loop

    If SearchRow = 2 Then
        MsgBox "No records.", , "Search Status"
        Exit Sub
    End If

    ListBox_SearchResults.RowSource = "Outcome"   '<<<< This is where the error kicks in.

End Sub

enter image description here

HackSlash

It's clear that you are assigning a sheetname to a rowsource because we can see the sheetname in your code. That's very helpful.

A rowsource can only be set to "A table name, query name, or SQL statement." or "A list of items with semicolons (;) as separators."

To make this work with your spreasheet you will need to select the range that you want to become the rowsource and convert it to a table. Then you have to give the table a name. Then you can assign the rowsource to the name of that table.

You could do this manually, or with VBA code.

ROWSOURCE: https://docs.microsoft.com/en-us/office/vba/api/access.listbox.rowsource

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related