Solved Exporting Forms?

Gasman

Enthusiastic Amateur
Local time
Today, 08:06
Joined
Sep 21, 2011
Messages
16,538
Hi all,
In this thread, I am trying to help the O/P

He uploaded an exported form cls file.
Then I tried to import it into my DB abd kept getting errors that it might beging with a Form_, and two other options.
Well yes, it did, as that is how Access exported it?

I tried one of my own forms and same problem there.

So found out that I needed to remove the Form_ prefix? :(

However why does Access export it as that and then refuse to import it as that?
 
The post is about tables but your question is about forms. I'm confused.

How did the OP export the class module? When I export objects, I prefix them with their object type so having a form name start with Form_ is natural in my world.
 
Last edited:
This text file?
Code:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Form_500"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub D001_Click()
    Debug.Print "UKVB:" & Me.UVKB & " Lookup:" & DLookup("ID", "Registrovani")
    If Me.UVKB.Value = DLookup("ID", "Registrovani") Then
        DoCmd.Close acForm, 500
        DoCmd.OpenForm (0)
    End If
End Sub
This is an export from the VBA editor of the form class.
This cannot be imported because it is not a complete form.
 
Clearly my question was unclear. HOW WAS THE CLASS MODULE EXPORTED? Post the code.
 
The post is about tables but your question is about forms. I'm confused.

How did the OP export the class module? When I export objects, I prefix them with their object type so having a form name start with Form_ is natural in my world.
Yes Pat, but the OP uploaded and exported form class, and now Josef points out it cannot be imported like that? That is where I and he was going wrong.
If you export a form class from the vba window it names it by default the name in the project in this case Form_500. Even renaming the file to 500 did not work. You have to remove the Form_ prefix inside that file to import it, but it is then not recognised as a form?
 
Clearly my question was unclear. HOW WAS THE CLASS MODULE EXPORTED? Post the code.
I do not know Pat, likely from the vba window as josef points out, as I could replicate with a form of mine.
Exported manually as I did it with my form.
 
It is not possible to export an Access form completely from the VBA editor (Menu - Export). Only the code module is exported via the VBA Editor.
(VBA code: vbe.ActiveVBProject.VBComponents(...).Export)
I recognize the export via the VBA editor because of the first 5 lines. This would not be there with SaveAsText.

To export a complete Access form as text, one must use Application.SaveAsText.
 
Last edited:

As Josef mentioned that only exported the class module of the form not the form.
 
I am not even sure of any utility to exporting the class module of a form or report. Not sure what you could do with it afterwards. Not sure if you could edit it and then synch up with a form. Maybe you could export then set the HasModule property to false then import with the matching name. Not sure what purpose that could ever serve.
However when you export as text it stores all the properties of the form or report to include the code too. Here is an example of the export at the bottom below all the form properties

Code:
**** Bottom of the export file with the code from the class. Form properties are above
CodeBehindForm
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Compare Database
Option Explicit

Public ComponentImage As String

Public Function SetImage(ctrlName As String)
  ComponentImage = Me.Controls(ctrlName).Picture
  Me.Visible = False
End Function

Private Sub cmdCancel_Click()
  DoCmd.Close acForm, Me.Name
End Sub

Private Sub cmdDelete_Click()
  ComponentImage = "Delete"
  Me.Visible = False
End Sub

Private Sub Form_Load()
 Dim ctrl As Access.Control
 
 For Each ctrl In Me.Controls
   If ctrl.Tag = "defaultimage" Then
     ctrl.OnClick = "=SetImage('" & ctrl.Name & "')"
   End If
 Next ctrl
End Sub
 
I use SaveAsText as a backup strategy. It works fabulously and has saved my bacon on multiple occasions. When "I" am the person logged in, the app checks the last update date in MSysObjects and if anything was updated since the last backup, it backs up all the objects except tables using SaveAsText.

Clearly, the export from the dropdown menu in the VBA window exports the "class module" rather than the form.
 
Ok, all thank you for your replies.
I obviously did the same as the OP, which as eveyone is saying, NOT the way to do it. :)

Thanks again for all the replies.
 
NOT the way to do it.
For standard modules and custom class modules exporting from the vba editor is the proper way to export the module.
So I can export a Find As You Type combobox class and you can import no problem. If you want to demo you have to change the extension from TXT to CLS. I had to change the extension to upload to this site since CLS is not an approved extension.
 

Attachments

Sorry, I should have stated'NOT the way to do it for Forms'
 
Here's some code:
Code:
        Case 1      'Export All Objects
            Call ExportDatabaseObjects("Forms", strPathName, strDatabase)
            Call ExportDatabaseObjects("Reports", strPathName, strDatabase)
            Call ExportDatabaseObjects("Queries", strPathName, strDatabase)
            Call ExportDatabaseObjects("Macros", strPathName, strDatabase)
            Call ExportDatabaseObjects("Modules", strPathName, strDatabase)
            Call ExportDatabaseObjects("Tables", strPathName, strDatabase)

Code:
Public Sub ExportDatabaseObjects(ExportType As String, sExportLocation As Variant, strDatabase As Variant)

On Error GoTo Err_ExportDatabaseObjects   

    Dim db As DAO.Database
    Dim td  As DAO.TableDef
    Dim d   As Document
    Dim c   As Container
    Dim i    As Integer 

     If strDatabase & "" = "" Then
        Set db = CurrentDb()
    Else
        Set db = DBEngine.Workspaces(0).OpenDatabase(strDatabase)
    End If

    Select Case ExportType
        Case "Tables"
            iCountTables = 0
            For Each td In db.TableDefs 'Tables
                If Left(td.Name, 4) <> "MSys" Then
                    DoCmd.TransferText acExportDelim, , td.Name, sExportLocation & "Table_" & td.Name & ".txt", True
                    iCountTables = iCountTables + 1
                End If
            Next td
        Case "Forms"
            Set c = db.Containers("Forms")
            iCountForms = 0
            For Each d In c.Documents
                Application.SaveAsText acForm, d.Name, sExportLocation & "Form_" & d.Name & ".txt"
                iCountForms = iCountForms + 1
            Next d
        Case "Reports"
            Set c = db.Containers("Reports")
            iCountReports = 0
            For Each d In c.Documents
                Application.SaveAsText acReport, d.Name, sExportLocation & "Report_" & d.Name & ".txt"
                iCountReports = iCountReports + 1
            Next d
        Case "Macros"
            Set c = db.Containers("Scripts")
            iCountScripts = 0
            For Each d In c.Documents
                Application.SaveAsText acMacro, d.Name, sExportLocation & "Macro_" & d.Name & ".txt"
                iCountScripts = iCountScripts + 1
            Next d
        Case "Modules"
            Set c = db.Containers("Modules")
            iCountModules = 0
            For Each d In c.Documents
                Application.SaveAsText acModule, d.Name, sExportLocation & "Module_" & d.Name & ".txt"
                iCountModules = iCountModules + 1
            Next d
        Case "Queries"
            iCountQueries = 0
            For i = 0 To db.QueryDefs.Count - 1
                Application.SaveAsText acQuery, db.QueryDefs(i).Name, sExportLocation & "Query_" & db.QueryDefs(i).Name & ".txt"
                iCountQueries = iCountQueries + 1
            Next i
        Case Else
    End Select

    Set db = Nothing
    Set c = Nothing   

    MsgBox "Selected objects have been exported as a text file to " & sExportLocation, vbInformation

Exit_ExportDatabaseObjects:
    Exit Sub   

Err_ExportDatabaseObjects:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_ExportDatabaseObjects 

End Sub
 
Last edited:
I fixed the code tags. I thought i put the code in tags but didn't pay attention when it loaded.
 

Users who are viewing this thread

Back
Top Bottom