Function that will tell me my control's location?

  • Thread starter Thread starter Bill Mitchell
  • Start date Start date
B

Bill Mitchell

Is there a function that will give me the Access Name for
my control location wherever it is?

For example, say my control is named myControl. Let's
say it is located on a subform called mySubForm on a form
called myForm.

Its Access location is forms![myForm]![mySubform].form!
[myControl]. Is there a fucntion that exists or that I
could write that would return 'forms![myForm]!
[mySubform].form![myControl]' when I clicked on the
control for instance? Would it then return the proper
loaction if that same control were located somewhere else?

This would be very helpful in many situations.

Thanks,

Bill
 
Hi Bill -

That's a great question. I threw together a "quick & dirty" example of how
you can return the full path to a control. For the sake of simplicity I used
the syntax of default collections in this response (see my response to a
question by CSDunn on 8/15/2003 in this newsgroup titled "Referring to
Subforms within Subforms" for more info on this).


Implement
----------
Add the following code to the GotFocus event of controls you wish to return
a value from:

Dim obj As Object

Set obj = Me.ActiveControl
MsgBox MyName(obj) ' just to see what is returned, replace with your
code


Function
----------
Just make sure this function is available from where you want to use it:

Public Function MyName(obj As Object) As String

Dim strName As String

strName = "(""" & obj.Name & """)"

Do

' this error handling is in here, because if parameter object is
invalid
' then separate error handling should capture that!
On Error Resume Next
Set obj = obj.Parent

If Err.Number <> 0 Then ' no more parents

' Prepend collection type for Forms & Reports
If (TypeOf obj Is Form) Then
MyName = "Forms" & strName
ElseIf (TypeOf obj Is Report) Then
MyName = "Reports" & strName
Else
MyName = strName
End If

Exit Function

End If

' Prepend this parent's name
strName = "(""" & obj.Name & """)" & strName
Loop

End Function
 
Steve,

Thanks so much for your solution, but there are a few
problems with it.

I think we need to replace the "(" in your solution
with "[". Also the "[""" & obj.Name & """]" with "[" &
obj.Name & "]", otherwise we get quotation marks inside
the control location name (e.g., Forms!["myForm"]), which
wouldn't work.

Also, we would need to add some sort of counter to see
how 'deep' the control was within the subform structure.
If the control is on myForm, the syntax would be 'Forms!
[myForm]![myControl]; however, if it were on the subform
it would be Forms![myForm]![mySubForm].form![myControl].

Your solution doesn't take into account the new '.form!
[myControl]' section. Maybe we could add a counter to
the loop that would add the .form! part after so many
loops.

Also, we need to add the '!'s to the code.

One last thing - what if mySubForm is named something
else when it is embedded on myForm? For instance, on my
main forms I tend to name my subforms just myForm |
Data1, myForm | Data2, etc. This is because I use code
to put different forms as the subform at different times.

Your solution assumes my subform is called my its form
name when embedded on myForm. It is, but myForm sees it
as myForm | Data1, not mySubForm. To my mind this may be
a very difficult problem to solve.

POSSIBLE SOLUTION?
One possible solution would be to have one function that
gives us a name when the form is on the parent form,
another function that gives the name on a sub form -
another that gives the name on a sub-sub form, etc. Then
maybe have the code do a loop generating a number that
would tell us what level down the control is, the apply
the approriate function to get the name?
 
Back
Top