passing screen.previouscontrol as a variable

  • Thread starter Thread starter William Taylor
  • Start date Start date
W

William Taylor

I have a function that builds a list of selected items in a multi-select
listbox. I pass the form name into the function and use the following code
snippet:
For Each varItem In ctlList.ItemsSelected
' Build String
strTemp = strTemp & "'" & Forms(pstrFormName)!lstTransfers.ItemData(varItem)
& "', "
Next varItem
I want this to be a reusable function so my question is this: Can I pass the
control name in as a variable as well? what is the syntax? would it be a
string variable or a control variable
 
William Taylor said:
I have a function that builds a list of selected items in a
multi-select listbox. I pass the form name into the function and use
the following code snippet:
For Each varItem In ctlList.ItemsSelected
' Build String
strTemp = strTemp & "'" &
Forms(pstrFormName)!lstTransfers.ItemData(varItem) & "', "
Next varItem
I want this to be a reusable function so my question is this: Can I
pass the control name in as a variable as well? what is the syntax?
would it be a string variable or a control variable

Is there any reason you can't just pass an object reference to the
control? You wouldn't even need an argument to identify the form
itself. Suppose your function were defined like this:

'----- start of example code -----
Public Function ListboxItemList( _
ctlList As Access.Listbox, _
Optional Quote As String _
) As String

Dim strTemp As String
Dim varItem As Variant

With ctlList
For Each varItem In .ItemsSelected
strTemp = strTemp & ", " & _
Quote & .ItemData(varItem) & Quote
Next varItem
End With

ListboxItemList = Mid(strTemp, 3)

End Function
'----- end of example code -----

Then, depending on whether your listbox's values are text or numeric,
you would call the function like this:

strList = ListboxItemList(Me!lstMyListbox, "'")

or

strList = ListboxItemList(Me!lstMyListbox)
 
William Taylor said:
Found answer in online help:
forms(VarFormName).Controls(VarControlName)

Good -- but why not pass the control itself, rather than its name and
the name of the form? See my response to your original question.
 
Back
Top