Allforms Collection in another db (A2007)

J

JimS

I want to walk through the Allforms collection (and the querydefs, etc
collections) from one db("object") to another("source") (both A2007). I want
to use the last modified date from the source db to populate a listbox on the
object db. I'll then ask the user to select the forms (queries, etc.) he
wants to transfer to the object db. Similar to the Access Wizard, except the
Access Import wizard doesn't tell me anything about the forms, queries, etc.
I'm transferring.

Anyway, how do I refer to the forms collection on another database using
vba? It appears I use the "OpenDatabase" method, but I haven't gotten much
farther.

Thanks in advance!
 
D

Dirk Goldgar

JimS said:
I want to walk through the Allforms collection (and the querydefs, etc
collections) from one db("object") to another("source") (both A2007). I
want
to use the last modified date from the source db to populate a listbox on
the
object db. I'll then ask the user to select the forms (queries, etc.) he
wants to transfer to the object db. Similar to the Access Wizard, except
the
Access Import wizard doesn't tell me anything about the forms, queries,
etc.
I'm transferring.

Anyway, how do I refer to the forms collection on another database using
vba? It appears I use the "OpenDatabase" method, but I haven't gotten much
farther.


You can't get at the "All..." collections of another database without
opening the database in an instance of Access, because those collections are
properties of the CurrentProject and CurrentData objects, which are in turn
properties of the Access Application object. If you must, you can open the
other database in a separate instance of Access, and get at the collections
in that instance by automation. For this approach, you would not be using
the DAO OpenDatabase method. But it's a lot of overhead to incur for what
you want to do.

Instead, you can use the OpenDatabase method to open a DAO Database object,
and use various collections belonging to the Database object to get the
information you want. It's not quite as tidy as the All... collections,
because the appropriate collections vary according to the type of object.

Suppose you wanted to build a list of all the tables, queries, forms,
reports, macros, and modules in a database. Suppose you have a text box
named "txtDBPath" on a form, where the user has entered the path to the
database in question. Suppose you also have a list box named "lstObjects",
with three columns for the object type, object name, and last-updated date
of each object. Then you might have a function like this to list the
objects in the database:

'------ start of code ------
Function ListObjects()

On Error GoTo Err_Handler

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim qdf As DAO.QueryDef
Dim cnt As DAO.Container
Dim doc As DAO.Document
Dim strObjType As String
Dim strObjName As String
Dim strObjDate As String

' Clear the list box, to start with.
Me.lstObjects.RowSource = ""

If IsNull(Me.txtDBPath) Then
Exit Function
End If

DoCmd.Hourglass True

Set db = DBEngine.OpenDatabase(Me.txtDBPath, , True)

' Add all tables to the list box.
strObjType = "Table"
For Each tdf In db.TableDefs
strObjName = tdf.Name
If Left(strObjName, 4) <> "MSys" Then
strObjDate = Format(tdf.LastUpdated, "short date")
Me.lstObjects.AddItem _
strObjType & ";" & strObjName & ";" & strObjDate
End If
Next tdf

' Add all queries to the list box.
strObjType = "Query"
For Each qdf In db.QueryDefs
strObjName = qdf.Name
If Left(strObjName, 3) <> "~sq" Then
strObjDate = Format(qdf.LastUpdated, "short date")
Me.lstObjects.AddItem _
strObjType & ";" & strObjName & ";" & strObjDate
End If
Next qdf

' Add all forms to the list box.
strObjType = "Form"
Set cnt = db.Containers("Forms")
For Each doc In cnt.Documents
strObjName = doc.Name
strObjDate = Format(doc.LastUpdated, "short date")
Me.lstObjects.AddItem _
strObjType & ";" & strObjName & ";" & strObjDate
Next doc
Set cnt = Nothing

' Add all reports to the list box.
strObjType = "Report"
Set cnt = db.Containers("Reports")
For Each doc In cnt.Documents
strObjName = doc.Name
strObjDate = Format(doc.LastUpdated, "short date")
Me.lstObjects.AddItem _
strObjType & ";" & strObjName & ";" & strObjDate
Next doc
Set cnt = Nothing

' Add all macros to the list box.
strObjType = "Macro"
Set cnt = db.Containers("Scripts")
For Each doc In cnt.Documents
strObjName = doc.Name
strObjDate = Format(doc.LastUpdated, "short date")
Me.lstObjects.AddItem _
strObjType & ";" & strObjName & ";" & strObjDate
Next doc
Set cnt = Nothing

' Add all modules to the list box.
strObjType = "Module"
Set cnt = db.Containers("Modules")
For Each doc In cnt.Documents
strObjName = doc.Name
strObjDate = Format(doc.LastUpdated, "short date")
Me.lstObjects.AddItem _
strObjType & ";" & strObjName & ";" & strObjDate
Next doc
Set cnt = Nothing

Exit_Point:
On Error Resume Next
DoCmd.Hourglass False
If Not db Is Nothing Then
db.Close
Set db = Nothing
End If
Exit Function

Err_Handler:
DoCmd.Hourglass False
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point

End Function
'------ end of code ------

You could call the function from the AfterUpdate event of the text box.
 
J

JimS

Dick, you've gone way above and beyond. Thank you so much. I learn from you
guys every day.

My objective is to identify all routines with a modified date that's more
recent than my production front end. I use sandboxes, then transfer objects
I've modified to the production front end. I use Tony's autofe as well.

Once I've identified the ones I need to transfer (I suggest to the user
which ones have changed by selecting them, then letting the user alter the
selections...) I think I can transfer them with one of the standard access
methods. What if I wanted the program to be in a third db, pointing to the
source and object db's?

Is that doable?
 
D

Dirk Goldgar

JimS said:
My objective is to identify all routines with a modified date that's more
recent than my production front end. I use sandboxes, then transfer
objects
I've modified to the production front end. I use Tony's autofe as well.

Once I've identified the ones I need to transfer (I suggest to the user
which ones have changed by selecting them, then letting the user alter the
selections...) I think I can transfer them with one of the standard access
methods. What if I wanted the program to be in a third db, pointing to the
source and object db's?

Is that doable?


Should be, but with certain complications. The TransferDatabase and
CopyObject methods only take one database argument; they assume that one of
the databases participating in the operation is the current one. You could
handle this by first importing an object from the source DB to the utility
DB, then exporting it from the utility DB to the target DB, and then
deleting the object from the utility DB. Or you could go ahead and open
either the target or the source database in a second, hidden instance of
Access via automation, and import/export the selected objects using that
instance. You would want to suppress any startup code in the database that
you opened in that way -- see http://www.mvps.org/access/api/api0068.htm .

Both of those approaches have drawbacks, but I'd be inclined to use the
utility application as a middleman, importing into it and exporting from it,
just for simplicity's sake. It's not something I've done, so I don't have
any real experience to draw on. The closest I've come is using a "patch" DB
to update a production database, but in that case all the objects to be
transfered were already in the patch DB.
 
J

JimS

Good thoughts, resources and judgements. Thank you for that, it's exactly
what I need. I'll let you know what I end up with. Perhaps I'll publish it
for others.
 

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