How to populate a form control listbox (not ActiveX) with multiple columns?

tcarnahan

New Member
Joined
Nov 27, 2013
Messages
2
Hi,

Due to compatibility issues, I am having to use a form control listbox (vs. ActiveX) control on my spreadsheet. I am trying to populate it with three fields from an ADO recordset, but I am having problems as it has been sometime since I have used Form Controls.

Here is the code I wrote for my ActiveX listbox. Can you point me to an example of where a form control listbox with multiple columns is populated from an ADO recordset?

Thanks ahead of time for any help you can provide in making this conversion!

-- Tom

Code:
Private Sub Worksheet_Activate()
On Error GoTo Err_Worksheet_Activate
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim i As Integer

    Set cnn = New ADODB.Connection

    cnn.ConnectionString = gcstr_Connection ' a global property with the connection string defined elsewhere. The connection is to a SQL Server database.
    
     cnn.Open
     
     Set rst = cnn.Execute("SELECT [FieldA], [FieldB], [FieldC] FROM dbo.<mytable>", , adCmdText)
     
     rst.MoveFirst
     
     With Me.lst_System
         .Clear
         Do
         .AddItem
         .List(i, 0) = rst![FieldA]
         .List(i, 1) = rst![FieldB]
         .List(i, 2) = rst![FieldC]
         i = i + 1
             rst.MoveNext
         Loop Until rst.EOF
     End With
     
Exit_Worksheet_Activate:
     On Error Resume Next
     rst.Close
     cnn.Close
     Set rst = Nothing
     Set cnn = Nothing
     Exit Sub
     
Err_Worksheet_Activate:
     MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
     Err.Clear
     Resume Exit_Worksheet_Activate
End Sub
</mytable>
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,215,493
Messages
6,125,131
Members
449,206
Latest member
burgsrus

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