Setting a Form object to an external form

J

JonWayn

I keep getting "Type mismatch" error when I try the above.

Sub UpdateCounty(County As String)
Dim db As Database, frmMain As Form, mdl As Module, cont As Object
Dim x%

Set db = OpenDatabase(County)
Set frmMain = db.Containers("Forms").Documents("Main")
'more codes
End Sub


I have tried several other ways using that containers and Documents
collection but these dont seem to lead to a path to the actual forms
contained in the database. Is there any suggestion out there on how to get
external forms?
 
T

TC

You need: set frm = Forms("Main")
or: set frm = Forms![Main]

The Forms *collection* (used above) is the collection of all >open< forms.

The forms *container* (used in your code) is a container of documents, each
of which is a form >defined in< your database (but not necessarily open).

Also, you are well advised to say DBEngine.OpenDatabase(...), not just
OpenDatabase(...). This avoids some DAO licencing errors that can occur on
some PCs.

HTH,
TC
 
T

TC

Further to this:

On reflection, I see that you are probably not primarily interested in the
open< forms (as returned by the container) - you are interested in >all<
the forms (as returned by the collection).

If so, make one change to your existing code: dim frmMain as DOCUMENT, not
as FORM. The members of the Forms *collection*, are "dim as Form" things.
The members of the Forms *container*, are "dim as Document" things.

If you want to get the properties of all the forms defined in the database,
you could iterate the Forms *container*, then for each Document (form)
within that container, do a DoCmd.OpenForm on the document (form) name, to
open that form in design view. *Then* you could reference that form in the
Forms *container*, to get its properties. Then, DoCmd.Close that form, &
repeat the process for the next document (form) in the Forms container.

HTH!
TC


TC said:
You need: set frm = Forms("Main")
or: set frm = Forms![Main]

The Forms *collection* (used above) is the collection of all >open< forms.

The forms *container* (used in your code) is a container of documents, each
of which is a form >defined in< your database (but not necessarily open).

Also, you are well advised to say DBEngine.OpenDatabase(...), not just
OpenDatabase(...). This avoids some DAO licencing errors that can occur on
some PCs.

HTH,
TC


JonWayn said:
I keep getting "Type mismatch" error when I try the above.

Sub UpdateCounty(County As String)
Dim db As Database, frmMain As Form, mdl As Module, cont As Object
Dim x%

Set db = OpenDatabase(County)
Set frmMain = db.Containers("Forms").Documents("Main")
'more codes
End Sub


I have tried several other ways using that containers and Documents
collection but these dont seem to lead to a path to the actual forms
contained in the database. Is there any suggestion out there on how to get
external forms?
 
J

JonWayn

I cannot use any of the methods you suggested because the form I am
interested in is in a closed remote database. Therefore Forms("Main") or
Forms!Main will not work.


TC said:
You need: set frm = Forms("Main")
or: set frm = Forms![Main]

The Forms *collection* (used above) is the collection of all >open< forms.

The forms *container* (used in your code) is a container of documents, each
of which is a form >defined in< your database (but not necessarily open).

Also, you are well advised to say DBEngine.OpenDatabase(...), not just
OpenDatabase(...). This avoids some DAO licencing errors that can occur on
some PCs.

HTH,
TC


JonWayn said:
I keep getting "Type mismatch" error when I try the above.

Sub UpdateCounty(County As String)
Dim db As Database, frmMain As Form, mdl As Module, cont As Object
Dim x%

Set db = OpenDatabase(County)
Set frmMain = db.Containers("Forms").Documents("Main")
'more codes
End Sub


I have tried several other ways using that containers and Documents
collection but these dont seem to lead to a path to the actual forms
contained in the database. Is there any suggestion out there on how to get
external forms?
 
D

Douglas J. Steele

Set frmMain = db.Containers("Forms").Documents("Main") doesn't open a form.
You need to explicity open the form, then set a reference to the form you
just opened. I haven't tested, but I believe you're going to need to use
Automation to do this

Sub UpdateCounty(County As String)
Dim objAccess As Access.Application
Dim frmMain As Form

Set objAccess = New Access.Application
With objAccess
.OpenCurrentDatabase County
.DoCmd.OpenForm FormName := "Main"
Set frmMain = .Forms("Main")
End With

' more code

With objAccess
.DoCmd.Close acForm, "Main", acSaveNo
.CloseCurrentDatabase
End With
Set objAccess = Nothing

End Sub

To learn more about using Automation with Access, check out:

ACC: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=147816
ACC2000: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=210111


--
Doug Steele, Microsoft Access MVP



JonWayn said:
I cannot use any of the methods you suggested because the form I am
interested in is in a closed remote database. Therefore Forms("Main") or
Forms!Main will not work.


TC said:
You need: set frm = Forms("Main")
or: set frm = Forms![Main]

The Forms *collection* (used above) is the collection of all >open< forms.

The forms *container* (used in your code) is a container of documents, each
of which is a form >defined in< your database (but not necessarily open).

Also, you are well advised to say DBEngine.OpenDatabase(...), not just
OpenDatabase(...). This avoids some DAO licencing errors that can occur on
some PCs.

HTH,
TC


JonWayn said:
I keep getting "Type mismatch" error when I try the above.

Sub UpdateCounty(County As String)
Dim db As Database, frmMain As Form, mdl As Module, cont As Object
Dim x%

Set db = OpenDatabase(County)
Set frmMain = db.Containers("Forms").Documents("Main")
'more codes
End Sub


I have tried several other ways using that containers and Documents
collection but these dont seem to lead to a path to the actual forms
contained in the database. Is there any suggestion out there on how to get
external forms?
 
T

TC

He should be able to do it the way I suggested in my additional post,
without using automation :)

TC


Douglas J. Steele said:
Set frmMain = db.Containers("Forms").Documents("Main") doesn't open a form.
You need to explicity open the form, then set a reference to the form you
just opened. I haven't tested, but I believe you're going to need to use
Automation to do this

Sub UpdateCounty(County As String)
Dim objAccess As Access.Application
Dim frmMain As Form

Set objAccess = New Access.Application
With objAccess
.OpenCurrentDatabase County
.DoCmd.OpenForm FormName := "Main"
Set frmMain = .Forms("Main")
End With

' more code

With objAccess
.DoCmd.Close acForm, "Main", acSaveNo
.CloseCurrentDatabase
End With
Set objAccess = Nothing

End Sub

To learn more about using Automation with Access, check out:

ACC: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=147816
ACC2000: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=210111


--
Doug Steele, Microsoft Access MVP



JonWayn said:
I cannot use any of the methods you suggested because the form I am
interested in is in a closed remote database. Therefore Forms("Main") or
Forms!Main will not work.


TC said:
You need: set frm = Forms("Main")
or: set frm = Forms![Main]

The Forms *collection* (used above) is the collection of all >open< forms.

The forms *container* (used in your code) is a container of documents, each
of which is a form >defined in< your database (but not necessarily open).

Also, you are well advised to say DBEngine.OpenDatabase(...), not just
OpenDatabase(...). This avoids some DAO licencing errors that can
occur
on to
get
 
D

Douglas J. Steele

Don't think so. All you can get from the Documents in the Forms Container
collection is the names of the forms. You need to use DoCmd.OpenForm to open
the form so that it's in the Forms collection itself, and, AFAIK, using
DoCmd.OpenForm will only let you open forms in the current database, not a
related one.

--
Doug Steele, Microsoft Access MVP



TC said:
He should be able to do it the way I suggested in my additional post,
without using automation :)

TC


Douglas J. Steele said:
Set frmMain = db.Containers("Forms").Documents("Main") doesn't open a form.
You need to explicity open the form, then set a reference to the form you
just opened. I haven't tested, but I believe you're going to need to use
Automation to do this

Sub UpdateCounty(County As String)
Dim objAccess As Access.Application
Dim frmMain As Form

Set objAccess = New Access.Application
With objAccess
.OpenCurrentDatabase County
.DoCmd.OpenForm FormName := "Main"
Set frmMain = .Forms("Main")
End With

' more code

With objAccess
.DoCmd.Close acForm, "Main", acSaveNo
.CloseCurrentDatabase
End With
Set objAccess = Nothing

End Sub

To learn more about using Automation with Access, check out:

ACC: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=147816
ACC2000: Using Microsoft Access as an Automation Server
http://support.microsoft.com/?id=210111


--
Doug Steele, Microsoft Access MVP



JonWayn said:
I cannot use any of the methods you suggested because the form I am
interested in is in a closed remote database. Therefore Forms("Main") or
Forms!Main will not work.


You need: set frm = Forms("Main")
or: set frm = Forms![Main]

The Forms *collection* (used above) is the collection of all >open< forms.

The forms *container* (used in your code) is a container of documents,
each
of which is a form >defined in< your database (but not necessarily open).

Also, you are well advised to say DBEngine.OpenDatabase(...), not just
OpenDatabase(...). This avoids some DAO licencing errors that can
occur
on
some PCs.

HTH,
TC


I keep getting "Type mismatch" error when I try the above.

Sub UpdateCounty(County As String)
Dim db As Database, frmMain As Form, mdl As Module, cont As Object
Dim x%

Set db = OpenDatabase(County)
Set frmMain = db.Containers("Forms").Documents("Main")
'more codes
End Sub


I have tried several other ways using that containers and Documents
collection but these dont seem to lead to a path to the actual forms
contained in the database. Is there any suggestion out there on
how
 
T

TC

Ah! I missed the "related" bit.

TC


Douglas J. Steele said:
Don't think so. All you can get from the Documents in the Forms Container
collection is the names of the forms. You need to use DoCmd.OpenForm to open
the form so that it's in the Forms collection itself, and, AFAIK, using
DoCmd.OpenForm will only let you open forms in the current database, not a
related one.

--
Doug Steele, Microsoft Access MVP



TC said:
He should be able to do it the way I suggested in my additional post,
without using automation :)

TC
Forms("Main")
or
Forms!Main will not work.


You need: set frm = Forms("Main")
or: set frm = Forms![Main]

The Forms *collection* (used above) is the collection of all open<
forms.

The forms *container* (used in your code) is a container of documents,
each
of which is a form >defined in< your database (but not necessarily
open).

Also, you are well advised to say DBEngine.OpenDatabase(...), not just
OpenDatabase(...). This avoids some DAO licencing errors that can occur
on
some PCs.

HTH,
TC


I keep getting "Type mismatch" error when I try the above.

Sub UpdateCounty(County As String)
Dim db As Database, frmMain As Form, mdl As Module, cont As Object
Dim x%

Set db = OpenDatabase(County)
Set frmMain = db.Containers("Forms").Documents("Main")
'more codes
End Sub


I have tried several other ways using that containers and Documents
collection but these dont seem to lead to a path to the actual forms
contained in the database. Is there any suggestion out there on
how
to
get
external forms?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top