Link to home
Start Free TrialLog in
Avatar of tricks801
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
ASKER CERTIFIED SOLUTION
Avatar of nico5038
nico5038
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tricks801
tricks801

ASKER

the string that gets passed into the method above is a concatenation of different fields from a table.  I just tried entering in very large values for all the fields and it works fine ( it just runs off the list box to where i cant see everything, but no error). The table is also storing the data correctly.  
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)

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?
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)
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.
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)
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
thanks nico