Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get started with a ListView? 1

Status
Not open for further replies.

beckwiga

Programmer
Mar 30, 2005
70
I am a newbie VBA programmer. I want to add a ListView box to my Form. I want the ListView to consist of the contents of my local table called ADDRESS. I'm not sure I know where to begin, for example, setting up the ADO and recordset information? Do I add this in the Private Sub ListView1_GotFocus()? My table is only like 3 columns and maybe a few dozen records. Anyone help get me started?
 
This is what I have so far... When I View my Form, the ListView just sits there as a big white empty box. No errors or anything. Clearly I'm not doing something right.



Private Sub ListView1_Load()

Dim LIX As ListItem
Dim rsAddress As Recordset
Dim iCount As Integer


lvwList.ListItems.Clear
lvwList.ColumnHeaders.Clear
Set rsAddress = Application.CurrentDb.OpenRecordset("ADDRESS")

For iCount = 0 To rsDisk.Fields.Count - 1
lvwList.ColumnHeaders.Add , , rsAddress.Fields(iCount).Name
Next

Do While Not rsAddress.EOF
For iCount = 0 To rsAddress.Fields.Count - 1
If iCount = 0 Then
Set LIX = lvwList.ListItems.Add
LIX.Text = rsAddress.Fields(iCount).Value & ""
Else
LIX.SubItems(iCount) = rsAddress.Fields(iCount).Value & ""
End If
Next
rsAddress.MoveNext
Loop
rsAddress.Close
Set rsAddress = Nothing


End Sub
 
Why not simply using a native access ListBox ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV. I want to be able to sort by column heading, add colors to the background of the listview, add icons, etc. i want to view it a Report view. I'm still not having any luck though.
 
I've been seaching the web for hours... I don't know why this isn't working. I am wondering if I don't have the ListView control registered correctly or something. I have VB.NET 2003 Standard and Visual Studio .NET 2002 Enterprise on this machine. In Access(2003), if I go to Active X Controls, Microsoft ListView 6.0 is registered. I used the regsvr32 to register it. I would think if it were not registered properly, I would get errors when I try to view my form and click on it or when it loads. If the code is wrong in my ListView, would the whitebox just sit there? I have no experience with these custom controls.
 
beckwiga,

If you have named your Listview control lvwList then the Load event handler must reflect this; i.e.

Code:
Private Sub lvwList_Load()
  ...
  ...
End Sub

Otherwise, the event will not fire.

Regards,
Mike
 
Mike -

Yeah, that helped out, thanks. Still having issues though, getting a Type Mismatch error. My code is now as follows:

Private Sub ListView0_Click()

Dim rs As Recordset
Dim db As Object
Dim iCount As Integer
Dim LIX As ListItem


ListView0.ListItems.Clear
ListView0.ColumnHeaders.Clear

Set db = CurrentDb
Set rs = db.OpenRecordset("ADDRESS")

For iCount = 0 To rs.Fields.Count - 1
ListView0.ColumnHeaders.Add , , rs.Fields(iCount).Name
Next

Do While Not rs.EOF
For iCount = 0 To rs.Fields.Count - 1
If iCount = 0 Then
Set LIX = ListView0.ListItems.Add
LIX.Text = rs.Fields(iCount).Value & ""
Else
LIX.SubItems(iCount) = rs.Fields(iCount).Value & ""
End If
Next
rs.MoveNext
Loop
rs.Close
Set rs = Nothing

End Sub



I am getting the Type Mismatch on the line Set rs = db.OpenRecordset("ADDRESS"). Do I need to declare a special type of Recordset or something? Not sure why this is not working. Also, how come I cannot declare something as a Database in VBA? I have declared my db as an Object. This is all still new to me.

 
Dim rs As DAO.Recordset
Dim db As Database

You need to reference the DAO 3.x library:
when in VBE menu Tools -> References ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top