Solved

Building a VBA Collection

Posted on 2006-11-17
6
1,042 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

760 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

21 Experts available now in Live!

Get 1:1 Help Now