Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I Reference another form with variables

Status
Not open for further replies.

tazlee1

Technical User
Sep 30, 2005
4
US
My objective is to have one form with a calendar that is used to input a date for other forms that have a date field. I know how to set up the calendar on the same form as the date field but I am trying to make one common calendar for the whole db. My problem is that once I set up the calendar, I don't know how to write the "OnClick" event code to reference the form that opened the calendar form. I also need to have the field name to be a variable as well because of DateFrom and DateTo fields. Any suggestions would be appreciated. Thanks
 
There are a lot of different calendar controls out there, and a lot of ways to do it. Here is a simple one using a bound ActiveX calendar control. Build a form with nothing on it but a calendar (frmPopUpCalendar). Set the propery of "Dialog" to "Yes". On this form put a Public variable right at the top.

--Begin Code
Option Compare Database
Option Explicit
Public mControlSource As Control

Public Property Set ControlSource(theControl As Control)
Set mControlSource = theControl
If IsDate(mControlSource.Value) Then
activeXctlCalendar.Value = mControlSource.Value
activeXctlCalendar.Year = Year(mControlSource.Value)
End If
End Property

Private Sub activeXctlCalendar_Click()
If mControlSource.Name = "activeXctlCalendar" Then
MsgBox " You must set the control source in code for this to work." & vbCrLf & _
" ex. Forms![frmPopUpCalendar].ControlSource = theTextBox "

Exit Sub
End If
mControlSource.Value = activeXctlCalendar.Value
DoCmd.Close acForm, Me.Name
End Sub
Private Sub Form_Open(Cancel As Integer)
'If they do not set the control source this form does not work.
'Set it to itself as a way to initialize the object
Set mControlSource = activeXctlCalendar
End Sub
---End Code

From your click event use open the pop up calendar form
and set the "Control Source".
If you click in "txtBoxBirthDate" then
Forms![frmPopUpCalendar].ControlSource = txtBoxBirthDate

I have not looked at Zameers, they may have a lot better functionality. There are tons of free examples out there. Of all the useless templates that come with Access this would be worthwhile.
 
Actually I showed you this to demonstrate something that a lot of people do not understand about VBA. Your form and report modules are "class" modules like any other class modules. Not only can you set values of the public variables, but you can use public procedures and functions. Here is a simple example. In form (frmTwo) have a public variable
public strTheFormThatCalled as string
From form (frmOne) open frmTwo and before closing frmOne add
forms("frmTwo").strTheFormThatCalled = me.name

in frmTwo type in some event
debug.print me.strTheFormThatCalled
It will return "frmOne"
You just built a custom property for frmTwo.

Maybe more than you wanted, but the concept opens up a lot of possibilities for your coding.
 
Thanks ZmrAbdulla and MajP, that helps me a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top