-
When my application loads it creates a record set from a database and fills a listview control with the information. For some reason when the app opens and the listview control fills only the first column of the first row fills and no more. If I move forward to the next record and back to the first it will fill the rest of the information, just not when the form loads. Here is the code:
Private Sub Fill_Assoc()
Dim ADDRESSBOOK As ListItem
ListView.ListItems.Clear
If rs_assoc.EOF = False Then
While Not rs_assoc.EOF
DoEvents
Set ADDRESSBOOK = ListView.ListItems.Add(, "A" & rs_assoc.AbsolutePosition, rs_assoc.Fields("FName"))
ADDRESSBOOK.SubItems(1) = rs_assoc.Fields("LName")
ADDRESSBOOK.SubItems(2) = rs_assoc.Fields("Address")
ADDRESSBOOK.SubItems(3) = rs_assoc.Fields("City")
ADDRESSBOOK.SubItems(4) = rs_assoc.Fields("State")
ADDRESSBOOK.SubItems(5) = rs_assoc.Fields("Zip")
ADDRESSBOOK.SubItems(6) = rs_assoc.Fields("WPhone")
ADDRESSBOOK.SubItems(7) = rs_assoc.Fields("HPhone")
ADDRESSBOOK.SubItems(8) = rs_assoc.Fields("CPhone")
ADDRESSBOOK.SubItems(9) = rs_assoc.Fields("Pager")
ADDRESSBOOK.SubItems(10) = rs_assoc.Fields("EMail")
Set ADDRESSBOOK = Nothing
rs_assoc.MoveNext
Wend
End If
End Sub
It stops at this line, ADDRESSBOOK.SubItems(1) = rs_assoc.Fields("LName") and moves onto the next sub without filling the rest of the columns. At this point the recordset does not appear to be eof. Does anyone have any ideas on this or seen this before? Any help would be greatly appreciated!
-
Have you confirmed that ADDRESSBOOK is correctly pointing to the newly created record?
Try this :
Code:
msgbox ADDRESSBOOK.Text
Do this just before the MoveNext and see if it contains the FName value.
Without seeing how you set up the recordset its hard to tell, you have a Private Subroutine that is getting input from outside the scope of the routine itself (rs_assoc) which isn't good code practice.
-
If your database object exist you could do this.
Code:
With ListView
If rs_assoc.EOF = False Then
rs_assoc.MoveFirst
While Not rs_assoc.EOF
.ListItems.Add , "MN" & .ListItems.Count, rs_assoc.Fields("FName")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDA" & .ListItems.Count, rs_assoc.Fields("LName")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDB" & .ListItems.Count, rs_assoc.Fields("Address")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDB" & .ListItems.Count, rs_assoc.Fields("City")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDB" & .ListItems.Count, rs_assoc.Fields("State")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDB" & .ListItems.Count, rs_assoc.Fields("Zip")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDB" & .ListItems.Count, rs_assoc.Fields("WPhone")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDB" & .ListItems.Count, rs_assoc.Fields("HPhone")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDB" & .ListItems.Count, rs_assoc.Fields("CPhone")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDB" & .ListItems.Count, rs_assoc.Fields("Pager")
.ListItems.Item(.ListItems.Count).ListSubItems.Add , "CHDB" & .ListItems.Count, rs_assoc.Fields("EMail")
DoEvents
rs_assoc.MoveNext
Wend
End With
-
If it moves back to the calling sub, it's probably because it encounters an error, and the calling sub has an error handler. The error could be that the recordset contains a null value. You can't assign a null value to a listview's subitem, so it would go to the nearest error handler in the call stack.
-
This is how I build the recordset. Would it be better to put the recordset creation and to fill the listview control in one sub? Why is it bad practice to fill the recordset in one sub and fill the listview in another(Gen-X)?
Private Sub Gath_Assoc()
sql_assoc$ = "SELECT FName, LName, Address, City, State, Zip, WPhone, HPhone, CPhone, Pager, EMail FROM Assoc_Main WHERE Associates = " & rs_comp!associates
Set rs_assoc = db.OpenRecordset(sql_assoc$)
End Sub
-
I may be way off base with this one, but how many columns do you have defined? I am assuming that since I don't see any columnheaders.add in your code that you are defining them at design time. You must define a header for each column you use. But you probably already know this and I have just irritated you because I assumed you didn't, so ignore this reply.
-
On irritating answers...check that you've got it on Report style, too ;).
-
Yes, it is in report view and my headers match my columns. Here is the code again in order of execution:
Define column headers:
Private Sub SetColumns()
ListView.ColumnHeaders.Clear
ListView.ColumnHeaders.Add , , "First Name"
ListView.ColumnHeaders.Add , , "Last Name"
ListView.ColumnHeaders.Add , , "Address"
ListView.ColumnHeaders.Add , , "City"
ListView.ColumnHeaders.Add , , "State"
ListView.ColumnHeaders.Add , , "Zip"
ListView.ColumnHeaders.Add , , "Work Phone"
ListView.ColumnHeaders.Add , , "Home Phone"
ListView.ColumnHeaders.Add , , "Cell Phone"
ListView.ColumnHeaders.Add , , "Pager"
ListView.ColumnHeaders.Add , , "E-Mail"
End Sub
Create recordset:
Private Sub Gath_Assoc()
sql_assoc$ = "SELECT FName, LName, Address, City, State, Zip, WPhone, HPhone, CPhone, Pager, EMail FROM Assoc_Main WHERE Associates = " & rs_comp!associates
Set rs_assoc = db.OpenRecordset(sql_assoc$)
End Sub
Fill the listview:
Private Sub Fill_Assoc()
Dim ADDRESSBOOK As ListItem
ListView.ListItems.Clear
If rs_assoc.RecordCount <> 0 Then
While Not rs_assoc.EOF
DoEvents
Set ADDRESSBOOK = ListView.ListItems.Add(, "A" & rs_assoc.AbsolutePosition, rs_assoc.Fields("FName"))
ADDRESSBOOK.SubItems(1) = rs_assoc.Fields("LName")
ADDRESSBOOK.SubItems(2) = rs_assoc.Fields("Address")
ADDRESSBOOK.SubItems(3) = rs_assoc.Fields("City")
ADDRESSBOOK.SubItems(4) = rs_assoc.Fields("State")
ADDRESSBOOK.SubItems(5) = rs_assoc.Fields("Zip")
ADDRESSBOOK.SubItems(6) = rs_assoc.Fields("WPhone")
ADDRESSBOOK.SubItems(7) = rs_assoc.Fields("HPhone")
ADDRESSBOOK.SubItems(8) = rs_assoc.Fields("CPhone")
ADDRESSBOOK.SubItems(9) = rs_assoc.Fields("Pager")
ADDRESSBOOK.SubItems(10) = rs_assoc.Fields("EMail")
Set ADDRESSBOOK = Nothing
rs_assoc.MoveNext
Wend
End If
End Sub
Still cannot figure this one out. Hopefully someone else has ran into this!
-
Okay, okay, my mistake. It appears to be the way I was calling all these sub routines. It appears that I was trying to view the listview control before setting the columns, duh! Oh well, I guess that is how you learn....
I want to thank you all for your replys, they eventually led me to figuring this one out....!