Solved

Building a VBA Collection

Posted on 2006-11-17
6
1,043 Views
Last Modified: 2009-12-16
Hello,
 
I normally build my Collections by just adding data to my Collection like this:

Public ContactNames As New Collection


I add records this way:

Application.Worksheets(3).Activate

For current_row_Contacts = 3 To 10000

        If Worksheets(3).Application.Range("Contacts_Cust_ID").Cells(current_row_Contacts) <> "" Then
            If Worksheets(3).Application.Range("Contacts_Cust_ID").Cells(current_row_Contacts).Value = CustID Then
                Set jbContacts = New JBSDKContacts

                jbContacts.Name = Worksheets(3).Application.Range("Contact_Name").Cells(current_row_Contacts)
                jbContacts.ContactTitle = Worksheets(3).Application.Range("Contact_Title").Cells(current_row_Contacts)

                jbCustomer.ContactNames.add jbContacts
               
            End If
        End If
Next



I'm getting a Run Time Error 13 - Type Mismatch when I try to read records from this collection and I want to buld my collection columns manually when I create the collection to make sure data types are correct if possible.



Thanks,
JMO9966
0
Comment
Question by:JMO9966
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:KnobiKnobi
ID: 17964925
how do you read the records (code?) ?

i tried your code (after i replaced "Contacts_Cust_ID" with fixed field (B1 for example)) and used    

 dim a_name as string      
 a_ name = jbCustomer.Item(1) .Name

to test, and it works fine.

0
 

Author Comment

by:JMO9966
ID: 17965234
Thanks,

I basically cycle thorugh the rows of a spreadsheet with that For Next Loop, this VBA code sits behind my Sheet1 of Excel Workbook.

  Dim jbCustomer As New JBSDKCustomer
  Dim jbContacts As New JBSDKContacts

For current_row_Contacts = 3 To 10000

        If Worksheets(3).Application.Range("Contacts_Cust_ID").Cells(current_row_Contacts) <> "" Then
            If Worksheets(3).Application.Range("Contacts_Cust_ID").Cells(current_row_Contacts).Value = CustID Then
                Set jbContacts = New JBSDKContacts

                jbContacts.Name = Worksheets(3).Application.Range("Contact_Name").Cells(current_row_Contacts)
                jbContacts.ContactTitle = Worksheets(3).Application.Range("Contact_Title").Cells(current_row_Contacts)

                jbCustomer.ContactNames.add jbContacts
               
            End If
        End If
Next


I have two class modules in my project.  JBSDKCustomer and JBSDKContacts.

I created my Collection as Public in the JBSDKCustomer class modules.

Public ContactNames As New Collection


 I read the data in Sheet 1 code and then call JBSDKCustomer to build XML from this data and then call a function in JBSDKContacts if the Customer record has a contact associated with it.

I get the type mismatch when I try to call this function in JBSDKContact from my JBSDKCustomer class module:

            For Each Contact In ContactNames
                If Contact.Name <> "" Then
                xmlCustomerAddRqNode.appendChild Contact.ContactAdd_Node(xmlRequestdoc)   **************** Type Mismatch error here
                End If
            Next Contact



Here's the function in JBSDKContacts


Public Function ContactAdd_Node(xmlRequestdoc As DOMDocument40) As IXMLDOMNode
'''''''''''''''''''''''''''''''''''''''''''''
' Create a RouteAddFromWorkCenter XML Node. '
'''''''''''''''''''''''''''''''''''''''''''''
  Dim result As IXMLDOMNode
 
  Set result = createJBNode(xmlRequestdoc, "ContactAdd")
   
    If ContactName <> "" Then
    result.appendChild createJBNode(xmlRequestdoc, "Name", Name)
    End If
 
  Set ContactAdd_Node = result

End Function



Thanks Knobi,
JMO9966
0
 
LVL 2

Accepted Solution

by:
KnobiKnobi earned 250 total points
ID: 17967357
Sorry, I can´t see it.

  For Each Contact In ContactNames
                If Contact.Name <> "" Then  
                 *** Ariving here means Collection filled successfully and reading record works to.
                       otherwise there would be no name

                 xmlCustomerAddRqNode.appendChild Contact.ContactAdd_Node(xmlRequestdoc)  
appendChild expects a "node" an ContactAdd_Node returns "node". seems ok.the open question is    is xmlReqeustDoc realy of type DOMDocument40
may be you will test the following to make sure which of the parameters cause the mismatch:
xmlCustomerAddRqNode.appendChild Contact.ContactAdd_Node(xmlRequestdoc)  to
  call Contact.ContactAdd_Node(xmlRequestdoc)
then stop execution there and step throu ContactAdd_Node function to see if this part works.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:JMO9966
ID: 17967918
Oh cool, I was trying to figure how to get into this function to step through it, I will use the call method as you mention.


Thanks Knobi,
JMO9966
0
 

Author Comment

by:JMO9966
ID: 17968553
Your not going to believe it, my problem was I copied this code from a different project of mine and the xml request doc was a diferent name.

bad:

call Contact.ContactAdd_Node(xmlRequestdoc)


good:

call Contact.ContactAdd_Node(Requestdoc)


I did learn a few things from you thought.

Thank You.
JMO9966
0
 
LVL 2

Expert Comment

by:KnobiKnobi
ID: 17969326
nice to hear
thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now