Passing a control as a parameter, how to simply code

Posted on 2009-12-23
Last Modified: 2013-12-25
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.
Question by:chobe
    LVL 13

    Expert Comment

    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:
    LVL 18

    Expert Comment

    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.
    LVL 13

    Expert Comment

    To manipulate the controls properties from another form it would be:
    Forms(MyForm).Controls(Forms(MyForm).ControlOrigin).Value = ""
    LVL 18

    Expert Comment

    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.


    Author Comment

    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....
    LVL 13

    Accepted Solution

    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.

    Author Closing Comment

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
    Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
    In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
    With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now