Copying an existing form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • velvadp
    New Member
    • Oct 2011
    • 1

    Copying an existing form

    Hi,

    I'm trying to copy an existing form in the same database with a different name. For example, I'm copying the form "Aggregate" to a new name "Aggregate2 "

    I'm trying to do create the name of the new form dynamically, but checking to see if the form exists and then incrementing the number. I have done this and it works, but I get a type mismatch error when I try to copy the form.

    My code looks like this:

    Code:
    Dim FormNum As Integer
    Dim FormStr As String
    Dim obj As AccessObject, dbs As Object
    
    Set dbs = Application.CurrentProject
    FormNum = 2
    FormStr = "Aggregate" & FormNum
    
    For Each obj In dbs.AllForms
        If obj.Name = "" & FormStr & "" Then
            FormNum = FormNum + 1
            FormStr = "Aggregate" & FormNum
        End If
    Next obj
    
    DoCmd.CopyObject FormStr, acForm, "Aggregate"
    Last edited by NeoPa; Oct 14 '11, 12:07 AM. Reason: Added mandatory [CODE] tags for you
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32661

    #2
    You're passing the wrong parameter values to the CopyObject command on line #16. See Help for the correct values but the parameters are :
    Code:
    [B]DoCmd.CopyObject([I]DestinationDatabase[/I], [I]NewName[/I], [I]SourceObjectType[/I], [I]SourceObjectName[/I])[/B]

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      @NeoPa:
      All Arguments to CopyObject() are Optional, the OP simply forgot the Placeholder for the 1st Argument (Destination Database).

      @velvadp:
      You should definately check and see if a Copy already exists, before attempting to Copy the Form. Notice also the Comma directly after CopyObject and the change in Syntax:
      Code:
      On Error Resume Next
      Dim FormNum As Integer
      Dim FormStr As String
      Dim obj As AccessObject
      Dim dbs As Object
        
      Set dbs = Application.CurrentProject
      
      FormStr = "Aggregate"
      
      'Delete the Copied Version if it exists
      DoCmd.DeleteObject acForm, FormStr & "2"
        
      For Each obj In dbs.AllForms
        If obj.Name = FormStr Then
          DoCmd.CopyObject , FormStr & "2", acForm, FormStr
            Exit For
        End If
      Next obj

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        Indeed they are ADezii, and that's an accurate reflection of the problem.

        However, if I'd responded in that way it could be inferred I was advising the OP to add an extra parenthesis, whereas I was rather hoping he might take the more appropriate option of using Named Parameters. With Named Parameters for those procedure calls that aren't so obvious (such that one could get the positions wrong) you make it clear to any developer/reviewer what is going on and that makes it easier all around. It's also harder to get it wrong in the first place.

        Comment

        Working...