This is completely untested aircode, just a concept to illustrate what the
code would look like. Problems with memo fields (as noted previously) will
probably still occur.
To keep Form1 and Form2 synchronized so they both stay at the same record,
paste the 2 functions below into a standard module (Modules tab of Database
window), and save. The illustration assumes a primary key named ClientID. In
the Current event procedure of Form1, add this line:
Call SyncForm(Me, "Form2", "ClientID", Me.ClientID)
and in the Current event procedure of Form2:
Call SyncForm(Me, "Form1", "ClientID", Me.ClientID)
If you prefer to use the On Current property of the form, it would be:
=SyncForm([Form], "Form2", "ClientID", [ClientID])
-----------------code begins-----------------------
Function SyncForm(frmSource As Form, strTargetFormName As String, _
strKeyField As String, varKeyValue As Variant) As Boolean
On Error GoTo Err_Handler
'Purpose: Keep two open forms sychronized to the same record.
'Limitations: Requires a single-field primary key. A2000 and later.
'Arguments: frmSource = a reference to form you are sync-ing from.
' strTargetFormName = name of form that should be sync'd.
' strKeyField = name of the primary key field.
' varKeyValue = the value of the primary key field.
'Return: True if sync happened. False for all other conditions _
(form not open, already there, filtered differetly)
'Usage: In Form_Current: _
Call SyncForm(Me, "Form2", "[ID]", Me.[ID])
Dim frmTarget As Form 'The other form.
Dim rsTargetClone As DAO.Recordset 'Clone set of other form.
Dim strWhere As String 'WhereCondition to match records.
Dim strDelim As String 'Delimiter for Where string.
Dim bSynced As Boolean 'True if forms synchronized.
'Do nothing if the target form is not loaded.
If CurrentProject.AllForms(strTargetFormName).IsLoaded Then
'Get a reference to the target form.
Set frmTarget = Forms(strTargetFormName)
If (frmTarget.NewRecord And frmSource.NewRecord) Or _
(frmTarget(strKeyField) = varKeyValue) Then
'Avoid endless triggering if already synchronized.
Else
'Save the record so the other form can move.
If frmTarget.Dirty Then
frmTarget.Dirty = False
End If
If frmSource.NewRecord Then
'Move the target form to a new record.
DoCmd.Echo False
frmTarget.SetFocus
RunCommand acCmdRecordsGoToNew
frmSource.SetFocus
DoCmd.Echo True
bSynced = True
Else
'Find the same record in the target form.
If Not IsNull(varKeyValue) Then
strDelim = GetDelim(varKeyValue)
strWhere = strKeyField & " = " & _
strDelim & varKeyValue & strDelim
Set rsTargetClone = frmTarget.RecordsetClone
rsTargetClone.FindFirst strWhere
If Not rsTargetClone.NoMatch Then
frmTarget.Bookmark = rsTargetClone.Bookmark
bSynced = True
End If
End If
End If
End If
End If
'Set return value
SyncForm = bSynced
Exit_Handler:
DoCmd.Echo True
Set rsTargetClone = Nothing
Set frmTarget = Nothing
Exit Function
Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_Handler
End Function
Private Function GetDelim(varIn As Variant) As String
Select Case VarType(varIn)
Case vbString
GetDelim = """"
Case vbDate
GetDelim = "#"
End Select
End Function
-----------------code ends-----------------------