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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a reference to a control to a function 1

Status
Not open for further replies.

p3t3

Programmer
Aug 21, 2002
46
GB
I have a form with a control "ctrl1" of type textbox.

I wash to pass a reference to the control to a function, then allow the function to update the control,

i.e

sub func(ctrl as control)
ctrl.value = "thats the one!!"
end sub

but, when i call the function i.e func(ctrl1), it just seem to pass the value...

ive tried this

dim ct As Control
set ct = ctrl1
func (ct)

...but this doesn't even set ct to an object...

...sure this is really simple...but whats going on??? set objJedi[skyWalker].Aniken = FatherOf(useThe.Force(objJedi[skyWalker].luke))
 
Hi!

You can try this:

sub func(cntlname As String)

Forms!FormName.Controls(cntlname).Value = "thats the one"

End sub

Then call it using the control's name func("ControlName")

There are other possibilities depending on what event is being used to call the sub.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Thanks, that works... but...

Can I really not pass a reference to the control, just like I would do with a pointer in C???

I would like to do this because I would like the function to be able to update a control on any form... Now I realise I could pass the form name as well to my function, but just passing a reference to a particular control would be a lot neater...

To give a few specifics, I have a calander button which pops up a form with a calder control. I would like to be have the function which pops up the calandar form take an argument of the textbox which is to receive the date... Obviously this calander button will appear on many different forms whereever there is a date field...

again, thanks... set objJedi[skyWalker].Aniken = FatherOf(useThe.Force(objJedi[skyWalker].luke))
 
Hi!

The problem I think you are encountering is that a control isn't recognizable to Access except in relationship to a form or report. Still, there are two possibilities that you can try:

first(and easiest):

sub func(cntl As Control)
func(Forms!YourFormName!YourControlName)
Maybe that will identify the control enough for Access to be able to locate it.

Second(A little harder):

sub func(cntlname As String)

Dim frm As Form

Set frm = Screen.ActiveForm
Forms!(frm.Name).Controls(cntlname) = "thats the one"

Set frm = Nothing

End sub

func(YourcontrolName)

After the calendar form closes the form with the textbox should be the activeform on the screen object.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Hmmmm, excellent. That has given me a good idea. Thanks.

If I could ask one more quik question, can I obtain the name of the last active form...

something like screen.activeform.parent (I know this doesnt work - ive tried many times)

I.e this would give the form name that popped up the calender form (assuming the calandar form is the active form) set objJedi[skyWalker].Aniken = FatherOf(useThe.Force(objJedi[skyWalker].luke))
 
Hi!

As far as I know there is no previous form property. There is a previous control property and, since the previous control is on the form in question, maybe you can use PreviousControl.Parent?(obviously I haven't tried this) Another possible method would be to do it like this:

Dim cntl As Control
Dim frm As Form

Set cntl = Screen.PreviousControl
Set frm = cntl.Parent

etc.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Tried to put this code in the onLoad even of the calandar form...

unfortunatly (from access help regarding .previousControl)

You can't use the PreviousControl property until more than one control on any form has received the focus after a form is opened. Microsoft Access generates an error if you attempt to use this property when only one control on a form has received the focus.

------------------------------------
i.e previousControl only tracks controls on the same form...

thanks anyway Jerby - youve been great help - i'll have a play round and see if i can figure out some method to get to the previous form... set objJedi[skyWalker].Aniken = FatherOf(useThe.Force(objJedi[skyWalker].luke))
 
Hi!

One more time! When you open the calendar form, you can use the OpenArgs to pass the form name:

DoCmd.OpenForm "CalendarForm", , , , , acDialog, Me.Name

You can then pass this along to the function:

func(Me.OpenArgs)

And use it this way:

sub func(FormName As String)

Forms!(FormName).Controls("txtCalendar") = "thats the one"

End sub

Now all you need to do is give the text boxes in each form the same name txtCalendar in this example. If you don't want to do that then call the by this name with the form name attached. Which will change the function like this:

sub func(FormName As String)

Dim cntlName As String

cntlName = "txtCalendar" & FormName

Forms!(FormName).Controls(cntlName) = "thats the one"

End sub

If you use a naming convention for your forms like frmFormName then you would use:

cntlName = "txtCalendar" & Mid(FormName, 4)

This should give you a lot of flexibility.

hth
Jeff Bridgham
bridgham@purdue.edu
 
I've run into the same issue... any recent thoughts since this thread was from 2002?

I'm certain Access is designed to pass controls by reference, but it doesn't seem to be working for me either. Here's the code that isn't working (get an error on the function call line stating that I need an object reference... the function call is passing by value, not by my control reference):

dim gctlMyControl as Control
Set gctlMyControl = Me!txtRecordSource ' I've also tried an explicit reference: PrivateControl (Forms!frmObject_Viewer!txtRecordSource)

PrivateControl (gctlMyControl) ' function call

Public Function PrivateControl(ByRef privCtl As Control)
privCtl.Visible = False
End Function

Thanks for any help...
 
Hi funominal!

Sorry I have been gone so long, but it has been very busy here. The first thing I would need to know is what are you trying to accomplish. If you still need or want an answer let me know.



Jeff Bridgham
bridgham@purdue.edu
 
Yes, still interested. I did a workaround, but really would like to solve this. Want to pass a control by reference.

Public gctlMyControl as Control ' declare var as control type in global module

Set gctlMyControl = Me!mycontrolname ' set at form module

PrivateControl (gctlMyControl) ' function call
' I've also tried an explicit reference using: PrivateControl (Forms!myformname!mycontrolname)


Public Function PrivateControl(ByRef privCtl As Control) As Boolean ' trying to pass control by reference, not by value

privCtl.Visible = False

PrivateControl = privCtl.Visible
End Function


get an error on the function call line stating that I need an object reference...
thanks
 
Hi again!

Depending on the situation, a work around may actually be mor practical, but here is the code that I got to work:

In a Module:

Public Sub basTrial(SomeControl As Control)

SomeControl.Visible = False

End Sub

In the form:

Private Sub Command4_Click()

Dim cntl As Object

Set cntl = Me.Controls("Text0")
Call basTrial(cntl)

End Sub

hth


Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top