Fill Multicolumn List Box Control on Form with Named Range SQL

justvba

New Member
Joined
Jan 6, 2017
Messages
41
Hi All,

I have a form called frmRemitSpinner with a listbox control called lstInvoices. I want to populate the entire list box based on a dynamic range InvoiceList but based on some conditions that I define in my SQL.

Now, in the properties of my listbox, I have set it to multi-select, column count 2.

Enclosed is the code I am currently using to fill it but I keep getting an error Run Time '-2147352571 (80020005) "Could not set the list property. Type mismatch"

I know this is because I have a null in the 2nd column but how can I make it say 0 or just leave it blank?

Code:
<code class="code-10 nolinks" id="code-10-27963740-1">Public Sub popStandardInvoices()

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim wb As String
Dim i As Integer


'Identify the workbook you are referencing
wb = Application.ThisWorkbook.FullName

'Open connection to the workbook
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                  "Data Source=" & wb & ";" & _
                  "Extended Properties=""Excel 12.0;HDR=Yes"";"


rst.Open "SELECT distinct(item),price FROM [InvoiceList], cnn, adOpenStatic

rst.MoveFirst
i = 0

With frmRemitSpinner.lstInvoices
    .Clear
    Do
    .AddItem
    .List(i, 0) = rst![item]
    .List(i, 1) = rst![price]
   
    i = i + 1
    rst.MoveNext
    Loop Until rst.EOF 
End With

rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing

End Sub
</code>
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You could use IsNull to identify Nulls and replace them with an appropriate value.
Code:
.List(i, 0) =Iif(IsNull(rst![item), "", rst![item])

.List(i, 0) =Iif(IsNull(rst![price]), 0, rst![price])
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,306
Members
449,095
Latest member
Chestertim

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