chobe
asked on
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.
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.
You can call the textbox's onclick event from within the calendar's onclick event like
sub yoursub
'code
'code
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
ctrl.setfocus
dosomething = true 'return true if no error
exit function
ErrorTrap:
debug.print err.description
dosomething=false
end function
I'm not sitting in front of Access ATM, but the above should run as an example.
sub yoursub
'code
'code
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
ctrl.setfocus
dosomething = true 'return true if no error
exit function
ErrorTrap:
debug.print err.description
dosomething=false
end function
I'm not sitting in front of Access ATM, but the above should run as an example.
To manipulate the controls properties from another form it would be:
Forms(MyForm).Controls(For ms(MyForm) .ControlOr igin).Valu e = ""
Forms(MyForm).Controls(For
Torrwin's post prompts me to reword mine for clarity..
from
You can call the textbox's onclick event from within the calendar's onclick event like
to
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.
from
You can call the textbox's onclick event from within the calendar's onclick event like
to
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.
ASKER
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....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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:
Forms(MyForm).ControlOrigi