Hello Steve,
Here is a working example: I'll use my form names to save time:
The purpose is to popup a calculator which will populate different fields on
the main form. The Calulator, has three fields bound to fields in the
Claims table, one is for unit, another for amount per unit, and the third
the total amount. Brackets [] around object names are for identification.
Forms:
[Claims] Main Form name:
[ClaimsDeductionCalcPopup] Form Name:
Textboxes
[Deduction] Textbox on [Claims] Form to receive value
[ClaimeeDeduction] Textbox on [Claims] form to receive value
Buttons:
[Calc1] Button to open [ClaimsDeductionCalcPopup] to populate
[Deduction]
[Calc2] Button to open [ClaimsDeductionCalcPopup] to populate
[ClaimeeDeduction]
Code for [Calc1] to open popup, [ClaimsDeductionCalcPopup]
Private Sub Calc1_Click()
DoCmd.OpenForm "ClaimsDeductionCalc", , , , , acDialog, 1
Call Deduction_AfterUpdate
End Sub
Note that I opened the form in Dialog mode. This is for process control, so
that they user doesn't change the record on the main form or open the popup
in normal mode and leave it open. The number 1 is the Openarg used to
identify that the [Deduction] textbox will be populated.
The second line, Call Deduction_AfterUpdate will run the AfterUpdate
event of the textbox to run calculation for populating other amounts on the
form. If you need to do this, then that is how it is accomplished.
The Code for [Calc2] to open popup, [ClaimsDeductionCalcPopup] is similar
Private Sub Calc2_Click()
DoCmd.OpenForm "ClaimsDeductionCalc", , , , , acDialog, 2
Call ClaimeeDeduction_AfterUpdate
End Sub
The Code for the popup form goes in the Load event of the form as well as a
button.
Button:
[OK] Runs update of textboxes on form [Claims]and closes form
[ClaimsDeductionCalcPopup]
Procedures:
For the OK Button:
Private Sub OK_Click()
If IsLoaded("Claims") Then
Dim frm As Form
Set frm = Forms![claims]
Select Case OpenArgs
Case 1 'Assign values related to the [Deduction] field
frm![DedUnits] = [DedUnits]
frm![DedUnitCost] = [DedUnitCost]
frm![Deduction] = [Deduction]
frm![DeductionReason] = [DeductionReason]
Case 2 'Assign values related to the [ClaimeeDeduction] field
frm![ClaimeeDedUnits] = [DedUnits]
frm![ClaimeeDedUnitCost] = [DedUnitCost]
frm![ClaimeeDeduction] = [Deduction]
f rm![ClaimeeDeductionReason] = [DeductionReason]
End Select
DoCmd.Close
Else
DoCmd.Close
End If
End Sub
In this procedure I set a Form variable, Frm to address the main form
values. This makes address the form much easier then writing
Forms![Claims]![Fieldname] for each reference. It is neater also. But
first I checked that the form is open. If not, I simply close the form,
becuase the popup could be open outside of the main form, in which case
there would be nothing to update. There are other ways to handle it, but
this is clean and easy. The Isloaded function should be available in the
NorthWinds sample database.
Openargs is addressed like a variable and doesn't need to be declared. It
is available while the form is opened and is a very powerful tool.
Notice how the fields are populated, frm![DedUnits] = [DedUnits].
Frm![DedUnits] addresses the main form and populates its textbox.
The textboxes on the popup form, are unbound. When the popup is opened they
need to be populated which will happen in the Load vent of the form.
For the Load Event.
Private Sub Form_Load()
If IsLoaded("Claims") Then
Dim frm As Form
Set frm = Forms![claims]
Select Case OpenArgs
Case 1
[DedUnits] = frm![DedUnits]
[DedUnitCost] = frm![DedUnitCost]
[Deduction] = frm![Deduction]
[DeductionReason] = frm![DeductionReason]
Case 2
[DedUnits] = frm![ClaimeeDedUnits]
[DedUnitCost] = frm![ClaimeeDedUnitCost]
[Deduction] = frm![ClaimeeDeduction]
[DeductionReason] = frm![ClaimeeDeductionReason]
End Select
End If
End Sub
Here I again set the main for to a variable, frm. This time I pulled the
values from the main form textboxes and populated the unbound textboxes on
the popup. I used the Select Case contruct to determine which textboxes on
the main form to pull from. Again, I used the Isloaded function to
determine whether to address the main form. The the main form wasn't open
then the popup was opened independently. A better method would be to test
for the value of openargs, becuase the mainform could be open, and the popup
still opened independently, but I didn't think about that, becuase the users
here aren't knowledgeable enough to open the popup alone and would have no
reason to do that.
I hope this is easy enough for you to follow. I decided not to include the
method of opening the popup when the equal sign (=) is pushed, because it
may confuse you. If you figure this out and want to try that, let me know
and I will post the procedures.
God Bless,
Mark A. Sam