Using VBA, how can I copy an Access Form to a different database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vigilante001
    New Member
    • Feb 2008
    • 4

    Using VBA, how can I copy an Access Form to a different database

    Hello All,

    The Problem:
    I have one form, FormA, and two databases, DB1 & DB2. FormA is in DB1, and I would like to copy FormA into DB2 using VBA. Is that possible?

    -The location of the databases is in a fixed location. Let's say, C:\Database\DB1 .mdb


    The Rationale:
    I manage a database for a couple of clients, and would like to make a small database file that, using startup options, they can open and a form is called that executes a VBA script to copy an updated form to their database file. (the location is known). Posting it on a server isn't much of an option, so I was thinking of emailing it to them.

    Thank you!
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by Vigilante001
    Hello All,

    The Problem:
    I have one form, FormA, and two databases, DB1 & DB2. FormA is in DB1, and I would like to copy FormA into DB2 using VBA. Is that possible?

    Thank you!
    I dont know if you can export and import forms in databases, but,
    Why don't you export FormA, and then import it into any DB you want. ¿?

    Comment

    • Vigilante001
      New Member
      • Feb 2008
      • 4

      #3
      Originally posted by kadghar
      I dont know if you can export and import forms in databases, but,
      Why don't you export FormA, and then import it into any DB you want. ¿?
      Thanks for the reply Kadghar.

      The issue isn't whether or not you can export a form. That's a simple right-click on the form & click export. The issue is can it be done using VBA... let me know if I'm still not being clear.

      Comment

      • Vigilante001
        New Member
        • Feb 2008
        • 4

        #4
        ...Or, if it can be done by making a Visual Basic application (using VB, not VBA).

        Comment

        • Vigilante001
          New Member
          • Feb 2008
          • 4

          #5
          OK!! I figured it out. Place this code in the code builder for the form, changing the specifics to match your need:

          Code:
          Option Compare Database
          Private Sub Form_Load()
          
              '***********************
              'SUMMARY: Copies Form to another database, then closes. Based on
              'the condition that it has not updated before.
              '***********************
              
              Dim SQL As String
              Dim rs As Recordset
              Set rs = Me.Recordset
              
              If rs.BOF = True And rs.EOF = True Then
                  'If the Updated table is empty, this update hasn't been run before.
                  'Copy the updated form over!
                  DoCmd.CopyObject "C:\Documents and Settings\mschnupp\Desktop\[I]FileNameHere.mdb[/I]", _
                               "[I]NameTheCopy[/I]", acForm, "[I]FormToCopy[/I]"
                  
                  'Record the date updated & deactivate the "Update" function.
                  SQL = "INSERT INTO [UpdateCheck] ([Updated])" & _
                        "VALUES (Date())"
               
                  DoCmd.SetWarnings (WarningsOff) 'Turn off/on the popup alert
                  DoCmd.RunSQL SQL 'Record as updated
                  DoCmd.SetWarnings (WarningsOn)
              
              Else
                  'Message displayed if a recorded update is present in the table.
                  MsgBox ("You have already performed this update.")
              End If
              
              Set rs = Nothing
              DoCmd.Close acForm, "Form1"
              
          End Sub

          Comment

          Working...