To make data entry task faster in Microsoft Access we duplicate certain field values (Ctrl+”) to bring them forward to the current record from previous record field, wherever it become necessary.  If there are more than one field and needs to duplicate the same into more than one record then repeating Ctrl+” everywhere is asking for more manual action, even though it is quicker than typing all the information literally.

For example, let us assume that we are creating a Mailing List of all Family members of all the residents in our locality and the sample data entry Form looks like the image given below:

Before Update screen image

Click to Enlarge the image

If there are five members in a family then the data fields Family Code and Address line field values must be repeated for  each member of the family in next four records, as shown in the next image.  Only the unique information needs to be keyed in.

Before Update screen image

Click to Enlarge the image

Let us try this out.

  1. create a new Table with the following structure and save it with the name FamilyDetails:
    Table: FamilyDetails
    Srl Field Name Data Type Field Size
    1 ID AutoNumber  
    2 FamilyID Integer  
    3 Title Text 15
    4 FName Text 25
    5 LName Text 25
    6 Add1 Text 50
    7 Add2 Text 50
    8 City Text 50
    9 State Text 50
    10 Country Text 50
    11 PIN Text 10
  2. Use the Form Wizard and create a Form in Column format, as shown in the image above, using the FamilyDetails Table and save the Form with the name frmFamilyDetails.
  3. Create a Textbox (wide enough to type a list of field names separated with commas) on the header of the Form as shown in the image above.
  4. Click on the Textbox to select it and display it’s Property Sheet (F4).
  5. Change the Name Property Value to FieldList.
  6. Create a Command Button below the Textbox, change it’s Name Property Value to cmdDup and change the Caption Property Value to Set Carry Forward.
  7. Display the Form’s VBA Module (Design – – > Tools – – > View Code). Copy and paste the following Code into the Form Module and save the form:

    Private Sub cmdDup_Click()
    'Set 'CarryForward' Text in the submitted field's TAG property
    SetTagValue Me, "FieldList"
    
    End Sub
    
    Private Sub Form_AfterUpdate()
    'Set Default Property Value
    SetDefaultValue Me
    
    End Sub
    
    Private Sub Form_Current()
    'Change color of Duplicated field to Red.
    SetColorChange Me
    
    End Sub

  8. Save and close the Form.
  9. Open the VBA Editing Window (ALT+F11).
  10. Create a new Standard Module (Insert – – > Module).
  11. Copy and paste the following VBA Code (consisting of three Functions) into the new Module and save it:
    Public Function SetTagValue(ByVal frm As Form, ByVal fldList As String)
    '---------------------------------------------------------------------
    'Author: a.p.r. pillai
    'Date  : Feb 2012
    'Rights: All Rights Rserved by www.msaccesstips.com
    '---------------------------------------------------------------------
    Dim txt, splt, ctl As Control
    Dim ctlName As String, ctlType As Integer, j As Integer
    Dim resetFlag As Boolean, ctrl_name As String
    
    resetFlag = False
    txt = Nz(frm.Controls(fldList).Value, "")
    ctrl_name = frm.Controls(fldList).Name
    If Len(txt) = 0 Then
       resetFlag = True
    Else
       'split the field list sepparate with commas
       'and load them into the Array: splt()
       splt = Split(txt, ",")
    End If
    '   Set frm = Me
       'initialize all Textboxes and Combobox Tag & Default Value
       'Properties, except the FieldList textbox.
       'and change all field's Forecolor to black. 
       For Each ctl In frm.Controls
          ctlType = ctl.ControlType
          ctlName = ctl.Name
          If ctlName = ctrl_name Then GoTo nextitem
          
          If ctlType = 109 Or ctlType = 111 Then
             ctl.Tag = ""
             ctl.DefaultValue = ""
             ctl.ForeColor = vbBlack
          End If
    nextitem:
       Next
    
       frm.Repaint 'show the change color on the form
       If resetFlag Then 'if the fieldlist textbox was empty then exit
         Exit Function
       End If
    
    'Compare each field's name on the form with the field names 
    'selected and stored in the 'FieldList' Array.
       For Each ctl In frm.Controls
          ctlName = ctl.Name
          ctlType = ctl.ControlType
          'control-type 109 is Textbox and 111 is combobox
          'only these controls are duplicated
          If ctlType = 109 Or ctlType = 111 Then
             For j = 0 To UBound(splt)
                'if a match found then change it's Tag Property Value to 'CarryForward'.
                If Trim(splt(j)) = ctlName Then
                   ctl.Tag = "CarryForward"
                End If
             Next
    
          End If
       Next
    
    End Function
    
    
    Public Function SetDefaultValue(ByVal frm As Form)
    '---------------------------------------------------------------------
    'Author: a.p.r. pillai
    'Date  : Feb 2012
    'Rights: All Rights Rserved by www.msaccesstips.com
    'Run this Procedure when the Form_Update() Event Procedure fires.
    '---------------------------------------------------------------------
    Dim ctl As Control
    For Each ctl In frm.Controls
    If ctl.ControlType = 109 Or ctl.ControlType = 111 Then
          'if a control found with it's Tag Property set with the text 'CarryForward'
          'copy the current field value into the Default Property.
          'When the new record is created the Default Value will automatically
          'appear in the new record.
          If ctl.Tag = "CarryForward" Then
              ctl.DefaultValue = Chr$(34) & ctl.Value & Chr$(34)
    
          End If
               'change the color of the data to black
               ctl.ForeColor = vbBlack
    End If
    
    Next ctl
    frm.Repaint
    
    End Function
    
    
    
    Public Function SetColorChange(ByVal frm As Form)
    '---------------------------------------------------------------------
    'Author: a.p.r. pillai
    'Date  : Feb 2012
    'Rights: All Rights Rserved by www.msaccesstips.com
    'Note  : This Function is run from the Form_Current() Event Procedure.
    '---------------------------------------------------------------------
    
    Dim ctl As Control
    If frm.NewRecord Then
         For Each ctl In frm.Controls
             If ctl.ControlType = 109 Or ctl.ControlType = 111 Then
                'Format the copied values with Red Color.
                If ctl.Tag = "CarryForward" Then
                   ctl.ForeColor = vbRed
                End If
             End If
         Next
    frm.Repaint
    End If
    
    End Function

  12. Open the FamilyDetails Form in Design View.
  13. Key in your own personal information (or the sample data from the first image above), but don’t move to the next new record now.
  14. Key in the following data field names into the text box ,in the header of the Form, separated with commas.

    FamilyID,Add1,Add2,City,State,Country,PIN

    Set the Child Labels of Text Boxes with the actual fields names (as shown in the above image) so that Users can type the required field names correctly from these labels.

  15. Click on the Command Button.

    Note: The Command Button Click Event Procedure calls the SetTagValue() Function, with the Form object and the Textbox name: FieldList as parameters, that reads the Field List and records the text ‘CarryForward’ in each field’s Tag Property. Remember, we have not yet updated the current record into the Table.

  16. Press Ctrl+S to update the current record and run the Form_AfterUpdate() Event Procedure that runs SetDefaultValue() function.

    Current record field (having their Tag Property set with the text ‘CarryForward’) values are copied into the Default Value Property of that field. The default values will appear in those fields in a new record automatically.

    Note: The Tag Property Value & Default Value are saving in those properties in Form View, not in design view. When you close the Form these settings are lost from these Properties. If you open the Form again you must specify the list of fields in the text box and click the Command Button to set the ‘CarryForward’ text in the Tag Property again.

    But, if you want certain fields to be assigned with permanent duplication feature on new records then open the form in design view and type the text CarryForward in the Tag Property manually and save the Form. In that case you don’t need the Text Box for the field list and Command Button on the header of the Form.

  17. Press Ctrl++ to create a new record.

    The values we have saved in the Default Value Property (through the Form_AfterUpdate() event procedure) appears in the new record. The Form_Current() event procedure runs too and the color of the duplicated text changes to red. Now, all that left to do is to key in the rest of the information.

    The Ctrl+S first to update the current record and then the Ctrl+Plus next to create a new record steps we have taken only to explain what happens at each stage.

    After keying in the data on a record the User can directly press Ctrl+Plus (click on the New (Blank) Record control on the navigation control at the bottom of the form) to create a new record. This action will also fire the Form_AfterUpdate() event first and then the Form_Current() event next.

Technorati Tags: