Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding values from out a list box

Status
Not open for further replies.

ginorom

Programmer
Jun 8, 2003
3
0
0
NL
Hey Guys,

I would like to know how can i take a value out of a list box by it's position from column and row.? For example i would like to take value 50 out of column 1 row 9.How can i do that?
 
Ginorom - I am not sure I understand what you are trying to do. I'll take a couple shots here.


'This would print the value from the 2nd column 11th row in the active form for a listbox named List0. Remember listboxes are 0 based. You can pass variables if you want to change the criteria on the fly

MsgBox Me!List0.Column(1, 10)


This is text from MS Access help.

The next example prints the values of each column for each selected row in the list box, instead of only the values in the bound column.

If you want to play with MS's code just make form named contacts and listbox named Names. Add this code to a pushbutton and it will print all values for the row you have selected

Sub AllSelectedData()
Dim frm As Form, ctl As Control
Dim varItm As Variant, intI As Integer

Set frm = Forms!Contacts
Set ctl = frm!Names
For Each varItm In ctl.ItemsSelected
For intI = 0 To ctl.ColumnCount - 1
Debug.Print ctl.Column(intI, varItm)
Next intI
Debug.Print
Next varItm
End Sub
 
Here is an example on how it looks in a list box

name age
John 25
Peter 23
Myra 19
Kim 34

Now, I want to take the ages from all the persons in the list box and add them up. How can i do that with VB?
 

Ginorom
This code will add the ages of selected items.

Sub Command2_Click()
Dim frm As Form, ctl As Control
Dim varItm As Variant, intI As Integer, ncount As Integer

Set frm = Forms!form1
Set ctl = frm!List3
For Each varItm In ctl.ItemsSelected
ncount = ncount + ctl.Column(1, varItm)
Next varItm
Debug.Print ncount

End Sub

'This adds the age for all items in the list box for your sample listbox
Sub Command2_Click()
Dim intI As Integer, ncount As Integer
For intI = 0 To Me!List3.ListCount - 1
ncount = ncount + Me!List3.Column(1, intI)
Next intI
Debug.Print ncount

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top