[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to check if collection is empty

Posted on 2009-07-07
2
Medium Priority
?
204 Views
Last Modified: 2012-05-07
This line...

            If mcolVendors(sVendor) Is Nothing Then

causes this error...

Run-time error '5':
Invalid procedure call or argument

The offending line is highlighted in a box of stars(*).

I had hoped that finding no items in the collection would return null but instead throws an error. Does anyone know how to check if an item is in a collection?
'Code in the worksheet class module:
 
Private Sub CommandButton1_Click()
    
    Dim wkb1 As Workbook, wkb2 As Workbook
    Dim lRowCounter As Long, lColumnCounter As Long
    Dim sFullName1 As String, sFileName1 As String, sFullName2 As String, sFileName2 As String
    Dim sVendor As String
    Dim dTotalCost As Double, dTotalSales As Double
 
    'sFullName1 = "\\msfs05.lowes.com\DATA1\SHARE\Lognet\Channel Change Process\13. IP&E Planning\Templates\Template ProForma Comp\JBK Examples\Data Collection Fields_v13 Routine.xls"
    sFullName1 = "C:\Documents and Settings\dpark\Desktop\Data Collection Fields_v13 Routine.xls"
    sFileName1 = sGetFileName(sFullName1)
    
    'sFullName2 = "\\msfs05.lowes.com\DATA1\SHARE\Lognet\Channel Change Process\13. IP&E Planning\Templates\Template ProForma Comp\JBK Examples\2009 L&G Fall Equipment_Program Sheet.xls"
    sFullName2 = "C:\Documents and Settings\dpark\Desktop\2009 L&G Fall Equipment_Program Sheet.xls"
    sFileName2 = sGetFileName(sFullName2)
      
    'Make sure both workbooks open
    If bIsWorkbookOpen(sFileName1) Then
        Set wkb1 = Workbooks(sFileName1)
        wkb1.Activate
    Else
        Set wkb1 = Workbooks.Open(Filename:=sFullName1)
        wkb1.Activate
    End If
        
    If bIsWorkbookOpen(sFileName2) Then
        Set wkb2 = Workbooks(sFileName2)
    Else
        Set wkb2 = Workbooks.Open(Filename:=sFullName2)
    End If
    
    Dim mcolVendors As New Collection
    
    lRowCounter = 20
    lColumnCounter = 2
    
    With wkb2.Worksheets("2009")
        Do Until IsEmpty(.Cells(lRowCounter, "B"))
            sVendor = .Cells(lRowCounter, "B").Value
        **************************************************************    
        **  If mcolVendors(sVendor) Is Nothing Then        ** 
        **************************************************************
                AddVendor sVendor, lRowCounter
            End If
                  
            mcolVendors(sVendor).TotalCost = .Cells(lRowCounter, "N").Value
            mcolVendors(sVendor).TotalSales = .Cells(lRowCounter, "O").Value
        Loop
    End With    
End Sub
        
'Code in the clsVendor class module:
 
Option Explicit
 
'Public TotalCost1 As Double
'Public TotalSales1 As Double
'Public PercentMargin As Double
 
Private dSumCost As Double
Private dSumSales As Double
 
Property Let Name(sVendor As String)
    Name = sVendor
End Property
 
'Sum TotalCost values
Property Let TotalCost(dTotalCost As Double)
    TotalCost = TotalCost + dTotalCost
End Property
 
'Sum TotalSales values
Property Let TotalSales(dTotalSales As Double)
    TotalSales = TotalSales + dTotalSales
End Property
 
'Return TotalCost
Property Get TotalCost() As Double
    TotalCost = TotalCost
End Property
 
'Return TotalSales
Property Get TotalSales() As Double
    TotalSales = TotalSales
End Property
 
Public Function PercentMargin() As Double
    GetPercentMargin = (TotalSales1 - TotalCost1) / TotalSales1
End Function
 
'Code in the modVendor standard module:
 
Option Explicit
 
Public mcolVendors As New Collection
 
Sub AddVendor(sVendor As String, lRowCounter As Long)
    Dim clsVendor As clsVendor
 
    With wkb2.Worksheets("2009")
        If mcolVendors(sVendor) Is Nothing Then
            Set clsVendor = New clsVendor
            clsVendor.Name = sVendor
'            clsVendor.TotalCost = .Cells(lRowCounter, "N").Value
'            clsVendor.TotalSales = .Cells(lRowCounter, "O").Value
            mcolVendors.Add clsVendor, clsVendor.Name
        Else
            MsgBox sVendor & " class object already exists!"
 
        End If
    End With
End Sub

Open in new window

0
Comment
Question by:ltdanp22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 24798400
Use the error to your advantage....

            Dim v As Variant

            sVendor = .Cells(lRowCounter, "B").Value
            On Error Resume Next
            v = mcolVendors(sVendor)
            If Err <> 0 Then
                AddVendor sVendor, lRowCounter
            End If
            On Error GoTo 0

Wayne
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24799107
If you don't want to have to trap the error then use a Dictionary object instead of a Collection:
http://msdn.microsoft.com/en-us/library/x4k5wbx4(VS.85).aspx

Then you can use the Exists() method:
http://msdn.microsoft.com/en-us/library/57hdf10z(VS.85).aspx

To use the Dictionary, add a reference to the "Microsoft Scripting Runtime".

Here is a simple Dictionary example:
Option Explicit
 
' Click on Project --> References and Check the "Microsoft Scripting Runtime" Entry.
 
Private dct As New Dictionary
 
Private Sub Form_Load()
    dct.Add "Item #1", "a"
    dct.Add "Item #2", "b"
    dct.Add "Item #3", "c"
    dct.Add "Item #4", "d"
End Sub
 
Private Sub Command1_Click()
    Dim key As String
    Dim value As Variant
    
    key = "Item #3"
    If dct.Exists(key) Then
        value = dct(key)
        MsgBox value, vbInformation, key
    Else
        MsgBox key, vbExclamation, "Key Not Found"
    End If
    
    key = "Item #5"
    If dct.Exists(key) Then
        value = dct(key)
        MsgBox value, vbInformation, key
    Else
        MsgBox key, vbExclamation, "Key Not Found"
    End If
End Sub

Open in new window

0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question