
' --------------------------------------------------------
' Hands-On 32-1
' No Code.
' Please follow the instructions in the book.
' --------------------------------------------------------



' --------------------------------------------------------
' Hands-On 32-2
' --------------------------------------------------------

Sub Copy_OnClickMacro()
    Dim ctl As Control
   
    ' open in the Design View the Supplier List form
    DoCmd.OpenForm "Supplier List", acDesign

    ' only run the code if the specified control
    ' exists on the form
    For Each ctl In Forms("Supplier List").Controls
        If TypeOf ctl Is CommandButton Then
            If StrComp(ctl.Name, "cmdClose", vbTextCompare) = 0 Then
                
                ' open in the Design View the Shippers Details form
                ' this form contains an embedded macro in the OnClick
                ' event of cmdClose button
            
                DoCmd.OpenForm "Shipper Details", acDesign
        
                ' copy macro from the OnClick event property of the
                ' cmdClose button on the Shipper Details form
                ' to the OnClick event property of the cmdClose button
                ' on the Supplier List form
            
                Forms("Supplier List").Controls("cmdClose").OnClickMacro = _
                    Forms("Shipper Details").Controls("cmdClose").OnClickMacro
                DoCmd.Save acForm, "Supplier List"
                DoCmd.Close acForm, "Shipper Details"
                MsgBox "The embedded macro was successfully copied."
                Exit Sub
            End If
        End If
    Next
  
        MsgBox "Operation could not be performed. " & vbCrLf & _
                "Ensure that the specified control exists."
        
End Sub


' --------------------------------------------------------
' code on page 904
' --------------------------------------------------------

Sub SaveEmToStandalone()
    Dim strMacro As String
    Dim objFileSys As Object
    Dim objFile As Object
    Dim strFileName As String
    
    ' open in the Design view the form that contains
    ' the embedded macro
    DoCmd.OpenForm "Login Dialog", acDesign
    
    ' to write an embedded macro to a file use the Value property
    strMacro = Forms("Login Dialog"). _
    Controls("cboCurrentEmployee"). _
    Properties("AfterUpdateEmMacro").Value
    
    ' close the form
    DoCmd.Close acForm, "Login Dialog"
    
    ' Create a text file
    strFileName = "C:\Acc07_ByExample\AfterUpdate for combo.txt"
    Set objFileSys = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFileSys.CreateTextFile(strFileName, True)
    
    ' Write strMacro to the text file
    objFile.Write strMacro
    
    ' Close the file
    objFile.Close
    
    ' Use the undocumented LoadFromText method
    ' of the Application object to create a standalone macro
    ' from the text file
    Application.LoadFromText acMacro, "Cbo Employee After Update", _
    strFileName
End Sub

