tricks801
asked on
Problem with adding an item to a listbox (runtime error 2176)
Im using the below code to add items to a listbox. I keep gettting runtime error 2176 : the setting for this property is too long (pretty sure this is refering to my listbox.
Public Function AddItem(lstItem As Control, strItem)
Dim arrItem, ctItem
With lstItem
Select Case .ControlType
Case acListBox, acComboBox:
If Not .RowSourceType = "Value List" Then Exit Function
Case Else:
Exit Function
End Select
If .RowSource = Empty Then
.RowSource = strItem
Else
**** .RowSource = .RowSource & ";" & strItem *****
End If
End With
End Function
**** denote that when trying to debug, this was highlighted
Public Function AddItem(lstItem As Control, strItem)
Dim arrItem, ctItem
With lstItem
Select Case .ControlType
Case acListBox, acComboBox:
If Not .RowSourceType = "Value List" Then Exit Function
Case Else:
Exit Function
End Select
If .RowSource = Empty Then
.RowSource = strItem
Else
**** .RowSource = .RowSource & ";" & strItem *****
End If
End With
End Function
**** denote that when trying to debug, this was highlighted
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As stated above, the string has a maximum length. (Thought something around 6k but not sure)
Due to that limitation, I never use the valuelist when th number of strings are coming from a table.
Using a temp table is the only "foolproof" solution.
I guess you need a flexible solution.
In that case you could create one table with:
NameControl
StringValue
And add a variable to indicate 1=Init 2=Add
Thus the first call with "1" can empty the rows with
"delete * from tblValueList where NameControl='" & lstItem.Name & "';"
While a "2" uses an insert.
This will ofcourse only work with unique controlnames, otherwise you'll need to pass the formname too.
Getting the idea ?
Nic;o)
Due to that limitation, I never use the valuelist when th number of strings are coming from a table.
Using a temp table is the only "foolproof" solution.
I guess you need a flexible solution.
In that case you could create one table with:
NameControl
StringValue
And add a variable to indicate 1=Init 2=Add
Thus the first call with "1" can empty the rows with
"delete * from tblValueList where NameControl='" & lstItem.Name & "';"
While a "2" uses an insert.
This will ofcourse only work with unique controlnames, otherwise you'll need to pass the formname too.
Getting the idea ?
Nic;o)
ASKER
the strings that i work with are never more than 100 characters long, and you say that it can hold a maximum of 6k?
Then i should be well under the limit right? That 6k isnt refering to the listbox in total is it?
Then i should be well under the limit right? That 6k isnt refering to the listbox in total is it?
Not when your code has an infinite loop...
Just use a breakpoint and step through the code using F8 to see howmany times the string is appended.
Nic;o)
Just use a breakpoint and step through the code using F8 to see howmany times the string is appended.
Nic;o)
The maximum size for the ValueList property, IIRC, is 255 characters.
If you are exceeding this, then you'll need to either use a table to store your list box values, or a callback function.
If you are exceeding this, then you'll need to either use a table to store your list box values, or a callback function.
ASKER
so the maximum size for the valuelist is 255 characters and thats per row correct? whats a callback function?
Hmm, looks to me you don't want to use a table.
Using a call back function you can achieve a longer string, but there's always a limit.
As stated before, the only real solution is to switch to using a table.
Nic;o)
Using a call back function you can achieve a longer string, but there's always a limit.
As stated before, the only real solution is to switch to using a table.
Nic;o)
No - the maximum size for the *whole* valuelist is 255 characters (ie, the length of all of the values plus the ; put together).
A callback function should more than suffice - if you exceed the maximum allowed by the function, your combo or listbox probably has too many values to be practical, and you'd then need to consider your UI design. Here are two examples of callback functions used with list/combo boxes. You would set the Row Source Type to =<functionname> (eg =ListMondays()) and the Row Source is left blank. These examples are taken from A97 help:
Function ListMondays(fld As Control,id As Variant,row As Variant,col As Variant,code As Variant) As Variant
Dim intOffset As Integer
Select Case code
Case acLBInitialize ' Initialize.
ListMondays = True
Case acLBOpen ' Open.
ListMondays = Timer ' Unique ID.
Case acLBGetRowCount ' Get rows.
ListMondays = 4
Case acLBGetColumnCount ' Get columns.
ListMondays = 1
Case acLBGetColumnWidth ' Get column width.
ListMondays = -1 ' Use default width.
Case acLBGetValue ' Get the data.
intOffset = Abs((9 - Weekday(Now))Mod 7)
ListMondays = Format(Now() + intOffset + 7 * row,"mmmm d")
End Select
End Function
The next example uses a static array to store the names of the databases in the current directory. To call this function, enter ListMDBs as the RowSourceType property setting and leave the RowSource property setting blank.
Function ListMDBs(fld As Control, id As Variant, row As Variant, col As Variant, code As Variant) As Variant
Static dbs(127) As String, Entries As Integer
Dim ReturnVal As Variant
ReturnVal = Null
Select Case code
Case acLBInitialize ' Initialize.
Entries = 0
dbs(Entries ) = Dir("*.MDB")
Do Until dbs(Entries) = "" Or Entries >= 127
Entries = Entries+1
dbs(Entries) = Dir
Loop
ReturnVal = Entries
Case acLBOpen ' Open.
ReturnVal = Timer ' Generate unique ID for control.
Case acLBGetRowCount ' Get number of rows.
ReturnVal = Entries
Case acLBGetColumnCount ' Get number of columns.
ReturnVal = 1
Case acLBGetColumnWidth ' Column width.
ReturnVal = -1 ' -1 forces use of default width.
Case acLBGetValue ' Get data.
ReturnVal = dbs(row)
Case acLBEnd ' End.
Erase dbs
End Select
ListMDBs = ReturnVal
End Function
A callback function should more than suffice - if you exceed the maximum allowed by the function, your combo or listbox probably has too many values to be practical, and you'd then need to consider your UI design. Here are two examples of callback functions used with list/combo boxes. You would set the Row Source Type to =<functionname> (eg =ListMondays()) and the Row Source is left blank. These examples are taken from A97 help:
Function ListMondays(fld As Control,id As Variant,row As Variant,col As Variant,code As Variant) As Variant
Dim intOffset As Integer
Select Case code
Case acLBInitialize ' Initialize.
ListMondays = True
Case acLBOpen ' Open.
ListMondays = Timer ' Unique ID.
Case acLBGetRowCount ' Get rows.
ListMondays = 4
Case acLBGetColumnCount ' Get columns.
ListMondays = 1
Case acLBGetColumnWidth ' Get column width.
ListMondays = -1 ' Use default width.
Case acLBGetValue ' Get the data.
intOffset = Abs((9 - Weekday(Now))Mod 7)
ListMondays = Format(Now() + intOffset + 7 * row,"mmmm d")
End Select
End Function
The next example uses a static array to store the names of the databases in the current directory. To call this function, enter ListMDBs as the RowSourceType property setting and leave the RowSource property setting blank.
Function ListMDBs(fld As Control, id As Variant, row As Variant, col As Variant, code As Variant) As Variant
Static dbs(127) As String, Entries As Integer
Dim ReturnVal As Variant
ReturnVal = Null
Select Case code
Case acLBInitialize ' Initialize.
Entries = 0
dbs(Entries ) = Dir("*.MDB")
Do Until dbs(Entries) = "" Or Entries >= 127
Entries = Entries+1
dbs(Entries) = Dir
Loop
ReturnVal = Entries
Case acLBOpen ' Open.
ReturnVal = Timer ' Generate unique ID for control.
Case acLBGetRowCount ' Get number of rows.
ReturnVal = Entries
Case acLBGetColumnCount ' Get number of columns.
ReturnVal = 1
Case acLBGetColumnWidth ' Column width.
ReturnVal = -1 ' -1 forces use of default width.
Case acLBGetValue ' Get data.
ReturnVal = dbs(row)
Case acLBEnd ' End.
Erase dbs
End Select
ListMDBs = ReturnVal
End Function
ASKER
thanks nico
ASKER