VBA UserForm for searching multiple records with one criteria

  • This is my first thread here.... so please if you can, don`t be harsh... :)
    There is the thing, I am trying to build a UserForm just for "simple" search.
    At the moment I have two worksheets - "Dati"; "kilas"
    In both ws the lookup value is in col A.


    What I did already,
    User will put a Client ID number in search textbox called: "krednr"
    Based on this criteria it retrives data to all other textboxes form different colums
    It goes for ws "Dati" and "kilas"


    This is what I have:


    Private Sub CommandButton1_Click()
    row_number = 0
    Do
    DoEvents
    row_number = row_number + 1
    item_in_review = Sheets("Dati").Range("A" & row_number)
    If item_in_review = krednr.Text Then
    TextBox1.Text = Sheets("Dati").Range("G" & row_number)
    TextBox3.Text = Sheets("Dati").Range("H" & row_number)
    TextBox4.Text = Sheets("Dati").Range("J" & row_number)
    TextBox5.Text = Sheets("Dati").Range("F" & row_number)
    TextBox6.Text = Sheets("Dati").Range("R" & row_number)
    TextBox7.Text = Sheets("Dati").Range("M" & row_number)
    TextBox8.Text = Sheets("Dati").Range("N" & row_number)
    TextBox9.Text = Sheets("Dati").Range("AI" & row_number)
    End If
    Loop Until item_in_review = ""
    row_number = 0
    Do
    DoEvents
    row_number = row_number + 1
    item_in_review = Sheets("kilas").Range("A" & row_number)
    If item_in_review = krednr.Text Then
    TextBox10.Text = Sheets("kilas").Range("D" & row_number)
    End If
    Loop Until item_in_review = ""

    End Sub



    But today I spent 4hours trying to find solution for cases when there is a multiple records for one client ID
    I need them to show in one text box or listbox....


    Example:




    [TABLE="class: grid, width: 500"]

    [tr]


    [td]

    A

    [/td]


    [td]

    B

    [/td]


    [td]

    C

    [/td]


    [/tr]


    [tr]


    [td]

    Client ID

    [/td]


    [td]

    Pet

    [/td]


    [td]

    Count

    [/td]


    [/tr]


    [tr]


    [td]

    123

    [/td]


    [td]

    cat

    [/td]


    [td]

    1

    [/td]


    [/tr]


    [tr]


    [td]

    177

    [/td]


    [td]

    dog

    [/td]


    [td]

    1

    [/td]


    [/tr]


    [tr]


    [td]

    185

    [/td]


    [td]

    lion

    [/td]


    [td]

    1

    [/td]


    [/tr]


    [tr]


    [td]

    123

    [/td]


    [td]

    dog

    [/td]


    [td]

    2

    [/td]


    [/tr]


    [tr]


    [td]

    123

    [/td]


    [td]

    puma

    [/td]


    [td]

    1

    [/td]


    [/tr]


    [/TABLE]


    Like for examp.:
    If i insert Client ID "123"
    It will show result
    cat 1
    dog 2
    puma 1


    And all in one "BOX"!


    Where should i start?!

  • Re: VBA UserForm for searching multiple records with one criteria


    You will need to count the number of Client Ids found, and if over 1, you will need to adjust your userform accordingly (for instance, if you have 100 Client IDs, you need to adjust the box so 100 can fit in - you can add scroll bars), and then loop in all the results to "build" the results in the userform.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!