Prev: Reassigning numbers
Next: Returning Only Blank Values
From: Mat on 10 Mar 2010 18:28 Hi all, When opening a form there appears to me to be two ways to do so: Option 1: docmd.openform "Form1" Option 2: dim frm as Form set frm = New Form1 OK so my question is: Can I combine the two options? dim frm as Form docmd.OpenForm strFormName set frm = Forms(Forms.Count-1) 'the newest form created The reason I don't think I can is because when I use option1 I can click design view on the form but when I use option 2 design view is disabled. I want to combine the two options because using option 1 I can open a form via a string whereas using option 2 I have to specify which form I want to create an instance of.
From: Marshall Barton on 10 Mar 2010 23:43 Mat wrote: >When opening a form there appears to me to be two ways to do so: > >Option 1: >docmd.openform "Form1" > >Option 2: >dim frm as Form >set frm = New Form1 > >OK so my question is: Can I combine the two options? >dim frm as Form >docmd.OpenForm strFormName >set frm = Forms(Forms.Count-1) 'the newest form created > >The reason I don't think I can is because when I use option1 I can >click design view on the form but when I use option 2 design view is >disabled. > >I want to combine the two options because using option 1 I can open a >form via a string whereas using option 2 I have to specify which form >I want to create an instance of. Couple of things: Option 2 needs to use: set frm = New Form_Form1 and there is no guarantee that Forms(Forms.Count-1) actually is the newest form created. Note that unless you want to open multiple instances of the same form, I know of no good reason to use: Set frm = New Form_... but then don't forget to follow that with: frm.Visible = True if you want to see the newly opened instance of the form. If you use OpenForm then you can use: Set frm = Forms("Form1") to refer to the form via a string. When you use: Set frm = New Form_Form1 the form will close when the procedure exits because the object variable, frm, will go out of scope. To prevent that you should create and manage your own collection of the open instances of the form, kind of like the Forms collection. For details about that, see: http://allenbrowne.com/ser-35.html -- Marsh
From: Mat on 11 Mar 2010 00:15 Thanks for the corrections I was sloppy with the notation. >> and there is no guarantee that Forms(Forms.Count-1) I didn't know that. That gives me something to test, because I might need to search through the collection for the right form. The reason is that my collection of forms is not the same form. Depending on what is clicked one of several forms will open and get stored in the collection. This has led me to create a select case statement based upon a string. select strFormName case "A": set frm = new Form_Form1 case "B": set frm = new Form_Form2 'add to the collection to preserve it However this means updating the case statement with every new form I wish to create. I was hoping to avoid the select case statement by opening the form using the docmd.openform statement and pass the strFormName to that then point to it then add it to the collection.
From: Allen Browne on 11 Mar 2010 09:36 Mat, if you don't need to have multiple instances of a form open, why do you need your own collection? Access maintains a collection of open forms. It's called Forms. You can locate a form if you know its name with: Forms(strFormName) -- Allen Browne - Microsoft MVP. Perth, Western Australia Tips for Access users - http://allenbrowne.com/tips.html Reply to group, rather than allenbrowne at mvps dot org. "Mat" <matthew.kay(a)optusnet.com.au> wrote in message news:9d9f9fbd-e4f3-47b2-9e2f-1cd35ffd9219(a)f17g2000prh.googlegroups.com... > Thanks for the corrections I was sloppy with the notation. > >>> and there is no guarantee that Forms(Forms.Count-1) > > I didn't know that. That gives me something to test, because I might > need to search through the collection for the right form. > > The reason is that my collection of forms is not the same form. > Depending on what is clicked one of several forms will open and get > stored in the collection. This has led me to create a select case > statement based upon a string. > select strFormName > case "A": > set frm = new Form_Form1 > case "B": > set frm = new Form_Form2 > 'add to the collection to preserve it > > However this means updating the case statement with every new form I > wish to create. > > I was hoping to avoid the select case statement by opening the form > using the docmd.openform statement and pass the strFormName to that > then point to it then add it to the collection.
From: Marshall Barton on 11 Mar 2010 11:36
Mat wrote: >Thanks for the corrections I was sloppy with the notation. > >>> and there is no guarantee that Forms(Forms.Count-1) > >I didn't know that. That gives me something to test, because I might >need to search through the collection for the right form. > >The reason is that my collection of forms is not the same form. >Depending on what is clicked one of several forms will open and get >stored in the collection. This has led me to create a select case >statement based upon a string. >select strFormName >case "A": > set frm = new Form_Form1 >case "B": >set frm = new Form_Form2 >'add to the collection to preserve it > >However this means updating the case statement with every new form I >wish to create. > >I was hoping to avoid the select case statement by opening the form >using the docmd.openform statement and pass the strFormName to that >then point to it then add it to the collection. I never heard of a way to create a class instance without using the literal class name with New. I think it would be easier to have a separate procedure with its own collection for each form with multiple instances. OTOH, its your code and if you prefer combining all of them in a single collection, who am I to object. Either way, I believe you will need to add/modify some code for each form. -- Marsh |