can anybody help me with a sample code on how to add items in listbox connected in database.
my item list are amount, description, and OR #.

Recommended Answers

All 6 Replies

Same as per your previous post, have a look at this discussion.

your post was using lisvtview, but im using listbox.
is listview and listbox the same in code?

use following code to add items to listbox
list1.AddItem "item1"

if you mean retrieve data from database to listbox , then use the following code

con.Provider = "Microsoft.Jet.OLEDB.4.0"
con.Open App.Path & "\db.mdb"
rec.Open "select field_name1,field_name2 from tbl", con, 2, 3
While (rec.EOF = False)
List1.AddItem rec.Fields(0)
List2.AddItem rec.Fields(1)
rec.MoveNext
Wend
con.Close
Set con = Nothing

Note that :-
1.Replace field_name1,field_name2 with your table's fields

hope this helps you

Oops, my bad. Seems I'm human after all. I read it as listview.

If you want to add data to the listbox, as rishif2 sampled above. (Thanx rishif)

If you want to retrieve the data from listbox and add to database...

con.Provider = "Microsoft.Jet.OLEDB.4.0"
con.Open App.Path & "\db.mdb"
rec.Open "SELECT field_name1,field_name2 FROM tbl WHERE Id ='" & idhere & "'", con, 2, 3

''If edit...
rec!field_name1 = Listbox1.Text
rec.Update

''If new record...
rec.Open "SELECT field_name1,field_name2 FROM tbl", con, 2, 3
rec!field_name1 = Listbox1.Text
rec.Update

con.Close
Set con = Nothing

its ok AndreRet
and thanks a lot guys.
you made it exactly as what i want.

:) Only a pleasure.

Happy coding...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.