Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2009
    Posts
    185

    Question Unanswered: Print Form's Code

    Are there any utils to print all the forms code. Export to a txt file would be nice.
    I am setting up a data base for this but it is manual and slow to keep current.
    Also print all the forms screens would be nice all so I print screen the screens now and save to a image file.

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    There is an undocumented method of the Application object that exports (serializes) any object (form, report etc.) from a database to a text file. The code is exported as well as a definition of the objects contained in the object you export.

    For a form:

    Code:
    Application.SaveAsText acForm, "MyForm", "Path\MyForm.txt"
    There is another solution to export the code only but SaveAsText is easier. Also note the the symmetric method for importing an object serialized as a text file also exists:

    Code:
    Application.LoadFromText acForm, "MyForm", "Path\MyForm.txt"
    Have a nice day!

  3. #3
    Join Date
    Jul 2009
    Posts
    185
    It works great to see all the information on a form. But is very verbose. How do I output just code.

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    easiest...
    in my books switch to the code window
    go to the top of the window
    then select all the code
    then copy and paste it into a suitable text editor or word processor
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Here is a solution:

    Code:
    Function ExportModule(ObjectName As String, ObjectType As Long, Optional Destination As String) As Long
    '
    ' Examples: Success = ExportModule("MyForm", AcForm, "C:\Project1\MyForm_Code.bas")
    ' --------  Success = ExportModule("MyReport", AcReport, "C:\Project1\MyReport_Code.bas")
    '           Success = ExportModule("MyModule", AcModule, "C:\Project1\MyModule.bas")
    '
    ' Returns: True (success) or an error code
    ' -------
    '
        On Error GoTo Err_ExportModule
        
        Const ERR_TITLE = "Error in function ExportModule."
        
        Dim mdl As Module
        Dim str As String
    '
    ' --> Check the existence of the object
    '
        If IsNull(DLookup("Type", "MSysObjects", "Name = '" & ObjectName & "'")) Then
            MsgBox "Object: " & ObjectName & " does not exist", vbExclamation, ERR_TITLE
            Exit Function
        End If
    '
    ' --> Open the object and get a reference on its module
    '
        Select Case ObjectType
            Case acForm
                DoCmd.OpenForm ObjectName, acDesign
                Set mdl = Forms(ObjectName).Module
            Case acReport
                 DoCmd.OpenReport ObjectName, acDesign
                Set mdl = Reports(ObjectName).Module
            Case acModule
                DoCmd.OpenModule ObjectName
                Set mdl = Modules(ObjectName)
            Case Else
                MsgBox "Unknown or unhandled object type:" & ObjectType, vbExclamation, ERR_TITLE
                Exit Function
        End Select
    '
    ' --> Retrieve the whole module in a single string
    '
       str = mdl.Lines(1, mdl.CountOfLines)
    '
    ' --> Alternatively retrieve line by line
    '
    '   Dim lngLine As Long
    '
    '   For lngLine = 1 To mdl.CountOfLines
    '       str = mdl.Lines(lngLine, 1)
    '       Debug.Print str
    '   Next lngLine
    '
    ' --> Do something with the retrieved lines
    '
    '   Debug.Print str
    '
    '   Dim intHandle As Integer
    '
    '   If Len(Destination) Then
    '       intHandle = FreeFile
    '       Open Destination For Output As #intHandle
    '       Print #intHandle, str
    '       Close #intHandle
    '   End If
    '
        ExportModule = True
        DoCmd.Close ObjectType, ObjectName
        
    Exit_ExportModule:
        Set mdl = Nothing
        Exit Function
    
    Err_ExportModule:
        ExportModule = Err.Number
        MsgBox "Error code: " & Err.Number & vbNewLine & "Error description: " & Err.Description, vbInformation, ERR_TITLE
        Resume Exit_ExportModule
        
    End Function
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •