?
Solved

Building a VBA Collection

Posted on 2006-11-17
6
Medium Priority
?
1,053 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
[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
  • 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

777 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