Getting value of listbox

Ben2k

Board Regular
Joined
Oct 16, 2002
Messages
77
Hi,

I have two listboxes, populated as follows

Sub Form_Load()

Me.trade1.RowSource = "SELECT COUNT(Qry_Summary_GetAllDeals.Type) FROM Qry_Summary_GetAllDeals WHERE (((Qry_Summary_GetAllDeals.Type)=""trade""));"

Me.trade4.RowSource = "SELECT COUNT(Qry_Summary_GetAllDeals.Type) FROM Qry_Summary_GetAllDeals WHERE (((Qry_Summary_GetAllDeals.Type)=""trade"") AND ((Qry_Summary_GetAllDeals.Finance)>0));"

I then want to use the values of the two listboxes in a calculation

Dim trade5value As Integer
trade5value = (trade4.Value / trade1.Value) * 100
Me.trade5.Value = trade5value

This returns an error "invalid use of null",

Is this due to the listbox having nothing selected? If so how can I select its value in code?

Thanks
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi Ben2k

Is this due to the listbox having nothing selected?

Yes.

If so how can I select its value in code?

To select a default item (if the listbox is not empty)

Code:
Me.trade1.RowSource = "SELECT COUNT(Qry_Summary_GetAllDeals.Type) FROM Qry_Summary_GetAllDeals WHERE (((Qry_Summary_GetAllDeals.Type)=""trade""));" 

if Me.trade1.listcount>0 then
 Me.trade1.selected(0)=True
end if

Me.trade4.RowSource = "SELECT COUNT(Qry_Summary_GetAllDeals.Type) FROM Qry_Summary_GetAllDeals WHERE (((Qry_Summary_GetAllDeals.Type)=""trade"") AND ((Qry_Summary_GetAllDeals.Finance)>0));" 

if Me.trade4.listcount>0 then
 Me.trade4.selected(0)=True
end if

I hope it helps.

Suat
 
Upvote 0

Forum statistics

Threads
1,223,645
Messages
6,173,527
Members
452,520
Latest member
Pingaware

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