Adding to Collection Object 
Author Message
 Adding to Collection Object

using A2k on Win98SE

I keep getting error 13 with this bit of code.  colButtons is a collection
object and looking at it in the Locals Window confirms that

*****
Private Sub UserForm_Initialize()
Dim x As Integer
Dim oCntrl As Control, oItem As Object

For Each oCntrl In Controls
    If Left$(oCntrl.Name, 3) = "btn" Then
        x = x + 1
     Set oItem = oCntrl.Object
        colButtons.Add oItem, x        '<===== Error 13 Data Type MisMatch
here
    End If
Next
...
****
I have also tried this without the oItem object like this

For Each oCntrl In Controls
    If Left$(oCntrl.Name, 3) = "btn" Then
        x = x + 1
        colButtons.Add oCntrl, x        '<===== Error 13 Data Type MisMatch
here
    End If
Next
****

What am I doing wrong here?

Thanks
Kevin



Thu, 16 Jun 2005 00:23:51 GMT  
 Adding to Collection Object
Hi Kevin,

As you are using Access, you may have more chance of getting an answer for
this on one of the Access newsgroups. This group deals with VBA in Word.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> using A2k on Win98SE

> I keep getting error 13 with this bit of code.  colButtons is a collection
> object and looking at it in the Locals Window confirms that

> *****
> Private Sub UserForm_Initialize()
> Dim x As Integer
> Dim oCntrl As Control, oItem As Object

> For Each oCntrl In Controls
>     If Left$(oCntrl.Name, 3) = "btn" Then
>         x = x + 1
>      Set oItem = oCntrl.Object
>         colButtons.Add oItem, x        '<===== Error 13 Data Type MisMatch
> here
>     End If
> Next
> ...
> ****
> I have also tried this without the oItem object like this

> For Each oCntrl In Controls
>     If Left$(oCntrl.Name, 3) = "btn" Then
>         x = x + 1
>         colButtons.Add oCntrl, x        '<===== Error 13 Data Type
MisMatch
> here
>     End If
> Next
> ****

> What am I doing wrong here?

> Thanks
> Kevin



Thu, 16 Jun 2005 01:45:29 GMT  
 Adding to Collection Object
Sorry that is Word.  I must have access on the brain.

Would that make a difference?.  The collection object is not part of the
Word Object Model, but of VBA.

By the way until now my VBA experience has been exclusively withen Access.

thanks,
Kevin

Quote:
> Hi Kevin,

> As you are using Access, you may have more chance of getting an answer for
> this on one of the Access newsgroups. This group deals with VBA in Word.

> --
> Regards
> Jonathan West - Word MVP
> MultiLinker - Automated generation of hyperlinks in Word
> Conversion to PDF & HTML
> http://www.multilinker.com
> Word FAQs at http://www.multilinker.com/wordfaq
> Please post any follow-up in the newsgroup. I do not reply to Word
questions
> by email



> > using A2k on Win98SE

> > I keep getting error 13 with this bit of code.  colButtons is a
collection
> > object and looking at it in the Locals Window confirms that

> > *****
> > Private Sub UserForm_Initialize()
> > Dim x As Integer
> > Dim oCntrl As Control, oItem As Object

> > For Each oCntrl In Controls
> >     If Left$(oCntrl.Name, 3) = "btn" Then
> >         x = x + 1
> >      Set oItem = oCntrl.Object
> >         colButtons.Add oItem, x        '<===== Error 13 Data Type
MisMatch
> > here
> >     End If
> > Next
> > ...
> > ****
> > I have also tried this without the oItem object like this

> > For Each oCntrl In Controls
> >     If Left$(oCntrl.Name, 3) = "btn" Then
> >         x = x + 1
> >         colButtons.Add oCntrl, x        '<===== Error 13 Data Type
> MisMatch
> > here
> >     End If
> > Next
> > ****

> > What am I doing wrong here?

> > Thanks
> > Kevin



Thu, 16 Jun 2005 06:07:46 GMT  
 Adding to Collection Object
Kevin

How have you declared your collection, colButtons, and have you
instantiated the collection somewhere by:

  Dim colButtons as Collection

  Set colButtons = New Collection

Malc
  www.dragondrop.com



Fri, 17 Jun 2005 01:27:32 GMT  
 Adding to Collection Object
Yes Malcolm, I put Dim colButtons as New Collection in the declarations
section.

But I have found my error.  It was staring me in the face but I was focused
on the wrong thing.

The second argument of the Add method needed to be a string and I was
passing an integer.

Thanks guys,

Kevin

The corrected code:
****
Private Sub UserForm_Initialize()
Dim x As Integer
Dim oCntrl As Control

For Each oCntrl In Controls
    If Left$(oCntrl.Name, 3) = "btn" Then
        x = x + 1
     Set oItem = oCntrl.Object
        colButtons.Add oCntrl, Cstr(x)        '<===== This is how it should
be done
    End If
Next
...


Quote:
> Kevin

> How have you declared your collection, colButtons, and have you
> instantiated the collection somewhere by:

>   Dim colButtons as Collection

>   Set colButtons = New Collection

> Malc
>   www.dragondrop.com



Fri, 17 Jun 2005 01:54:56 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. ? Collection add object -> usercontrol add control

2. ? Collection add object -> usercontrol add control

3. Adding objects to a collection object

4. How do you add object to a collection

5. More on Adding a class object to collections

6. Adding user defined types to a collection object

7. Adding objects to collections

8. VB5.0 Type mismatch when adding object to collection

9. Storing objects added to a collection in a database

10. Adding Objects to Collections

11. Adding User-Defined Type to a Collection Object

12. Collection.Add and Collection.Item question?

 

 
Powered by phpBB® Forum Software