Hi Marcin,
To make the example a little more meaningful, I'll use the
following names for your textboxes and command button:
txtField1 - Textbox field on fWeightInput
txtField2 - Textbox field on fWeight
cmdCopyValue - Button on fWeight to copy the value from fWeightInput
Here is some code that you can use that would be indicated under the
OnClick event property for the button:
Private Sub cmdCopyValue_Click()
If IsObjectOpen("fWeightInput", acForm) = True Then
Me.txtField2 = Forms("fWeightInput")("txtField1")
End If
End Sub
First of all, the "Me" qualifier refers to the current form or
report that you're in - it saves you from having to code:
Forms("fWeight")("txtField2") = Forms("fWeightInput")("txtField1")
If you needed to run this process from a third form, copying
the value from fWeightInput to fWeight, the code directly above is
what you would need to run in order to accomplish it. Also, you
should run the "IsObjectOpen" check on both forms.
The call to the function "IsObjectOpen()" is used to ensure that
the form that you're trying to retrieve the value from is open. In
the event that (in this case) form fWeight was not open when you
tried to click the button, you would get a runtime error. It may
seem a little redundant if that form should always be open, but
it's good to use in a case when attempting to retrieve a value from
another form like this. Here's the code for IsObjectOpen which you
can Copy|Paste directly as a function in your app:
Public Function IsObjectOpen(ByVal strObjectName As String, Optional lngObjectType As Long = acForm) As Boolean
If fG_DebugMode = False Then
On Error GoTo Err_IsObjectOpen
End If
If SysCmd(acSysCmdGetObjectState, lngObjectType, strObjectName) = acObjStateOpen Then
If lngObjectType = acForm Then
If Forms(strObjectName).CurrentView = 1 Then
IsObjectOpen = True
Else
IsObjectOpen = False
End If
Else
IsObjectOpen = False
End If
Else
IsObjectOpen = False
End If
End Function
The function defaults to type acForm (for forms), but you can use it
for other object types as well. For example "acReport" is used for
reports.
Hope that helps.
Thanks,
Warren
|