PDA

View Full Version : Pass value between forms.



Blitskrieg
08-16-2011, 03:12 PM
Greetings!

I have a form that contains a combobox with a set of values. I want to be able to call that same form and return the value of the combobox into the same control on any one of up to (5) forms that I called it from. Best way to go about this?

Here is the code I used to link it to my first form:


Private Sub OK_Click()
Forms![Form Name]![Call Code] = Me.Codes.Value
DoCmd.Close
Exit Sub
End Sub


Many thanks in advance. This forum has always been helpful!

HiTechCoach
08-17-2011, 01:41 PM
I think what you want is a generic form to be called by multiple forms and or conttols on the same form.

The way I handle this is to create a wrapper function. I pass the active screen control to the function. The function opens the form. the passes the data back to the control passed.

See this example: Pop ups - Calendar, Calculator, Zoom box (http://www.hitechcoach.com/index.php?option=com_docman&task=doc_details&gid=60&Itemid=28)

Blitskrieg
08-17-2011, 02:27 PM
I thank you for your reply. Unfortunately I was unable to look into your example and understand which module/sub would provide a solution to my problem. I'm leaning toward trying to accomplish this by using a Global Variable, is that something you would be able to assist with.

Thanks again for the reply, please forgive my level of understanding.

HiTechCoach
08-17-2011, 03:00 PM
I thank you for your reply. Unfortunately I was unable to look into your example and understand which module/sub would provide a solution to my problem. I'm leaning toward trying to accomplish this by using a Global Variable, is that something you would be able to assist with.

Thanks again for the reply, please forgive my level of understanding.


The example has three versions of how to handle this. Look at the double click event of one of the text box controls. This will tell you the function you need to check out.

About Global Variables:

It is probably possible to do it with a Global Variable. I avoid Global Variables. In general, Global Variable are not considered Best Practice in software development. This is not limited to just Access. Global Variable as have issues with getting reset in Access. FWIW: In the hundreds of Access database I have created and/or worked on I have never used a Global Variable. The few databases that others had created the used Global Variable(s) were buggy. Once I removed the Global Variable(s) it was a lot more stable.

Check out the Calculator example:

The double click event is:


=PopupCalc([Screen].[ActiveControl])

This is all the code you need in a standard module:


Option Compare Database 'Use database order for string comparisons
Option Explicit

Private Function isFormLoaded(strFormName As String)
isFormLoaded = SysCmd(SYSCMD_GETOBJECTSTATE, A_FORM, strFormName)
End Function

Function PopupCalc(ctl As Control) As Variant
' Load frmCalc in dialog mode, and return the
' calculated value at the end of the use session.
Const FRM_CALC = "zsfrmCalc"
Dim strArgs As String
If IsNull(ctl.Value) Then
strArgs = "0"
Else
strArgs = ctl.Value
End If
DoCmd.OpenForm FRM_CALC, , , , , A_DIALOG, strArgs
If isFormLoaded(FRM_CALC) Then
ctl.Value = Forms(FRM_CALC)!lblReadOut.Caption
DoCmd.Close A_FORM, FRM_CALC
End If
End Function

You code use the above code with just a few edits:

1) change the form name:


Const FRM_CALC = "zsfrmCalc" ' set to your form name

2) Change this line to set the value

from


ctl.Value = Forms(FRM_CALC)!lblReadOut.Caption

to


ctl.Value = Forms(FRM_CALC)!Codes

Tabitha30
09-15-2011, 02:53 AM
thank you for your sharing, it is so useful to me.