sueb 379
Contributor
I have a collection manager (managing instances of forms) that was working fine, but I broke it and, since it was developed by someone else, I don't understand it well, so I don't know how to fix it.
The problem is that when I try to open another instance of a form, I get the runtime error 457, saying "This key is already associated with an element of this collection" on line 27 of the following code: - Function OpenAClient(frmForm As Form, _
-
frmCaption As String, _
-
frmCollection As Collection, _
-
Optional strFilter As String = "")
-
' Purpose: Open a new instance of the specified form, manage its properties, and append it
-
' to the specified collection
-
-
' Open a new instance and set a caption
-
frmForm.Caption = frmCaption & " " & frmCollection.Count & ", opened " & Now()
-
-
' Set the form's editing mode according to the current user's privileges
-
If ReadWrite Then
-
frmForm.RecordsetType = conDynaset
-
Else
-
frmForm.RecordsetType = conSnapshot
-
End If
-
-
' Use a pointer, if one was supplied
-
If strFilter & "" <> "" Then
-
frmForm.Filter = strFilter
-
frmForm.FilterOn = True
-
Else
-
frmForm.FilterOn = False
-
End If
-
-
' Append it to the specified collection and display it
-
frmCollection.Add Item:=frmForm, Key:=CStr(frmForm.Hwnd)
-
frmForm.Visible = True
-
-
End Function
-
I originally had just one collection of forms, but later needed this same functionality for other forms, so I made it a common routine that accepts the collection to handle (instead of hard-coding the collection within it). That's what broke it, I think. I suspect that the property .Hwnd (what does that stand for, anyway?) is not getting incremented; in fact, I suspect that the collection is not getting returned out of this function. I don't know how to specify an "in out" parameter, but I'm thinking that's what needs to happen?
Here's an example of how I call it: - Function OpenAnOverviewClient(Optional strFilter As String = "")
-
OpenAClient Form_Patient_IUR_Overview, "PAT", clnOverviewClient, strFilter
-
End Function
-
@sueb, I'm really pressed for time, but I threw together something for you to look at, that should be workable.
9 4130 NeoPa 32,556
Recognized Expert Moderator MVP
Another question of quite enormous scope again I'm afraid Sue.
What I can tell you is that to pass a parameter to a procedure which can be used by the procedure as well as updated (such that the calling code has the updated state of the parameter when it's finished) you use the ByRef keyword. Here's an illustration : - Private Sub TestByRef(ByRef intX As Integer)
-
intX = intX + 10
-
End Sub
The following code illustrates this working : - Dim intA As Integer
-
-
intA = 25
-
Call TestByRef(intA)
-
Debug.Print intA
The value printed in the Immediate Pane would be 35. Not 25 as you would expect without ByRef being specified.
sueb 379
Contributor
I added "ByRef" to the parameter frmCollection, but still got the same error. What is happening with that .Hwnd property anyway? What is it?
ADezii 8,834
Recognized Expert Expert
The problem is that your Function never actually creates a New Instance of the Form passed to it as an Argument. This is only accomplished by using the New Keyword, as in: - Dim frm As Form_frmCustomers
-
Set frm = New Form_frmCustomers
-
-
'Assuming your Collection has already been Defined
-
frmForm.Caption = "Instance Number " & (frmCollection.Count + 1) & ", opened " & Now()
-
-
' Append it to the specified collection and display it
-
frmCollection.Add Item:=frm, Key:=CStr(frm.hWnd)
-
frmForm.Visible = True
P.S. - The hWnd Property of a Form is a Unique Long Integer Value, assigned by Microsoft Windows to the Current Window (in this case each Form Instance). Since the 'Key' Component of a Collection must be a Unique Value for each Element, hWnd is a Logical choice.
sueb 379
Contributor
But how do I pass in to the function the different forms that I want it to manage? I have four different form collections that I want to manage in the same way, without having to duplicate code. In the example, the Dim statement is serving a single form type.
ADezii 8,834
Recognized Expert Expert @sueb, I'm really pressed for time, but I threw together something for you to look at, that should be workable.
sueb 379
Contributor @ADezii, thanks a bunch--I'll look at it as soon as I get a chance!
sueb 379
Contributor @ADezii, this looks like it may have done the trick! So, basically, the change is to add, to each calling routine, a Dim and a Set statement to create the object that I'm sending in to the collection manager.
Thanks--I hadn't really understood the scope of the object correctly, but this makes it clearer.
sueb 379
Contributor @NeoPa, thanks for the explanation of the meaning and use of the .Hwnd property--that helps my basic understanding of the objects being managed.
NeoPa 32,556
Recognized Expert Moderator MVP
I'm glad things seem to be sorted out now.
I think my point was about the ByRef actually, but that's just being precise ;-)
I think you've already got what you need from ADezii, but collections can be managed within a procedure if one of the arguments to the procedure is a collection object. In such cases the ByRef effect is automatic anyway of course.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: m. pollack. |
last post by:
Hi all,
Is there any information to be had about the "Object
Collection Editor" that appears when you click on a
collection property in the PropertyGrid control? I have a
class that maintains a...
|
by: ron |
last post by:
Hi,
I currently have a jagged array as such.
int myJArray = new ;
In my constructor i initialize the array of arrays like
such.
for(int i=0; i<21; i++)
{
myJArray = new int;
|
by: Alfred Taylor |
last post by:
I'm testing the waters of n-tier development and I ran into a scenario that
I'm not sure what the best solution would be. I have a Company object which
contains a collection of contacts retrieved...
|
by: Weston Weems |
last post by:
I've got a collection of objects say fruit. and fruit has a fairly decent
heirachial structure not just flat props.
I've got my repeater to bind to my fruit collection just fine, but what I
want...
|
by: Nak |
last post by:
Hi there,
I am trying to make a collection that will work in the "Object
collection editor" dialog. I have implimeneted ICollection, IList and
IEnumerable and now I have an "unnamed"...
|
by: ECathell |
last post by:
I have an object called box. It has the following properties
BoxType
BoxTare
BoxDescription
it is in a Namespace of x.database.Boxes
Box is a row in my database, but in my UI I will be...
|
by: a |
last post by:
Q. Unable to get a Profile custom (object) collection to bind to
GridView,etc (IList objects)?
This is my first custom object so I may be doing something rather simple,
wrong, or it may be...
|
by: Jon |
last post by:
Hello everyone.
If I have a collection, that contains objects of a different type, but all
inherit from the same base class, and this base class has an interface on it,
can I bind the collection...
|
by: Torsten Z |
last post by:
Hi,
I have tried several things to passing an array of objects or a Object
Collection from .NET to VB6 but I can't get it work. Passing one object
works fine, but more than one not. Has anybody...
|
by: gasfusion |
last post by:
Hey guys.
I'm building an object collection which will be a part of a Data Access Layer i am currently working on. However, i am having some issues iterating through a collection.
This is what...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |