PC Review


Reply
Thread Tools Rate Thread

automation error 440 when adding to collection

 
 
Ken
Guest
Posts: n/a
 
      8th Jul 2011
I have a collection built from data in a range, and want to augment
that collection with data from another range. I also want to keep
track of the the new data by putting it in a listbox. The following
code works as far as augmenting the list

Set R = Range("Table14")

we = Range("Week_ending").Value
Count = EmpList.Count

For i = 1 To R.Rows.Count

If R.Cells(i, 1).Value = we - 7 Then
Set E = New Employee
E.Badge = R.Cells(i, 3)
E.LName = R.Cells(i, 4)
E.FName = R.Cells(i, 5)
On Error Resume Next
Call EmpList.Add(E, CStr(R.Cells(i, 3)))
ListBox3.AddItem CStr(E.Badge) 'this should
only happen when E is actually added to EmpList
ListBox3.List(ListBox3.ListCount - 1, 1) = E.LName
50
End If

Next i

Since I don't want to add the employees to listbox3 if they are
already in the collection, I thought I should be able to make the
following simple change to the On Error Resume Next line

Set R = Range("Table14")

we = Range("Week_ending").Value
Count = EmpList.Count

For i = 1 To R.Rows.Count

If R.Cells(i, 1).Value = we - 7 Then
Set E = New Employee
E.Badge = R.Cells(i, 3)
E.LName = R.Cells(i, 4)
E.FName = R.Cells(i, 5)
On Error GoTo 50
Call EmpList.Add(E, CStr(R.Cells(i, 3)))
ListBox3.AddItem CStr(E.Badge)
ListBox3.List(ListBox3.ListCount - 1, 1) = E.LName
50
End If

Next i

This causes an "Automation Error" 440 which I can't seem avoid. I
have tried clearing the error and moving the On Error statement
around, but, the second time through the If-End If loop it always
crashes.

Is there a fix to this, or another way to keep track of the names that
get added?

Thanks

Ken
 
Reply With Quote
 
 
 
 
GS
Guest
Posts: n/a
 
      8th Jul 2011
Try...


Set R = Range("Table14")

we = Range("Week_ending").Value
Count = EmpList.Count

For i = 1 To R.Rows.Count

If R.Cells(i, 1).Value = we - 7 Then
Set E = New Employee
E.Badge = R.Cells(i, 3)
E.LName = R.Cells(i, 4)
E.FName = R.Cells(i, 5)
On Error Resume Next
Call EmpList.Add(E, CStr(R.Cells(i, 3)))
'this should only happen when E is actually added to EmpList
If Err = 0 Then
ListBox3.AddItem CStr(E.Badge)
ListBox3.List(ListBox3.ListCount - 1, 1) = E.LName
Else
Err.Clear 'reset for next iteration
End If
End If
Next i

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      8th Jul 2011
Garry

I tried that and had the same problem. The first time through it
works fine, identifies the err, skips a couple line, and goes through
again. When I debug.print the error, after resetting, is shows the
error is 0 the first time, the error is 0 the second time, then it has
the key alreadt used error like it should, but then still gives the
404 error rather than just skipping to line 50 as it did the first
time. I don't know what is different the second time through. I came
up with a work around that works quite well, but, I wish I understood
what was going as the following solution might not always work.

Set R = Range("Table14")

we = Range("Week_ending").Value
Count = EmpList.Count

For i = 1 To R.Rows.Count

If R.Cells(i, 1).Value = we - 7 Then
Set E = New Employee
E.Badge = R.Cells(i, 3)
E.LName = R.Cells(i, 4)
E.FName = R.Cells(i, 5)
On Error Resume Next
Call EmpList.Add(E, CStr(R.Cells(i, 3)))
' ListBox3.AddItem CStr(E.Badge) 'i gave
up on this
' ListBox3.List(ListBox3.ListCount - 1, 1) = E.LName
'and this
End If

Next i

For i = Count + 1 To EmpList.Count
ListBox3.AddItem EmpList(i).Badge
ListBox3.List(ListBox3.ListCount - 1, 1) = EmpList(i).LName
Next i


Thanks for your time on this, and your many other contributions to
this group.

Ken

On Jul 7, 11:04*pm, GS <g...@somewhere.net> wrote:
> Try...
>
> * Set R = Range("Table14")
>
> * we = Range("Week_ending").Value
> * Count = EmpList.Count
>
> * For i = 1 To R.Rows.Count
>
> * * If R.Cells(i, 1).Value = we - 7 Then
> * * * Set E = New Employee
> * * * E.Badge = R.Cells(i, 3)
> * * * E.LName = R.Cells(i, 4)
> * * * E.FName = R.Cells(i, 5)
> * * * On Error Resume Next
> * * * Call EmpList.Add(E, CStr(R.Cells(i, 3)))
> * * * 'this should only happen when E is actually added to EmpList
> * * * If Err = 0 Then
> * * * * ListBox3.AddItem CStr(E.Badge)
> * * * * ListBox3.List(ListBox3.ListCount - 1, 1) = E.LName
> * * * Else
> * * * * Err.Clear 'reset for next iteration
> * * * End If
> * * End If
> * Next i
>
> --
> Garry
>
> Free usenet access athttp://www.eternal-september.org
> ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      8th Jul 2011
Can you show the code for EmpList.Add()?

Also, why are you resetting E each time? Seems to me that this might be
causing some conflict since I don't see the connection between Employee
and EmpList.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      8th Jul 2011
Garry
EmpList is a collection of Employees. Employees is a collection
defined in my Class module as something with a few properties
including badge number, first and last name, and a bunch of man hour
fields. E is short lived employee that comes from a chunk of data and
is added to the collection if and only if it is not already a member
(badge number exists) of the collection. I use this technique a lot
to simply get a unique list of something. It works great, resuming
next and generating a unique collection; until I try to skip a couple
other steps like putting the skipped name in a listbox. There is no
code, or at least no code that I am aware of, to the EmpList.Add. It
is the method for adding to a collection that exists simply by virtue
of EmpList being a collection.
Ken



On Jul 8, 10:09*am, GS <g...@somewhere.net> wrote:
> Can you show the code for EmpList.Add()?
>
> Also, why are you resetting E each time? Seems to me that this might be
> causing some conflict since I don't see the connection between Employee
> and EmpList.
>
> --
> Garry
>
> Free usenet access athttp://www.eternal-september.org
> ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      9th Jul 2011
Ken expressed precisely :
> Garry
> EmpList is a collection of Employees. Employees is a collection
> defined in my Class module as something with a few properties
> including badge number, first and last name, and a bunch of man hour
> fields. E is short lived employee that comes from a chunk of data and
> is added to the collection if and only if it is not already a member
> (badge number exists) of the collection. I use this technique a lot
> to simply get a unique list of something. It works great, resuming
> next and generating a unique collection; until I try to skip a couple
> other steps like putting the skipped name in a listbox. There is no
> code, or at least no code that I am aware of, to the EmpList.Add. It
> is the method for adding to a collection that exists simply by virtue
> of EmpList being a collection.
> Ken
>
>
>
> On Jul 8, 10:09*am, GS <g...@somewhere.net> wrote:
>> Can you show the code for EmpList.Add()?
>>
>> Also, why are you resetting E each time? Seems to me that this might be
>> causing some conflict since I don't see the connection between Employee
>> and EmpList.
>>
>> --
>> Garry
>>
>> Free usenet access athttp://www.eternal-september.org
>> ClassicVB Users Regroup! comp.lang.basic.visual.misc


Ken,
Thanks for the details. This is what I expected and so was trying to
figure out if the problem lies in executing the Add method, OR if it
has to do with the ListBox. I can only guess as to where the ListBox is
(in a userform or worksheet) and that makes it hard to understand
exactly what's going on.

I reproduced the Employees collection and was able to add new members
as you were doing. I repeated a member that already existed and it
worked as expected (ie: didn't add the member to the ListBox. I don't
seem to be able to reproduce the problem using a ListBox on a userform.
Haven't tried using one on a worksheet, though.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      9th Jul 2011
Garry
My listbox is on a userform; specifically in a frame on a multipage on
a userform, to throw in a couple more potential, but unlikely,
culprits. I am using Excel 2007.
Ken

On Jul 8, 9:16*pm, GS <g...@somewhere.net> wrote:
> Ken expressed precisely :
>
>
>
>
>
> > Garry
> > EmpList is a collection of Employees. *Employees is a collection
> > defined in my Class module as something with a few properties
> > including badge number, first and last name, and a bunch of man hour
> > fields. *E is short lived employee that comes from a chunk of data and
> > is added to the collection if and only if it is not already a member
> > (badge number exists) of the collection. *I use this technique a lot
> > to simply get a unique list of something. *It works great, resuming
> > next and generating a unique collection; until I try to skip a couple
> > other steps like putting the skipped name in a listbox. *There is no
> > code, or at least no code that I am aware of, to the EmpList.Add. *It
> > is the method for adding to a collection that exists simply by virtue
> > of EmpList being a collection.
> > Ken

>
> > On Jul 8, 10:09*am, GS <g...@somewhere.net> wrote:
> >> Can you show the code for EmpList.Add()?

>
> >> Also, why are you resetting E each time? Seems to me that this might be
> >> causing some conflict since I don't see the connection between Employee
> >> and EmpList.

>
> >> --
> >> Garry

>
> >> Free usenet access athttp://www.eternal-september.org
> >> ClassicVB Users Regroup! comp.lang.basic.visual.misc

>
> Ken,
> Thanks for the details. This is what I expected and so was trying to
> figure out if the problem lies in executing the Add method, OR if it
> has to do with the ListBox. I can only guess as to where the ListBox is
> (in a userform or worksheet) and that makes it hard to understand
> exactly what's going on.
>
> I reproduced the Employees collection and was able to add new members
> as you were doing. I repeated a member that already existed and it
> worked as expected (ie: didn't add the member to the ListBox. I don't
> seem to be able to reproduce the problem using a ListBox on a userform.
> Haven't tried using one on a worksheet, though.
>
> --
> Garry
>
> Free usenet access athttp://www.eternal-september.org
> ClassicVB Users Regroup! comp.lang.basic.visual.misc- Hide quoted text -
>
> - Show quoted text -


 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      9th Jul 2011
Ken explained on 7/9/2011 :
> Garry
> My listbox is on a userform; specifically in a frame on a multipage on
> a userform, to throw in a couple more potential, but unlikely,
> culprits. I am using Excel 2007.
> Ken


Ok, Ken. Thanks! Unfortunatly, I hit the Close button without saving
and so will have to restart from scratch...! <ugh!>

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      10th Jul 2011
Ken,
I'm still unable to reproduce your error. I'm wondering how you're
managing the collection and its class instances. (I've been reviewing
this topic in both Excel VBA Programmer's Reference (Ch6) and Pro Excel
Development (Ch7) just to make sure I wasn't missing something)

My thoughts are that you might be having a problem with class
instanciation, so I recommend reviewing either publication if you have
them. I suspect you have the former since your code and class scenario
closely resembles it, but the latter has more detailed info about
managing collections using a class.

Sorry I can't be of more help...

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding validation fails with automation error PhilipAppsWork@gmail.com Microsoft Excel Programming 4 13th Feb 2008 08:30 PM
Client callback adding items to databound collection = crossthread error Steve K. Microsoft C# .NET 1 10th Jan 2008 10:22 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
Re: Automation Error(-2147467259) in Properties Collection Bob Phillips Microsoft Excel Programming 1 14th Apr 2005 07:08 PM
Re: Automation Error(-2147467259) in Properties Collection Bob Phillips Microsoft Excel Programming 0 14th Apr 2005 04:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:43 PM.