Passing a control as a parameter, how to simply code

My form has several textboxes to capture timeline events (dates). To control input of the date I am using a activex calendar control object.

The textbox onclick event is used to display the calendar, set the calendars date value to any previously entered date existing in the field or if null current date, and to set the name of the textbox in the calendars .tag property for use in the calendar's onclick event. At this point, code stops pending calendar's on click or lost focus events.

In the on click event of the calendar I use a select case = calendar.tag to then insert the calendar date into which ever textbox the user clicked.

Problem is I may need to requery or manipulate the text box properties (setting lock property etc) after date is entered etc.  I can't figure out how to return to the textbox onclick procedure from the calendars onclick event. Screen.PreviousControl is not useful since focus may change several time is the course of events.  I read about creating a public property or passing the control instead of using a very complicated select case procedure but I can't figure out how to make this work.  Any suggestions would be appreciated.
Who is Participating?
And that's where the code I mentioned comes in handy.  If you have the textbox's name stored in a property, you can access it from your calendar's onclick event.
I don't think you can return to the textbox's OnClick event itself, but you can definitely keep track of which textbox it was using a property.

At the top of your form declare a module-level variable to store the textbox's name in:
Private m_sControlOrigin as String

Then, create the property to be accessed:

Public Property Get ControlOrigin() as String
        ControlOrigin = m_sControlOrigin
End Property

Public Property Let ControlOrigin(ByVal controlName As String)
        m_sControlOrigin = controlName
End Property

In the OnClick even of each of your textboxes, set the value of the variable:
m_sControlOrigin = "MyTextboxName"

To access this from another form it would be:
You can call the textbox's onclick event from within the calendar's onclick event like

sub yoursub
call textbox_click()
' more code
end sub

or just textbox_click (without call or the parentheses)

or you can pass the control as an object variable...

'e.g. In a form's class module
sub yoursub
dim objTextbox as control
dim boolSuccess as boolean
set objTextbox = me.txtCompanyname
call doSomething(objTextbox) 'just call if you don't require the return value
'or assign function result to variable
boolSuccess = doSomething(objTextbox)
end sub

function dosomething(ctrl as control)
on error goto Errortrap
    ctrl.visible = true
    ctrl.locked = true
    dosomething = true 'return true if no error
    exit function
debug.print err.description
end function

I'm not sitting in front of Access ATM, but the above should run as an example.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

To manipulate the controls properties from another form it would be:
Forms(MyForm).Controls(Forms(MyForm).ControlOrigin).Value = ""
Torrwin's post prompts me to reword mine for clarity..
 You can call the textbox's onclick event from within the calendar's onclick event like
You can call the textbox's onclick event handler (if you've defined a textbox_Click sub in the form's class module) from within the calendar's onclick event handler.

chobeAuthor Commented:
Using Simon's code I was able to create the object name, and call myfunc to open the calendar, set the calendar.value to ctrl.value, unlock Ctrl, etc., from the textbox click event, then things fall apart.  Code ends and now I'm left with getting the calendars pick date back to the textbox from the calendar's onclick event....
chobeAuthor Commented:
Using your suggestion worked great! Very simple...Don't know why first time I accepted your answer did not work but thanks again for the assist!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.