Returning a value to a form from a pop-up

  • Thread starter Thread starter molsonexpert
  • Start date Start date
M

molsonexpert

I posted a similar question in another newsgroup, but I still don't have a
solution:

I have a text box that, when clicked, opens a pop up form. On the pop up, a
user enters data, a calculation is made, and when the pop up is closed, that
calculated amount appears in the text box that was originally clicked. So
far, this works fine. My question is, I need this scenario to work for about
8 other text boxes, using the same pop up form. The way I'm doing it, I
would need to create 8 separate pop up forms (because the OnClose event for
the form is referencing the text box on the main form), but I'm thinking
there's a way to only use one, no?

Thanks.

steve.
 
When you open the pop-up form (presumably using DoCmd.OpenForm), pass the
name of the textbox to return the value to as the OpenArgs argument.

In the pop-up form, read the form's OpenArgs value to determine where to
return the value to.
 
Hello Steve,

Not knowing the method you are using to reference the original textbox, but
I would suggest that then you open the popup form you assign a value to the
Openargs property which would have a different value for each different
control.

Example:

DoCmd.OpenForm "PopUpFormName", , , , , , 1

The 1 will pass to the popup form which you can reference like a Public
Variable. Then when you do the calculation on the Popup form you can use
Select Case staterment like this:

Select Case OpenArgs

Case 1
<calculation for textbox1>
Case 2
<calculation for textbox2>
Case 3
<calculation for textbox3>
Case 4
<calculation for textbox4>

End Select


God Bless,

Mark A. Sam
 
Mark A. Sam said:
Hello Steve,

Not knowing the method you are using to reference the original textbox,
but
I would suggest that then you open the popup form you assign a value to
the
Openargs property which would have a different value for each different
control.

Example:

DoCmd.OpenForm "PopUpFormName", , , , , , 1

The 1 will pass to the popup form which you can reference like a Public
Variable. Then when you do the calculation on the Popup form you can use
Select Case staterment like this:

Select Case OpenArgs

Case 1
<calculation for textbox1>
Case 2
<calculation for textbox2>
Case 3
<calculation for textbox3>
Case 4
<calculation for textbox4>

End Select


God Bless,

Mark A. Sam

Thanks for the help, but unfortunately I'm pretty weak on writing code.
Could you give me an example? let's say on my main form ("Main1"), the text
box is called Text1. Clicking on it will bring up the pop up form called
Pop1. The calculation is in a text box called Calc1. One problem I don't
understand is how the OpenArgs gets referenced, or how to declare it as a
variable.

Thanks again for all you help.
 
Hello Steve,

I can, in fact this is something I was working on today. It is a little
involved writing it out so that you can understand, so I'll do it tomorrow
when I'm not so bushed. In fact, my technique opens the popup form when the
user clicks the = sign key or clicks a button. I'll show you both.

God Bless,

Mark
 
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
 
Mark A. Sam said:
Hello Steve,

Here is a working example: I'll use my form names to save time:

Wow, many thanks Mark. I think you went above and beyond the call on this
one. I'll be playing with this later today.

btw, on a side note, could you suggest any good resources on coding? I'm
actually not too bad with Access but my vba coding leaves a lot to be
desired.

Thanks once again.

steve.
 
Your welcome. I can't suggest a book, becuase there are so many books on
Access. I have been doing Access since the early 90's and learned as I
went along. I'm not a great programmer, just experienced. I have been
working with Access so much, I can figure things out.

fmsinc.com has an app called Code Tools with many good examples which you
can cut and paste into your modules. Also try the Access Web (
http://www.mvps.org/access/ ) for programming examples.

God Bless,

Mark
 
Mark A. Sam said:
Your welcome. I can't suggest a book, becuase there are so many books on
Access. I have been doing Access since the early 90's and learned as I
went along. I'm not a great programmer, just experienced. I have been
working with Access so much, I can figure things out.

fmsinc.com has an app called Code Tools with many good examples which you
can cut and paste into your modules. Also try the Access Web (
http://www.mvps.org/access/ ) for programming examples.

God Bless,

Mark

Thanks again, and your code works perfectly.

steve.
 
Back
Top