473,503 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Export a Form or Report as a "Package"

topher23
234 Recognized Expert New Member
It happens often in Access development environments: you're working in one database, and you realize that you want to use a form or report in another database that you or your company are also developing. But when you import/export the object, you discover that a lot of the objects that it depends on (tables, queries, subforms/subreports) aren't in the other database and also need import/export. Eventually, you get everything transferred, but wonder if it was really worth all the hassle.

The answer is, "No, it's not." Not when you could have done it all with VBA, that is! By accessing the object's Dependencies collection, we can find and export every object that your form or report requires to run.

The dependencies are saved in the databases system tables, not in the object definitions themselves, so they must be populated to their own object using [object].GetDependencies. From there, it's just a matter of iterating through all of the dependencies.

The following three code procedures accomplish exporting each dependency object to a purpose-created database. You can then send the package to another developer or copy it wholesale into another working database. Paste the code into a new module to use it.

The first procedure creates the export database and passes the top-level object to the packaging procedure. The DepColl array and DepI iterator are used to hold references to all of the converted objects in order to keep from converting the same objects due to multiple dependencies. In this procedure, we also make sure that "Track Name Autocorrect Info" is set to True (1), because this creates the system table that stores all object dependencies.

Expand|Select|Wrap|Line Numbers
  1. 'PackageObjects Module
  2. 'Example: PackageObject([Object Name],"Form" or "Report")
  3. '2014 Topher Ritchie
  4. 'This code is free for use in development and must
  5. 'retain this section.
  6. '=========================================================
  7. 'Module Declarations section
  8. '=========================================================
  9.  
  10. Option Compare Database
  11. Option Explicit
  12.  
  13. Public DepColl() As String
  14. Public DepI As Long
  15.  
  16.  
  17. Public Sub PackageObject(strObject As String, strType As String)
  18. Dim db As Object
  19.  
  20.     Application.SetOption "Track Name AutoCorrect Info", 1
  21.     Access.DBEngine.CreateDatabase "F:\MyDatabases\dbExport_" & Format(Date, "yyyy-mm-dd") & ".accdb", DB_LANG_GENERAL
  22.  
  23.     Set db = Application.CurrentProject
  24.     If strType = "Form" Then
  25.         OutputItem strObject, -32768
  26.         PackageDependencies db.AllForms.Item(strObject)
  27.     ElseIf strType = "Report" Then
  28.         OutputItem strObject, -32764
  29.         PackageDependencies db.AllReports.Item(strObject)
  30.     End If
  31.     ReDim DepColl(0)
  32.     DepI = 0
  33. End Sub
  34.  
This next procedure is the "meat" of the module. This populates an object with the dependencies collection, then goes through each object dependency to ensure it hasn't already been exported. If it hasn't, the code exports it, then calls itself recursively using the exported object in order to get all n-level dependencies for the primary object. Unfortunately, this is where the code runs into a bit of trouble. It may run into an infinite loop in which it continues to call itself, but no new objects are being output. Fortunately, if you are watching the Immediate window, you will see when this begins to occur because no new objects will show up there. It's not the best solution, but since this code is intended for development purposes only and not for use in production systems, simply break the code at this point and stop it.

Expand|Select|Wrap|Line Numbers
  1. Public Sub PackageDependencies(objObject As Object)
  2.     Dim DepObj As Object
  3.     Dim DepObjColl As Object
  4.     Dim i As Long
  5. On Error GoTo Err_Exit:
  6.  
  7.     Set DepObjColl = objObject.GetDependencyInfo
  8.     For Each DepObj In DepObjColl.Dependencies
  9.         For i = 1 To DepI
  10.             If DepObj.name = DepColl(i) Then GoTo SkipObj
  11.         Next i
  12.         DepI = DepI + 1
  13.         ReDim Preserve DepColl(DepI)
  14.         DepColl(DepI) = DepObj.name
  15.         OutputItem DepObj.name, DLookup("Type", "MSysObjects", "NAME = '" & DepObj.name & "'")
  16.         PackageDependencies DepObj
  17. SkipObj:
  18.     Next DepObj
  19.  
  20. Err_Exit:
  21. End Sub
  22.  
The final code procedure does the actual export to the holding database. Similar to the original procedure call, it gets passed an object name and type, but in this case the type, rather than being plaintext, is an integer value pulled from a system table using the DLookup statement above. This final procedure simply translates the Type into an acObjectType constant declaration for the sake of clarity.

Expand|Select|Wrap|Line Numbers
  1. Public Sub OutputItem(strName As String, lngType As Long)
  2.     Debug.Print strName, lngType
  3.     If lngType = 1 Or lngType = 6 Then
  4.         DoCmd.TransferDatabase acExport, "Microsoft Access", "F:\PRISM\PRISM Support Programs\Development\dbExport_" & Format(Date, "yyyy-mm-dd") & ".accdb", acTable, strName, strName
  5.     ElseIf lngType = 5 Then
  6.         DoCmd.TransferDatabase acExport, "Microsoft Access", "F:\PRISM\PRISM Support Programs\Development\dbExport_" & Format(Date, "yyyy-mm-dd") & ".accdb", acQuery, strName, strName
  7.     ElseIf lngType = -32768 Then
  8.         DoCmd.TransferDatabase acExport, "Microsoft Access", "F:\PRISM\PRISM Support Programs\Development\dbExport_" & Format(Date, "yyyy-mm-dd") & ".accdb", acForm, strName, strName
  9.     ElseIf lngType = -32764 Then
  10.         DoCmd.TransferDatabase acExport, "Microsoft Access", "F:\MyDatabases\dbExport_" & Format(Date, "yyyy-mm-dd") & ".accdb", acReport, strName, strName
  11.     End If
  12. End Sub
  13.  
Using this code, you will be able to output a complete Form or Report with all of its dependencies intact, ready to be used. By adding a few lines of code to the first procedure, you could also use it to transfer queries, if you'd like, but that was outside of my original scope.
Feb 4 '14 #1
1 8733
jimatqsi
1,271 Recognized Expert Top Contributor
topher23,
Thanks very much, this is really helpful.

I note with dismay the need to make sure that "Track Name Autocorrect Info" is set to True (1), because I have suffered greatly at the hands of the beast on occasion. It tends to cause long, long hourglass pauses in a database with lots of objects.But now I avoid that to the extent possible.

Thanks again for a very insightful article.

Jim
Apr 19 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2665
by: RAF | last post by:
hi i want to export crystal report to html format i am using following code Dim rpt As New test Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
3
3636
by: Colin Graham | last post by:
Error when attempting to export Crystal Report as PDF document asp.net. I get the following error. Error in File...
7
14339
by: Pat | last post by:
I would like to send the Print Preview of a MS Access form to a Snapshot file. The form contains an OLE graph. BACKGROUND A snapshot of a report is possible. If I could I would use a report to...
2
4887
by: Yisroel Markov | last post by:
Greetings, two of my users have the same front end db (A2K). Each of them has created, in addition to the standard reports, a personal customized report. Now they both want to be able to run...
5
25022
by: Badis | last post by:
Hi, Could any one give a sample example on how to export crystal report called "MyReport.rpt" to PDF using Crystal Report !!? Thanks
6
6235
by: smcdonald | last post by:
I have a report that opens up using a pretty complex query. I then pop up a form with combo boxes so the user can apply a filter to the existing report and then refresh the report. I need to export...
6
7060
by: IsdWeb | last post by:
Is it possible to export an Access report from one DB and import it into another?
3
4047
by: Rama Jayapal | last post by:
i have to generate bills for the specified candidate so in order to generate a single person report i wrote the following code Datatable dt=new Datatable; DataAdapter da=new...
3
7471
by: Carl Hansen | last post by:
Hi NG I try to export a report, by using the following code - it fails with errorcode: 2282 DoCmd.OutputTo acOutputReport, "Faktura", acFormatSNP, stinavn & "\" & strName & ".snp", True if...
0
2312
by: Sohail1980 | last post by:
Hi All, I have a crystal report that loads default values from the database, using SQL server 2005. On my web page, I have 2 databound dropdownlist and upon selectedindexchanged the report must...
0
7084
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...
0
7278
Oralloy
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,...
0
7328
jinu1996
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...
1
6991
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...
0
7458
tracyyun
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...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.