Passing a value from one form to another

T

Tom

Hello:

I have a form ("frmCorrespondence") that has a date combo (DocumentDate).

When clicking on the combo, DocumentDate calls another form (frmCalendar).
On the frmCalendar, I have a calendar object (btw, the reason why I bring
up the calendar via another form is because I have different worksheets that
also needs to bring up the same calendar).

I utilized Stephen Leban's method (http://www.lebans.com/openform.htm) to
"position" a form based on the position of the control that calls the
subform.


The following is the original process (calendar object resides directly on
the same worksheet; Visible = False when opening the main form).
1. Click on DocumentDate
2. Calendar object pops up
3. I select a date
4. DocumentDate combo is updated w/ the selected date

Now, after having made the changes to accomodate Stephen's approach, the
following is (is not) happening:
1. Click on DocumentDate
2. Calendar object pops up (actually "frmCalendar" w/ the calendar object
3. I select a date
4. DocumentDate combo is NOT updated



Below are the 2 sets of functions (Original & Changed versions). Does
anyone know as to how I can pass on the value from "frmCalendar.Calendar" to
"frmCorrespondence.DocumentDate"?


Thanks so much in advance,
Tom




======= OriginalFunctions=============
Private Sub DocumentDate_MouseDown(Button As Integer, Shift As Integer, X As
Single, Y As Single)

' Show Calendar and set its date.
Calendar.Visible = True
Calendar.SetFocus
' Set to today if DocumentDate has no value.
Calendar.Value = IIf(IsNull(DocumentDate), Date, DocumentDate.Value)

End Sub

Private Sub Calendar_Click()
' Set OrderDate to the selected date and hide the calendar.
DocumentDate.Value = Calendar.Value
DocumentDate.SetFocus
Calendar.Visible = False

End Sub
======= OriginalFunctions=============




******* ChangedFunctions*************
Private Sub DocumentDate_MouseDown(Button As Integer, Shift As Integer, x As
Single, y As Single)

Dim blRet As Boolean
blRet = PositionFormRelativeToControl("frmCalendar",
Forms![frmCorrespondence].[DocumentDate], 2)

Forms![frmCalendar].[Calendar].Value =
IIf(IsNull(Forms![frmCorrespondence].[DocumentDate]), Date,
Forms![frmCorrespondence].DocumentDate].Value)

End Sub

*******************************************

Private Sub Calendar_Click()

DocumentDate.Value = Forms![frmCalendar].[Calendar].Value
DocumentDate.SetFocus
Forms![frmCalendar].[Calendar].Visible = False

End Sub

******* ChangedFunctions*************
 
G

Graham Mandeno

Hi Tom

Presumably frmCorrespondence no longer has a control on it named Calendar,
so your Calendar_Click code will never be executed.
Instead, you are opening frmCalendar, which now contains the control named
Calendar.

Usually, the method to employ would be to open frmCalendar using acDialog
for the WindowMode argument, and this would ensure that the calling code was
suspended for the time that frmCalendar was visible. Then, when a date was
selected, code in frmCalendar would hide itself, allowing the calling code
in frmCorrespondence to continue. It would then retrieve the date value
from the calendar control, and close frmCalendar.

Unfortunately, Stephen's code relies on the form NOT being opened with
acDialog, because after opening the form, his code needs to continue
executing to position the form. So, you will need another solution.

I suggest you define a global variable in a standard module:
Public ctlCalendarDestination as Control

You can point this variable to your date textbox before opening the
frmCalendar, and frmCalendar can retrieve the value from there (if there is
one) and set the new value back before it closes.

So, your code on frmCorrespondence is simply this:

Private Sub DocumentDate_Click()
Set ctlCalendarDestination = DocumentDate
Call PositionFormRelativeToControl("frmCalendar", _
DocumentDate, 2)
End Sub

(Note that I have used the Click event instead of MouseDown, as you are not
interested in the mouse position or which button was clicked)

Then, in frmCalendar, initialize the Calendar.Value on opening::

Private Sub Form_Load()
If IsNull(ctlCalendarDestination) Then
Calendar.Value = Date
Else
Calendar.Value = ctlCalendarDestination.Value
End If
End Sub

.... and write the selected date value back to the control, and close the
form:

Private Sub Calendar_Click()
ctlCalendarDestination.Value = Calendar.Value
DoCmd.Close acForm, Me.Name
End Sub
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


Tom said:
Hello:

I have a form ("frmCorrespondence") that has a date combo (DocumentDate).

When clicking on the combo, DocumentDate calls another form (frmCalendar).
On the frmCalendar, I have a calendar object (btw, the reason why I bring
up the calendar via another form is because I have different worksheets
that also needs to bring up the same calendar).

I utilized Stephen Leban's method (http://www.lebans.com/openform.htm) to
"position" a form based on the position of the control that calls the
subform.


The following is the original process (calendar object resides directly on
the same worksheet; Visible = False when opening the main form).
1. Click on DocumentDate
2. Calendar object pops up
3. I select a date
4. DocumentDate combo is updated w/ the selected date

Now, after having made the changes to accomodate Stephen's approach, the
following is (is not) happening:
1. Click on DocumentDate
2. Calendar object pops up (actually "frmCalendar" w/ the calendar object
3. I select a date
4. DocumentDate combo is NOT updated



Below are the 2 sets of functions (Original & Changed versions). Does
anyone know as to how I can pass on the value from "frmCalendar.Calendar"
to "frmCorrespondence.DocumentDate"?


Thanks so much in advance,
Tom




======= OriginalFunctions=============
Private Sub DocumentDate_MouseDown(Button As Integer, Shift As Integer, X
As Single, Y As Single)

' Show Calendar and set its date.
Calendar.Visible = True
Calendar.SetFocus
' Set to today if DocumentDate has no value.
Calendar.Value = IIf(IsNull(DocumentDate), Date, DocumentDate.Value)

End Sub

Private Sub Calendar_Click()
' Set OrderDate to the selected date and hide the calendar.
DocumentDate.Value = Calendar.Value
DocumentDate.SetFocus
Calendar.Visible = False

End Sub
======= OriginalFunctions=============




******* ChangedFunctions*************
Private Sub DocumentDate_MouseDown(Button As Integer, Shift As Integer, x
As Single, y As Single)

Dim blRet As Boolean
blRet = PositionFormRelativeToControl("frmCalendar",
Forms![frmCorrespondence].[DocumentDate], 2)

Forms![frmCalendar].[Calendar].Value =
IIf(IsNull(Forms![frmCorrespondence].[DocumentDate]), Date,
Forms![frmCorrespondence].DocumentDate].Value)

End Sub

*******************************************

Private Sub Calendar_Click()

DocumentDate.Value = Forms![frmCalendar].[Calendar].Value
DocumentDate.SetFocus
Forms![frmCalendar].[Calendar].Visible = False

End Sub

******* ChangedFunctions*************
 
T

Tom

Graham:

Thanks... that works great now.

I appreciate your help in this matter.

Tom



Graham Mandeno said:
Hi Tom

Presumably frmCorrespondence no longer has a control on it named Calendar,
so your Calendar_Click code will never be executed.
Instead, you are opening frmCalendar, which now contains the control named
Calendar.

Usually, the method to employ would be to open frmCalendar using acDialog
for the WindowMode argument, and this would ensure that the calling code was
suspended for the time that frmCalendar was visible. Then, when a date was
selected, code in frmCalendar would hide itself, allowing the calling code
in frmCorrespondence to continue. It would then retrieve the date value
from the calendar control, and close frmCalendar.

Unfortunately, Stephen's code relies on the form NOT being opened with
acDialog, because after opening the form, his code needs to continue
executing to position the form. So, you will need another solution.

I suggest you define a global variable in a standard module:
Public ctlCalendarDestination as Control

You can point this variable to your date textbox before opening the
frmCalendar, and frmCalendar can retrieve the value from there (if there is
one) and set the new value back before it closes.

So, your code on frmCorrespondence is simply this:

Private Sub DocumentDate_Click()
Set ctlCalendarDestination = DocumentDate
Call PositionFormRelativeToControl("frmCalendar", _
DocumentDate, 2)
End Sub

(Note that I have used the Click event instead of MouseDown, as you are not
interested in the mouse position or which button was clicked)

Then, in frmCalendar, initialize the Calendar.Value on opening::

Private Sub Form_Load()
If IsNull(ctlCalendarDestination) Then
Calendar.Value = Date
Else
Calendar.Value = ctlCalendarDestination.Value
End If
End Sub

... and write the selected date value back to the control, and close the
form:

Private Sub Calendar_Click()
ctlCalendarDestination.Value = Calendar.Value
DoCmd.Close acForm, Me.Name
End Sub
--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand


Tom said:
Hello:

I have a form ("frmCorrespondence") that has a date combo (DocumentDate).

When clicking on the combo, DocumentDate calls another form (frmCalendar).
On the frmCalendar, I have a calendar object (btw, the reason why I bring
up the calendar via another form is because I have different worksheets
that also needs to bring up the same calendar).

I utilized Stephen Leban's method (http://www.lebans.com/openform.htm) to
"position" a form based on the position of the control that calls the
subform.


The following is the original process (calendar object resides directly on
the same worksheet; Visible = False when opening the main form).
1. Click on DocumentDate
2. Calendar object pops up
3. I select a date
4. DocumentDate combo is updated w/ the selected date

Now, after having made the changes to accomodate Stephen's approach, the
following is (is not) happening:
1. Click on DocumentDate
2. Calendar object pops up (actually "frmCalendar" w/ the calendar object
3. I select a date
4. DocumentDate combo is NOT updated



Below are the 2 sets of functions (Original & Changed versions). Does
anyone know as to how I can pass on the value from "frmCalendar.Calendar"
to "frmCorrespondence.DocumentDate"?


Thanks so much in advance,
Tom




======= OriginalFunctions=============
Private Sub DocumentDate_MouseDown(Button As Integer, Shift As Integer, X
As Single, Y As Single)

' Show Calendar and set its date.
Calendar.Visible = True
Calendar.SetFocus
' Set to today if DocumentDate has no value.
Calendar.Value = IIf(IsNull(DocumentDate), Date, DocumentDate.Value)

End Sub

Private Sub Calendar_Click()
' Set OrderDate to the selected date and hide the calendar.
DocumentDate.Value = Calendar.Value
DocumentDate.SetFocus
Calendar.Visible = False

End Sub
======= OriginalFunctions=============




******* ChangedFunctions*************
Private Sub DocumentDate_MouseDown(Button As Integer, Shift As Integer, x
As Single, y As Single)

Dim blRet As Boolean
blRet = PositionFormRelativeToControl("frmCalendar",
Forms![frmCorrespondence].[DocumentDate], 2)

Forms![frmCalendar].[Calendar].Value =
IIf(IsNull(Forms![frmCorrespondence].[DocumentDate]), Date,
Forms![frmCorrespondence].DocumentDate].Value)

End Sub

*******************************************

Private Sub Calendar_Click()

DocumentDate.Value = Forms![frmCalendar].[Calendar].Value
DocumentDate.SetFocus
Forms![frmCalendar].[Calendar].Visible = False

End Sub

******* ChangedFunctions*************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top