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

x
?
Solved

Building a VBA Collection

Posted on 2006-11-17
6
Medium Priority
?
1,055 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
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.

 

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

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…
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