• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

One matching row from SQL to VBA UserForm ListBox.

Ana Luna

New Member
Dear all,
In the code bellow, I am trying to search the value ClientID in the TBRDetail table and load the matching row(s) into ListBox2.
In your opinion, what would be the best option to achieve it?

To be honest, I've managed to load without any problem when there are more than one matching rows, but when only one row matches (only one row needs to be loaded into ListBox2), the values get misaligned as I'm showing you in the image below (they all go into the first column, one below the other).

Thank you so much, your help is much appreciated.
Regards,

<<<<<<<start code>>>>>>>>>>>>>>>>

>>> You've noted many times <<<
>>> use code - tags <<<
Code:
' Clear ListBox2 before populating it
UserForm1.ListBox2.Clear


' Set up the Command object for the new SQL query
With cmd
    .CommandType = adCmdText
    .CommandText = "SELECT [Year], [Sales], [KPI], [Sales Quota]" & _
                   " FROM [TBRDetail] WHERE [Client ID] = '" & ClientID & "'"
End With

' Create a new Recordset object for the SQL query
Set rs = New ADODB.Recordset

' Execute the command and retrieve the results
Set rs = cmd.Execute

If Not rs.EOF Then


End If
<<<<<<<finish code>>>>>>>>>>>>>>>>

<<image>>

1698356757680.png
 
Last edited by a moderator:
Back
Top