Solved

Building a VBA Collection

Posted on 2006-11-17
6
1,045 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

809 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