Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating controls in memory? 2

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
0
0
GB
Sorry if this is a very basic question but I'd like a bit of help!

I need to create a VB list box in memory rather than on a form, so that I can still do normal list box things like List1.AddItem, but without actually having a list box on a form.

I tried this:

Code:
Dim EList As ListBox
EList.AddItem "This is a test"
MsgBox EList.List(0)

but it stops on the second line with RTE 91.

Can this be done?

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
What is wrong with having a Listbox on a form that never gets shown?
 
yeah. You can place a listbox on the form, off to the side, and set Visible = FALSE.

If you need something that has a broader scope, then an array may be in order.

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now.
 
Dim EList As ListBox
Set EList = Controls.Add("VB.ListBox", "EListName")
EList.AddItem "This is a test"
MsgBox EList.List(0)
 
Ideally I wanted a form-less solution because this code is in a Module, and even if I hid a list on a form somewhere it'd mean I'd always have to add that form to every project where I used the Module. I was hoping that a more elegant solution would be to create the list in memory.

I thought maybe I was doing it wrong but it sounds like it isn't possible.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
>I'd always have to add that form to every project where I used the Module

So create a project template that already contains the form

However, the real question is - why do you need a listbox at all? i.e. what is it you are trying to achieve that requires a listbox? There may be alternative solutions ...
 
Just have an array variable. You can do everything to an array that you can do to the data in a listbox (and quicker)
 
Say for example that I'm lazy :)

I want to sort a list of names in a comma-delimited string. I want to pass the string to a module that splits the string by the commas, adds each name to a sorted list held in memory, and then reads the list back into a string with the names in order.

Now I KNOW in advance that you're going to say "write a sort routine using an array", and yes, I could. If you can ignore the specific requirement though and just generalise your answer, it seems to be "no" you can't create a list box in memory.

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
>that you're going to say "write a sort routine using an array"

Nah, I had a feeling that it was sorting that you'd be interested in (and was the reason I made the comment about reams of supporting code in response to Ted's suggestion) - and I've certainly used a hidden lisbox for exactly this purpose in the past.

However, why not consider a disconnected recordset? They are a lot more flexible than a list box. here's an example:
Code:
[blue]Private Sub Command1_Click()
    Debug.Print SortCSV("wombat,spoon,elephant")
End Sub

' Late binding variant
Private Function SortCSV(strCSV As String) As String
    Dim rs As Object
    Dim item As Variant
    
    Set rs = CreateObject("ADODB.Recordset")
    With rs
       .Fields.append "field1", adVarChar, 50
        .Open
        For Each item In Split(strCSV, ",")
            .AddNew "field1", item
        Next
        .Sort = "field1 ASC"
        .Update
        SortCSV = .GetString(, , , ",")
        SortCSV = Left(SortCSV, Len(SortCSV) - 1)
    End With

End Function[/blue]
 
OK thanks, as always it's interesting to learn something new!

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Nice little SIMPLE way to do a sort. I couldn't get the late binding version to work without referencing ADO 2.8 library.
 
Sorry, yes - you need to add the following declaration:

Const adVarChar = 200
 
Ah...Thanks! Since I can't give you another
star.gif
officially this one will have to do.

Thanks again strongm! :)
 
thanks a lot

========================================
I kept my Job because of TEK-TIPS
Thanks a lot to all who keeps on helping others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top