The idea would be to read the items of Listbox1 (one-by-one) and run a query (Listbox1 ->Field1) and display the result (Field2 -> Listbox3) in the same Listbox3.

For some reason, only appears in Listbox3 the result of the last item of Listbox1.

                 Dim con As New OleDb.OleDbConnection

                 con.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Persist Security Info = False; Data Source = | DataDirectory | \ db.mdb"

                 For Each item In ListBox1.Items

                         The dim cmd_sel OleDb.OleDbCommand con.CreateCommand = ()

                         cmd_sel.CommandText = "SELECT Field1, Field2 FROM WHERE Tabla Field1 LIKE '" & item & "'"

                         The Dim oledbReader OleDb.OleDbDataReader

                         con.Open ()

                         oledbReader cmd_sel.ExecuteReader = ()

                         While the oledbReader.Read ()

                                 ListBox3.Items.Add (oledbReader.Item (1))

                         Loop

                         con.close ()

                         oledbReader.Close ()

Recommended Answers

All 5 Replies

Have you tried looping through your code to see if it is in fact doing what you suspect? A lot of times just checking your variables at run time and making sure they have the right data can be helpful.

Yes, I tried include a msgboxes and everything seems fine.

What happens when you run the following code

Dim con As New OleDb.OleDbConnection
con.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Persist Security Info = False; Data Source = | DataDirectory | \ db.mdb"

For Each item In ListBox1.Items

    dim cmd_sel OleDb.OleDbCommand con.CreateCommand = ()
    cmd_sel.CommandText = "SELECT Field1, Field2 FROM WHERE Tabla Field1 LIKE '" & item & "'"
    Dim oledbReader OleDb.OleDbDataReader

    con.Open ()
    oledbReader cmd_sel.ExecuteReader = ()

    While oledbReader.Read ()
        Debug.WriteLine(oledbReader.Item(1))
        ListBox3.Items.Add (oledbReader.Item(1))
    Loop

oledbReader.Close ()
con.close ()

I want to know what appears in the debug output window. Also, what's the idea of the frequent appearance of "the" in your code as in

The Dim oledbReader OleDb.OleDbDataReader

and

While the oledbReader.Read ()

Im sorry. I dont know what happened when copy and pasted the code but my original code doesnt have "the" before variables.

Im going to try the code you sugested.

Debug when executing the query:

The thread '<No Name>' (0x6b70) has exited with code 0 (0x0).
The thread '<No Name>' (0x667c) has exited with code 0 (0x0).
The thread '<No Name>' (0x7430) has exited with code 0 (0x0).
The thread '<No Name>' (0x73e8) has exited with code 0 (0x0).
The thread '<No Name>' (0x7974) has exited with code 0 (0x0).
The thread '<No Name>' (0x1620) has exited with code 0 (0x0).
The thread '<No Name>' (0x7448) has exited with code 0 (0x0).
The thread '<No Name>' (0x7d08) has exited with code 0 (0x0).
The thread '<No Name>' (0xf7c) has exited with code 0 (0x0).
The thread '<No Name>' (0x11a0) has exited with code 0 (0x0).
The thread '<No Name>' (0x4dd4) has exited with code 0 (0x0).
The thread '<No Name>' (0x28b8) has exited with code 0 (0x0).
The thread '<No Name>' (0x73f8) has exited with code 0 (0x0).

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.