Solved Referencing a control with a string? (1 Viewer)

allanc

Registered User.
Local time
Today, 16:53
Joined
Nov 27, 2019
Messages
46
Hello Friends!

Happy Friday and V-Day!

The code below is used to change the value of a field to the next one. (Found on Google or possibly this forum, works pretty well)

Code:
Private Sub Combo385Scroll()
[Combo385].SetFocus
If [Combo385].ListIndex <> [Combo385].ListCount - 1 Then
        [Combo385].ListIndex = [Combo385].ListIndex + 1
      Else
        [Combo385].ListIndex = 0
      End If
End Sub

I am trying to write a sub so instead of creating a sub for every control, I would like to create a sub as below so I can call on different controls easily.

Public Sub ScrollNext (FieldName as String)
(To call, I cando ScrollNext("CustomerName")

How do we reference the control using the string provided when calling the sub? Code below is firing a syntax error.

Code:
Public Sub ScrollNext(FieldName As String)
(FieldName).SetFocus              'I want this like to execute CustomerName.SetFocus. Given that CustomerName as String was used to call this sub.
If Me(FieldName).ListIndex <> Me(FieldName).ListCount - 1 Then
Me(FieldName).ListIndex = Me(FieldName).ListIndex + 1
Else
Me(FieldName).ListIndex = 0
End If
End Sub

Thank you for your time!
Cheers!
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:53
Joined
May 21, 2018
Messages
8,463
randValues me.somelistbox
Code:
Public Sub RandValues(lst as access.listbox)
 lst.SetFocus              'I want this like to execute CustomerName.SetFocus. Given that CustomerName as String was used to call this sub.
  If lst.ListIndex <> lst.ListCount - 1 Then
     lst.ListIndex = lst.ListIndex + 1
  Else
    lst.ListIndex = 0
  End If
End Sub
 

allanc

Registered User.
Local time
Today, 16:53
Joined
Nov 27, 2019
Messages
46
randValues me.somelistbox
Code:
Public Sub RandValues(lst as access.listbox)
lst.SetFocus              'I want this like to execute CustomerName.SetFocus. Given that CustomerName as String was used to call this sub.
  If lst.ListIndex <> lst.ListCount - 1 Then
     lst.ListIndex = lst.ListIndex + 1
  Else
    lst.ListIndex = 0
  End If
End Sub

Wow that was quick! Thanks a lot for your help! It works perfectly. (y)(y)(y)

For learning purpose, is there a way to reference a control using a string? Let's say I know that the control name is "CustomerName". How do I reference that control so I can do something like CustomerName.Value = "Allan" ?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:53
Joined
Feb 28, 2001
Messages
27,001
Me.Controls("controlname") is the reference to a control on the form referenced by Me. For further reference, if the control has a .Value property then that will be the default property so you never need to name it on either side of the equals-sign.

Therefore, you CAN write X = Me.Controls("controlname") and get a value. However, not all controls you could reference this way will have values so be careful in what you attempt to do this way.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:53
Joined
May 21, 2018
Messages
8,463
Doing it the way I showed has a much bigger advantage. You can place the code in a standard module and call if from and form
called from form 1
randValues me.listOne on Form 1
Called from form 2
randValues me.lstOne on form 2
Code:
Public Sub RandValues(lst as access.listbox)
lst.SetFocus 'I want this like to execute CustomerName.SetFocus. Given that CustomerName as String was used to call this sub.
If lst.ListIndex <> lst.ListCount - 1 Then
lst.ListIndex = lst.ListIndex + 1
Else
lst.ListIndex = 0
End If
End Sub
If you used a name, the problem is it does not know what form it is on. That is OK if you are only placing the procedure in the forms module, but you would have to repeat the code in the next form's module to reuse it.
Code:
Public Sub RandValues(lstName as string)
dim lst as access.listbox
set lst = me.controls(lstName)
lst.SetFocus 'I want this like to execute CustomerName.SetFocus. Given that CustomerName as String was used to call this sub.
If lst.ListIndex <> lst.ListCount - 1 Then
lst.ListIndex = lst.ListIndex + 1
Else
lst.ListIndex = 0
End If
End Sub
 

allanc

Registered User.
Local time
Today, 16:53
Joined
Nov 27, 2019
Messages
46
Me.Controls("controlname") is the reference to a control on the form referenced by Me. For further reference, if the control has a .Value property then that will be the default property so you never need to name it on either side of the equals-sign.

Therefore, you CAN write X = Me.Controls("controlname") and get a value. However, not all controls you could reference this way will have values so be careful in what you attempt to do this way.
That's very good to know. Thank you!
 

allanc

Registered User.
Local time
Today, 16:53
Joined
Nov 27, 2019
Messages
46
Doing it the way I showed has a much bigger advantage. You can place the code in a standard module and call if from and form
called from form 1
randValues me.listOne on Form 1
Called from form 2
randValues me.lstOne on form 2
Code:
Public Sub RandValues(lst as access.listbox)
lst.SetFocus 'I want this like to execute CustomerName.SetFocus. Given that CustomerName as String was used to call this sub.
If lst.ListIndex <> lst.ListCount - 1 Then
lst.ListIndex = lst.ListIndex + 1
Else
lst.ListIndex = 0
End If
End Sub
If you used a name, the problem is it does not know what form it is on. That is OK if you are only placing the procedure in the forms module, but you would have to repeat the code in the next form's module to reuse it.
Code:
Public Sub RandValues(lstName as string)
dim lst as access.listbox
set lst = me.controls(lstName)
lst.SetFocus 'I want this like to execute CustomerName.SetFocus. Given that CustomerName as String was used to call this sub.
If lst.ListIndex <> lst.ListCount - 1 Then
lst.ListIndex = lst.ListIndex + 1
Else
lst.ListIndex = 0
End If
End Sub
Thank you for your detailed explanation. I have learned a lot :)
 

Users who are viewing this thread

Top Bottom