Hi, cancan someone help me with my problem?
I am using vb 6.0 and MS Access 2003. I have a form where all the data in the database should display. The problem is, I don't know why my code is not working.

here it is:

Private Sub Form_Load()
Dim list As ListItem
ListView1.ListItems.Clear
Set rs = New Recordset
rs.open "select* from tblname", cn, adOpenDynamic, adLockOptimistic
    Do Until rs.EOF
        Set list = ListView1.ListItems.Add(, ,!RecordNumber)
    lista.ListItems.SubItems(1) = !Firstname
    lista.ListItems.SubItems(2) = !Lastname
    lista.ListItems.SubItems(3) = !Birthdate

            rs.MoveNext
    Loop
End Sub

And if I input the lastname, firstname and date of birth (using a DTPicker), the saved data will appear in the listview.
Please help. Thank you.

Where is "lista"?, Is it supposed to be "list"?
change the !RecordNumber into rs("RecordNumber") and the other fieldnames...respectively

-also change you loop, use this instead, this is much faster:

Dim i as Long
For i = 0 To rs.Recordcount - 1
    'Your code to populate the listview
    
     Doevents
Next i

For question number 2:

'On the click event of a button, which is a save button....
cn.Execute "INSERT INTO tblname VALUES (" & txtRecordNumber.Text & ",'" & txtFName.Text & "','" & txtLName.Text & "','" & dtPicker.Value & "')"

'Now call/paste your code here to populate your listview like the codes in your Form_Load event...

I tried your code, when the form was loading it debugged and highlighted the ".ListItems" here:

lista.ListItems.SubItems(1) = !Firstname

saying that method or data member not found

Do Until rs.EOF
Set list = ListView1.ListItems.Add(, ,!RecordNumber)
lista.ListItems.SubItems(1) = !Firstname
lista.ListItems.SubItems(2) = !Lastname
lista.ListItems.SubItems(3) = !Birthdate

rs.MoveNext
Loop

Is incorrect.

You have no reference to the field name -

Do while not rs.EOF = true
        Set list = ListView1.ListItems.Add(, ,rs!RecordNumber)
    lista.ListItems.SubItems(1) = rs!Firstname
    lista.ListItems.SubItems(2) = rs!Lastname
    lista.ListItems.SubItems(3) = rs!Birthdate

            rs.MoveNext
    Loop

@Andre - sir, I still got the same message, saying that:

lista.ListItems.SubItems(1) = rs!Firstname

invalid property value

Try:

Modified code from sir Andre

Do while not rs.EOF = true
    
    Set list = ListView1.ListItems.Add , , rs.Fields!("RecordNumber")
    lista.ListItems(lista.ListItems.Count).SubItems(1) = rs.Fields!("Firstname")
    lista.ListItems(lista.ListItems.Count).SubItems(2) = rs.Fields!("Lastname")
    lista.ListItems(lista.ListItems.Count).SubItems(3) = rs.Fields!("Birthdate")

    rs.MoveNext
    Loop

Is that lista or list.? Confuses me.

Just change to list if not lista.

sir abelingaw, I'll just correct you on this one for the future users

Set list = ListView1.ListItems.Add , , rs.Fields!("RecordNumber")

schould be like this:

Set list = ListView1.ListItems.Add (, , rs.Fields!("RecordNumber"))

thanks anyway. it helped a lot. :))

We are glad that we could help you solve this.:)

Please mark this thread as "SOLVED", found at the bottom of this page, thanks.

@UnderSurvival:

HHhhm, that code worked for me. Just don't know if it didn't work for you.

Well, thanks also.

Mark thread as Solved if problem's fixed.

try use this->

private sub loadlist()
Dim clX As ColumnHeader
Dim itX As ListItem

Set tmp = New ADODB.Recordset
tmp.CursorLocation = adUseClient
tmp.Open ("select * from tblname"),db,adOpenDynamic,adLockOptimistic
If tmp.RecordCount > 0 Then
tmp.MoveFirst
i = 0
Set clX = lstvw.ColumnHeaders.Add(, , "Sr.No.", 700)
Set clX = lstvw.ColumnHeaders.Add(, , "First Name", 1200)
Set clX = lstvw.ColumnHeaders.Add(, , "Last Name", 1200)
Set clX = lstvw.ColumnHeaders.Add(, , "Date of Birth", 1500)

While Not tmp.EOF
i = i + 1
Set itX = lstvw.ListItems.Add(, , i)
itX.SubItems(1) = tmp.Fields("firstname")
itX.SubItems(2) = tmp.Fields("lastname")
itX.SubItems(3) = tmp.Fields("birthdate")
tmp.MoveNext
Wend

tmp.close
set tmp=nothing
End If
end sub

Edited 5 Years Ago by dspnhn: n/a

@UnderSurvival, please mark this thread as solved if you have managed to get a solution, found at the bottom of this page, thanks.:)

It has been open for some time now.

This article has been dead for over six months. Start a new discussion instead.