does any1 have any idea how to link listboxes with adodc to a database?

Recommended Answers

All 10 Replies

With a listbox, I'm pretty sure you have to do it all manually, for example:
1) Connect To Database
2) Query Database
3) Add Relevant info to listbox using the .additem method.

As far as making the listbox directly receive the recordsets.... I don't think you can.

Do you have a recordset???
Then, you do the folling

With RS
Do until .EOF
lisbox.additem recordset.field
.movenext
loop

end with

let me know if this sounds confusing though/

Which, is basically what I said, without posting the code. If you are trying to bind the listbox to the database, though, that method will not work.

this is what my code is like, i'm trying to add a new task and in another form, i can later modify, if i wanna make changes....i desperately need help from any1 who's willing to offer, coz i'been stuck with listbox b4 i can proceed with my project. when i add new tasks, my lstAttach doesn't show the tasks in the database and in my frmModify, i use an ADODC which works for the textboxes but not my lstAttach...any ideas? below is my code for frmAdd and frmModify..can sum1 point me in the right direction, so i can get this load on listbox off my back...!

Private Sub cmdAdd_Click()
Set wk2 = DBEngine.CreateWorkspace("MyWks", "admin", "", dbUseJet)
        Set db2 = wk2.OpenDatabase("c:\Project 1\legalminder1.mdb", False)
        Set rs2 = db2.OpenRecordset("addrecord", dbOpenDynaset)
        MsgBox "Add Record", vbInformation, "Record"
        
        rs2.AddNew
        rs2.Fields(0).Value = UCase(txtID.Text)
        rs2.Fields(1).Value = UCase(txtDefineTask.Text)
        rs2.Fields(2).Value = UCase(txtDocuments.Text)
        rs2.Fields(3).Value = DTPicker1.Value
        rs2.Fields(4).Value = CInt(txtDays.Text)
        rs2.Fields(5).Value = UCase(txtNotes.Text)
        rs2.Fields(6).Value = lstAttach.Text
        rs2.Update
        lstAttach.AddItem NoNull(rs2.Fields(6).Value)
        
        txtDefineTask = ""
        txtDocuments = ""
        DTPicker1.Value = Date
        txtDays = ""
        txtNotes.Text = ""
        lstAttach.Clear
        txtDefineTask.SetFocus
   
 End Sub
 
Private Sub cmdModify_Click()
strResponse1 = MsgBox(" Do you want to Save Changes to this Record ?", vbYesNo + vbExclamation, "Record")
        If strResponse1 = 6 Then
            cn5.Open "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=c:\Project 1\legalminder1.mdb; Persist Security Info=false"
            rs5.Open "select * from addrecord", cn5, adOpenDynamic
            cn5.BeginTrans
            sql = "update addrecord set " & _
            "Task = '" & txtDefineTask.Text & "', " & _
            "Documents = '" & txtDocuments.Text & "', " & _
            "DDate = '" & txtDueDate.Text & "', " & _
            "Days = '" & txtDays.Text & "', " & _
            "Notes = '" & UCase(txtNotes.Text) & "', " & _
            "Attachment='" & UCase(lstAttach.Text) & "' where ID = " & txtID.Text
            cn5.Execute sql
            MsgBox "Record Updated", vbInformation, "Record"
            cn5.CommitTrans
            Adodc1.Refresh
            rs5.Close
            cn5.Close
            Adodc1.Recordset.MoveNext
        txtID.Text = NoNull(Adodc1.Recordset.Fields(0).Value)
        txtDefineTask.Text = NoNull(Adodc1.Recordset.Fields(1).Value)
        txtDocuments.Text = NoNull(Adodc1.Recordset.Fields(2).Value)
        txtDueDate.Text = NoNull(Adodc1.Recordset.Fields(3).Value)
        txtDays.Text = NoNull(Adodc1.Recordset.Fields(4).Value)
        txtNotes.Text = NoNull(Adodc1.Recordset.Fields(5).Value)
        lstAttach.Text = NoNull(Adodc1.Recordset.Fields(6).Value)
        
        ElseIf strResponse1 = 7 Then
            Exit Sub
        End If
   
End Sub

One Problem that stands out, is that the lstAttach is a listbox, and NOT a textbox. A listbox has a .text property, but it's basically there just to trick you. I wrote a test program, that has only 2 lines of code on form load. Which is: list1.text = "test" and then msgbox list1.text , and you'll see the msgbox is still blank. I guess microsoft had some lazy programmers, and just decided that if a certain control doesn't logically need a certain property, to just ignore it. If you want to add the item from the listbox, you have to know which item you want to add. A listbox is almost identical to an object form of an array, in that it has indices (indexes). If you want to know which item the USER selected from the listbox, you'll need to use something like this:

rs2.AddNew
rs2.Fields(0).Value = UCase(txtID.Text)
rs2.Fields(1).Value = UCase(txtDefineTask.Text)
rs2.Fields(2).Value = UCase(txtDocuments.Text)
rs2.Fields(3).Value = DTPicker1.Value
rs2.Fields(4).Value = CInt(txtDays.Text)
rs2.Fields(5).Value = UCase(txtNotes.Text)
rs2.Fields(6).Value = lstAttach.list(lstattach.listindex)
rs2.Update

What I've done, here, is changed lstAttach.text to lstAttach.list(lstattach.listindex). If the user has not selected any item in the listbox (meaning, no item is highlighted) then you can use numbers if you know which number you want to add. For example, to add the very first item in the listbox to the database: lstAttach.list(0) . I hope this helps to clear up some of the problem.

the thing is i'm adding attachments from a dialog box, so say i add 3 files from there, only the 3rd file is read and displayed in the Attachment field in my database, not the other 2! nor does these added files show in my lstAttach in frmModify, even though textbox works when i navigate on my adodc.

I don't understand.....maybe you should attach the entire project. You only have 6 fields in the record (from what I can tell), so, how would you plan to add all the other stuff in the listbox? You'd need fields for those too, right? If you close in the modify portion, what do we see? UCase(lstAttach.Text), ok, it's using text again, and not list(0) or list(1) or list(list1.listindex)

how do i post attachments here, or should i zip it, or jst paste the entire code?

zip it, and then attach it (you might have to click the "advanced" button below. Then I can see what you are doing, and try to follow along with your explanation of what it's SUPPOSED to be doing ;)

i've attached the project, i look forward to ur assistance...as i'm in need of help! thanks in advance.

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.