Using ADO Query Results to Fill ComboBox

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
When using ADO to extract data to excel, is it possible to use 'CopyFromRecordset' to fill a Combo/Listbox?

Or is it easiest to extract the data to a worksheet and fill the box from there?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
OK...so I have this portion of code...

Code:
    arrValues = rst.GetRows()
        With Customer.cboxCustomer
            .List = arrValues + "*"
            .List = Application.Transpose(arrValues)
        End With
How can I add '*' to the list so that instead of picking the Customer Name for my future query, I can select * for ALL Customers?
 
Upvote 0
You can add something like All.
Code:
.List = Application.Transpose(arrValues) ' add recordset
 
.AddItem "All", 0 'add All at the top of the list.
When it comes to creating the SQL (which I think you might be doing later) you can just check if All has been selected.

If it has then don't add any criteria.
Code:
strSQL = "SELECT * FROM TABLE1"
 
If Combobox1.Value <>"All" Then
       ' add criteria
 
      strSQL = strSQL & " WHERE Field1 ='" & Combobox1.Value &"'"
End If
 
Upvote 0
OK...all of a sudden, I am getting a type-mismatch error. I believe the issue is that arrValues is not being populated.

What am I missing? :confused:

Code:
Dim arrValues
.
.
.
    Set rst = New ADODB.Recordset
        rst.Open "SELECT [Name] FROM [Data Connection TBL] GROUP BY [Name];", cnn, adOpenForwardOnly, adLockReadOnly, adCmdText
        arrValues = rst.GetRows()
'Fill ComboBox
'---------------------------------------------------------------------------------------------------------------------------------------
        With Indemn_Customer.cboxCustomer
            .List = arrValues
           [COLOR=red].List = Application.Transpose(arrValues)[/COLOR]
            .AddItem "All Customers", 0 'add All at the top of the list.
        End With
 
Upvote 0
Try this (I suspect you are only getting one result):
Code:
Dim arrValues
.
.
.
    Set rst = New ADODB.Recordset
        rst.Open "SELECT [Name] FROM [Data Connection TBL] GROUP BY [Name];", cnn, adOpenForwardOnly, adLockReadOnly, adCmdText
   If not rst.EOF then
        arrValues = rst.GetRows()
'Fill ComboBox
'---------------------------------------------------------------------------------------------------------------------------------------
        With Indemn_Customer.cboxCustomer
            If IsArray(arrvalues) Then
           .List = Application.Transpose(arrValues)
           else
            .List = Array(arrValues)
           End If
            .AddItem "All Customers", 0 'add All at the top of the list.
        End With
   End If
 
Last edited:
Upvote 0
Code:
sub snb()
 with  New ADODB.Recordset
  .Open "SELECT [Name] FROM [Data Connection TBL] GROUP BY [Name];", cnn, adOpenForwardOnly, adLockReadOnly, adCmdText
  if typename(.GetRows)= "string" then 
   Indemn_Customer.cboxCustomer.list=split("All Customers|" & .getrows,"|")
  else
   Indemn_Customer.cboxCustomer.List =split("All Customers|" & join(Application.Transpose(.getrows),"|"),"|")
  end if
 end with
end sub
 
Upvote 0
Snb, I had to add the "End With" and have the following...

Rich (BB code):
 With New ADODB.Recordset
  .Open "SELECT [Name] FROM [Data Connection TBL] GROUP BY [Name] ORDER BY [Name];", cnn, adOpenForwardOnly, adLockReadOnly, adCmdText
      If TypeName(.GetRows) = "string" Then
        Indemn_Customer.cboxCustomer.List = Split("All Customers|" & .GetRows, "|")
     Else
        Indemn_Customer.cboxCustomer.List = Split("All Customers|" & Join(Application.Transpose(.GetRows), "|"), "|")
    End If
End With
I get an error: "Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record."
 
Upvote 0
Try this (I suspect you are only getting one result):
Rich (BB code):
Dim arrValues
.
.
.
    Set rst = New ADODB.Recordset
        rst.Open "SELECT [Name] FROM [Data Connection TBL] GROUP BY [Name];", cnn, adOpenForwardOnly, adLockReadOnly, adCmdText
   If not rst.EOF then
        arrValues = rst.GetRows()
'Fill ComboBox
'---------------------------------------------------------------------------------------------------------------------------------------
        With Indemn_Customer.cboxCustomer
            If IsArray(arrvalues) Then
           .List = Application.Transpose(arrValues)
           else
            .List = Array(arrValues)
           End If
            .AddItem "All Customers", 0 'add All at the top of the list.
        End With
   End If

rorya, I got the Type mismatch error as indicated above.

A "watch" on arrvalues indicates: Variant/Variant(0 to 0, 0 to 858)

Without this revision, if I removed ".List = Application.Transpose(arrValues)", then my "list" only included "All Customers".
 
Last edited:
Upvote 0
What's in arrValues when you get the error?

Is it just one value, one record, or multiple records?
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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