Introduction.
To make data entry tasks faster in Microsoft Access we duplicate certain field values (Ctrl+") to bring them forward to the current record from the previous record field, wherever it becomes necessary. If there is 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:
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 the next four records, as shown in the next image. Only the unique information needs to be keyed in.
A Sample Trial Run - Duplicating Field Values.
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 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.
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.
Click on the Text Box to select it and display its Property Sheet (F4).
Change the Name Property Value to FieldList.
Create a Command Button below the Textbox, change its Name Property Value to cmdDup and change the Caption Property Value to Set Carry Forward.
Display the Form’s VBA Module (Design - - > Tools - - > View Code). Copy and paste the following Code into the Form
Module and save the form:The Form Module Code.
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
Save and close the Form.
Open the VBA Editing Window (ALT+F11).
Create a new Standard Module (Insert - - > Module).
Functions in Standard Module.
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
Data Entry.
Open the FamilyDetails Form in Design View.
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.
Key in the following data field names in the text box, in the header of the Form, separated with commas.
FamilyID,Add1,Add2,City,State,Country,PIN
Set the Child Labels of TextBoxes with the actual field names (as shown in the above image) so that Users can type the required field names correctly from these labels.
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 in the Table.
Press Ctrl+S to update the current record and run the Form_AfterUpdate() Event Procedure that runs SetDefaultValue() function.
The 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 is saved in those properties in Form View, not in the 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 to a permanent duplication feature on new records, then open the form in the design view and type the text CarryForward in the Tag Property manually and save the Form. In that case, doesn't need this TextBox for the field list and Command Button on the header of the Form.
Press Ctrl++ to create a new record.
The values we have saved in the Default Value Property (through the Form_AfterUpdate() event procedure) appear in the new record. The Form_Current() event procedure runs too, and the color of the duplicated text changes to red. Now, all that's 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 step 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.
What piece of code(or macro) makes CTRL+P create a new record and CTRL+S update the current record?
ReplyDeleteCtrl++ creates new Record (Ctrl+P prints the current object), CTRL+S saves the current record. These are built-in programs of Access and they are triggered by the Key combination specified.
ReplyDeleteCheck this link to learn how to define Keyboard Shortcuts: http://www.msaccesstips.com/2007/12/keyboard-shortcuts/
Thanks A.p.r., I thought by Conditional Formatting you meant you were going to use the Conditional button on the Ribbon Design button :-)
ReplyDelete[...] a look at this: Duplicating fields with Conditional Formatting. http://www.MsAccessTips.com (Learn MS-Access Tips and Tricks) Learn Advanced MS-Access [...]
ReplyDelete