Control reset to Null in For Next Loop

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a function in which I move through all the controls in an Access 2002
form. When I get to a control whose tag matches an argument (strTag), I want
the function to return the control. The problem is that after the 'Next'
statement, my element variable, cntX, gets reset to Null. However, the
function will still be able to find a match in the control's tag, but the
function returns Null.

The code is below.
***********************
* Private Function ControlFromTag(ByRef strTag As String) As Control
* 5: 'Finds a control that has the specified tag and returns the control
*
* 10: Dim cntX As Access.Control 'Declare Access control.
* 20: For Each cntX In Me.Controls 'Move to each control in collection
(an Access form).
* 30: If cntX.Tag = strTag Then 'Check to see if the control's tag
matches the argument.
* 40: Set ControlFromTag = cntX 'Match found, set function equal to
found control.
* 50: Exit Function
* 60: Else
* 70: End If
* 80: Next cntX 'Move to next control in collection. cntX set to Null
after first time Line #80 executes. Why??
* 90: Set cntX = Nothing 'Destroy object.
* 100: End Function
***********************

Any help would be very appreciated.

Thanks,
Eric
 
Hi Eric

If a Control variable (cntX) has a value of Null, it doesn't mean it's been
reset. It just means that the control's Value property is Null - in other
words, the textbox (or whatever) is blank.

If you have some code that says:
Dim c as Control
Set c = ControlFromTag("xyz")
Debug.Print c.Name
Debug.Print c
....then you would expect the first Print line to show the name of the
matching control, and the second to show its value, which may very well be
Null.
 
Graham,
Thank you for your prompt help (I've had some issues myself recently with
accessing msdn forums). I think I understand what you are saying. My
understanding is that since I wasn't referencing a particular property, it
assumes the default property. However, is there a way to to return a
reference to the control object itself, rather than just the value of the
name (or any other property) of the control?

I appreciate your help.

Thanks,
Eric

Graham Mandeno said:
Hi Eric

If a Control variable (cntX) has a value of Null, it doesn't mean it's been
reset. It just means that the control's Value property is Null - in other
words, the textbox (or whatever) is blank.

If you have some code that says:
Dim c as Control
Set c = ControlFromTag("xyz")
Debug.Print c.Name
Debug.Print c
....then you would expect the first Print line to show the name of the
matching control, and the second to show its value, which may very well be
Null.

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Eric said:
I have a function in which I move through all the controls in an Access 2002
form. When I get to a control whose tag matches an argument (strTag), I want
the function to return the control. The problem is that after the 'Next'
statement, my element variable, cntX, gets reset to Null. However, the
function will still be able to find a match in the control's tag, but the
function returns Null.

The code is below.
***********************
* Private Function ControlFromTag(ByRef strTag As String) As Control
* 5: 'Finds a control that has the specified tag and returns the control
*
* 10: Dim cntX As Access.Control 'Declare Access control.
* 20: For Each cntX In Me.Controls 'Move to each control in collection
(an Access form).
* 30: If cntX.Tag = strTag Then 'Check to see if the control's tag
matches the argument.
* 40: Set ControlFromTag = cntX 'Match found, set function equal to
found control.
* 50: Exit Function
* 60: Else
* 70: End If
* 80: Next cntX 'Move to next control in collection. cntX set to Null
after first time Line #80 executes. Why??
* 90: Set cntX = Nothing 'Destroy object.
* 100: End Function
***********************

Any help would be very appreciated.

Thanks,
Eric
 
Hi Eric

Because a control is an object, you must use a Set statement to assign it to
a variable. If you use a Let statement (in other words, just plain =), you
will get the object's default *property* (if it has one).

So, even though your function is returning a Control (object), if you want
to assign that control to another variable, you must use Set. The following
code might help explain it:

Assume you have a textbox named "Text1" with a Tag "xyz" containing the text
"Hello World!"

Dim v as Variant
Dim c as Control
v = ControlFromTag("xyz")
Set c = ControlFromTag("xyz")
Debug.Print v
' gives "Hello World!" (the default property of the object returned by the
function)
Debug.Print c
' gives "Hello World!" (the default property of the object c)
Debug.Print c.Name
' gives "Text1"
Debug.Print c.Tag
' gives 'xyz'

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Eric said:
Graham,
Thank you for your prompt help (I've had some issues myself recently with
accessing msdn forums). I think I understand what you are saying. My
understanding is that since I wasn't referencing a particular property, it
assumes the default property. However, is there a way to to return a
reference to the control object itself, rather than just the value of the
name (or any other property) of the control?

I appreciate your help.

Thanks,
Eric

Graham Mandeno said:
Hi Eric

If a Control variable (cntX) has a value of Null, it doesn't mean it's been
reset. It just means that the control's Value property is Null - in other
words, the textbox (or whatever) is blank.

If you have some code that says:
Dim c as Control
Set c = ControlFromTag("xyz")
Debug.Print c.Name
Debug.Print c
....then you would expect the first Print line to show the name of the
matching control, and the second to show its value, which may very well be
Null.

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Eric said:
I have a function in which I move through all the controls in an
Access
2002
form. When I get to a control whose tag matches an argument (strTag),
I
want
the function to return the control. The problem is that after the 'Next'
statement, my element variable, cntX, gets reset to Null. However, the
function will still be able to find a match in the control's tag, but the
function returns Null.

The code is below.
***********************
* Private Function ControlFromTag(ByRef strTag As String) As Control
* 5: 'Finds a control that has the specified tag and returns the control
*
* 10: Dim cntX As Access.Control 'Declare Access control.
* 20: For Each cntX In Me.Controls 'Move to each control in collection
(an Access form).
* 30: If cntX.Tag = strTag Then 'Check to see if the control's tag
matches the argument.
* 40: Set ControlFromTag = cntX 'Match found, set function equal to
found control.
* 50: Exit Function
* 60: Else
* 70: End If
* 80: Next cntX 'Move to next control in collection. cntX set to Null
after first time Line #80 executes. Why??
* 90: Set cntX = Nothing 'Destroy object.
* 100: End Function
***********************

Any help would be very appreciated.

Thanks,
Eric
 
Graham,
Thank you again for your help. I think we are on the same page as I
understand what you are saying about the Set statement and did use that on
line#40 of my code:
Set ControlFromTag = cntX

However, I tried my code in a brand new database and it seemed to work okay
in that I was able to get my function to return a reference to a control on
an Access form that matched a Tag value. I'm going to take a closer look at
the other code in the module.

Thanks!
Eric

Graham Mandeno said:
Hi Eric

Because a control is an object, you must use a Set statement to assign it to
a variable. If you use a Let statement (in other words, just plain =), you
will get the object's default *property* (if it has one).

So, even though your function is returning a Control (object), if you want
to assign that control to another variable, you must use Set. The following
code might help explain it:

Assume you have a textbox named "Text1" with a Tag "xyz" containing the text
"Hello World!"

Dim v as Variant
Dim c as Control
v = ControlFromTag("xyz")
Set c = ControlFromTag("xyz")
Debug.Print v
' gives "Hello World!" (the default property of the object returned by the
function)
Debug.Print c
' gives "Hello World!" (the default property of the object c)
Debug.Print c.Name
' gives "Text1"
Debug.Print c.Tag
' gives 'xyz'

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

Eric said:
Graham,
Thank you for your prompt help (I've had some issues myself recently with
accessing msdn forums). I think I understand what you are saying. My
understanding is that since I wasn't referencing a particular property, it
assumes the default property. However, is there a way to to return a
reference to the control object itself, rather than just the value of the
name (or any other property) of the control?

I appreciate your help.

Thanks,
Eric

Graham Mandeno said:
Hi Eric

If a Control variable (cntX) has a value of Null, it doesn't mean it's been
reset. It just means that the control's Value property is Null - in other
words, the textbox (or whatever) is blank.

If you have some code that says:
Dim c as Control
Set c = ControlFromTag("xyz")
Debug.Print c.Name
Debug.Print c
....then you would expect the first Print line to show the name of the
matching control, and the second to show its value, which may very well be
Null.

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

I have a function in which I move through all the controls in an Access
2002
form. When I get to a control whose tag matches an argument (strTag), I
want
the function to return the control. The problem is that after the 'Next'
statement, my element variable, cntX, gets reset to Null. However, the
function will still be able to find a match in the control's tag, but the
function returns Null.

The code is below.
***********************
* Private Function ControlFromTag(ByRef strTag As String) As Control
* 5: 'Finds a control that has the specified tag and returns the control
*
* 10: Dim cntX As Access.Control 'Declare Access control.
* 20: For Each cntX In Me.Controls 'Move to each control in collection
(an Access form).
* 30: If cntX.Tag = strTag Then 'Check to see if the control's tag
matches the argument.
* 40: Set ControlFromTag = cntX 'Match found, set function equal to
found control.
* 50: Exit Function
* 60: Else
* 70: End If
* 80: Next cntX 'Move to next control in collection. cntX set to Null
after first time Line #80 executes. Why??
* 90: Set cntX = Nothing 'Destroy object.
* 100: End Function
***********************

Any help would be very appreciated.

Thanks,
Eric
 
Back
Top