By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

Moving listview to module help

P: 4
Hi guys

I;m trying to move all my Listview content into a module because I have to call it several times and it becomes annoying having to type it a number of times. I'm a little stuck though. I've managed to move most of the listview code onto a module but I keep getting errors. See the code below:

Expand|Select|Wrap|Line Numbers
  1. Public Sub AddItem(lvwListView1 as listview)
  2. ConnectDB
  3. Dim lstNew As ListItem
  4. Set rs = New ADODB.Recordset
  5.     rs.Open "SELECT * From Product Where ProductID like '" & ProductID & "%'", cn, adOpenDynamic, adLockOptimistic
  6.     'add to first column
  7.     Set lstNew = lvwListView1.ListItems.Add(, , rs.Fields("ProductID").Value)
  8.     'add to next column
  9.     lstNew.ListSubItems.Add , , rs.Fields("ProductName").Value
  10.     lstNew.ListSubItems.Add , , Quantity
  11.     lstNew.ListSubItems.Add , , rs.Fields("UnitPrice").Value
  12.     lstNew.ListSubItems.Add , , (rs.Fields("UnitPrice").Value * Val(Quantity))
  13. rs.Close
  14. End Sub
Expand|Select|Wrap|Line Numbers
  1. Public Sub ReRunListview(lvwListView as listview)
  2.     Dim i As Long
  3.     Dim lvwItem As ListItem
  4.     For i = 1 To lvwListView1.ListItems.Count
  5.         Set lvwItem = lvwListView.ListItems.Add(, , lvwListView1.ListItems.item(i).Text)
  6.         lvwItem.SubItems(1) = lvwListView1.ListItems(i).SubItems(1)
  7.         lvwItem.SubItems(2) = lvwListView1.ListItems(i).SubItems(2)
  8.         lvwItem.SubItems(3) = lvwListView1.ListItems(i).SubItems(3)
  9.         lvwItem.SubItems(4) = lvwListView1.ListItems(i).SubItems(4)
  10.     Next
  11. End Sub
The AddItem function will get the actual data from the database. Then I will call the ReRunListview function whenever I want to call it from another form.
But, when I run the code, the AddItem function works fine. It's when I get to the 'for statement' in the ReRunListview function I get the following error:

Runtime error 91 - Object variable or With block variable not set.
Any help will be greatly appreciated. :)
Apr 29 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 5K+
P: 8,434
All I can think of is that you must have called it without correctly passing a listview control, or something.
Apr 30 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.