How to Create a New Data Entry Form for an MS Access Table Programmatically by Copying Another Form Using VBA

This fully commented procedure below describes how to create a new MS Access form by copying an existing form and then change the data source of the newly created form. This procedure eliminates the need for developers creating MS Access forms manually and speed up the development process significantly. Once the code has been fully tested, all the data entry forms for an entire database can be done in minutes, instead of days. MAARS uses this capability to writes forms automatically.

Public Sub CreateNewFormForThisTableCopyMethod(tblName As String)
 Dim frm As Form, frmNameJustCreated As String, frmNameFinal As String
 ' Purpose: Create a new MS Access Form using VBA by copying another form
 ' Change the data source fo the new created form to the given table in tblName
 
 frmNameFinal = "frm" & tblName ' This will be the name of the final form
 
 If formExistsInTheDatabase(frmNameFinal) Then ' Check if the form already exists in the database
    MsgBox frmNameFinal & " already exists in the database"
    
 Else
   'Form does not exists in the database, so create it by copying the "frmBlankForm" form
    DoCmd.CopyObject , frmNameFinal, acForm, "frmBlankForm"
   'Open the newly created form in Design mode
    DoCmd.OpenForm frmNameFinal, acDesign
    Set frm = Forms(frmNameFinal)
    'MsgBox frmNameJustCreated
    
    With frm
        .RecordSource = tblName ' Change the record source of the newly created form to tblName
        .Section(0).Height = 400 ' Form Detail
        .Section(1).Visible = True  ' Form Header
        .Section(1).Height = 400 ' Form Header
        .Section(2).Height = 540 ' Form Footer
        
    End With
    
    DoCmd.Save acForm, frmNameFinal ' Save the form
    DoCmd.Close acForm, frmNameFinal ' Close the form
    DoCmd.Restore ' Restore the form
    
    'Add Controls to the Form that was just created
    Call AddControlsToTheFormOfThisTable(tblName) ' This procedure will add all the labels, text boxes and buttons to the form
 End If
 
End Sub

This is a complimentary article written by the MAARS team for the MAARS user community. Code in this article drives the operation of MAARS (MS Access Application wRiting Software). MAARS is an intelligent automation program that speeds up MS Access Application Development by 10x, 20x or 100x times. To learn more about MAARS, click here.

Disclaimer: Some information included in this article may have been sourced from other publicly available websites and blogs. In such cases, credit goes to those authors for the original ideas and thoughts, but we do take credit for putting valuable information together and improve the efficiency of other office developers.