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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you use a list box to select a form? 1

Status
Not open for further replies.

GWhiz

Programmer
Dec 16, 1999
49
US
Hi, folks.

I'm using a list box to display a list of forms. The form name displayed needs to be a bit more descriptive than the form name of the object that's actually going to be loaded, so I've stored the name to display and the associated form name to load in a database that's easy to update.

The application is constantly being expanded by adding new forms, and I'm trying to keep code maintenance to a minimum -- so I'd like the program to automatically find new forms every time it's started, and to find a match between a new form and its name as stored in the database.

As long as the name of the form object in the database is correct -- that is, if it matches perfectly the name of a new form -- the program should, theoretically, load the form.

However, the name stored in the database is a string, not a true object reference, and I can't figure out how to make the string into a valid object reference that actually causes a form to be loaded.

The only way I've been able to make this work is the "brute force" method, where I add a new explicit object reference each time a new form is created, but this requires adding explicit object references every time a new form is created.

I've tried using the "SET" command, passing the string name of the form in the database to an object variable, but this either doesn't seem to work, or the way I'm using it is not correct.

I'm just looking for a more "elegant" way to do this on the fly, so I can just create the new form, add its descriptive name and the associate object name to the database, and let the program find the form with the same (object) name on its own, without having to add an explicit object reference to the code every time I create a new form.

In other words, an algorithm for finding and loading new forms, starting with nothing more than a string name of the form, rather than using the "brute force" method.

Anyone have some experience with this?

Thanks so much. Your help, as always, has been invaluable.

Respectfully,

GWhiz
 
TestFormName (below) is correct name of object (returned from list box), but the following returns an "Object required" error:

Dim TestForm As Form
TestFormName = Listbox.ListIndex
Set TestForm = TestFormName

The following returns a "Can't assign to read-only property" error:

Dim TestForm As Form
TestFormName = Listbox.ListIndex
TestForm.Name = TestFormName

The following returns a "User-defined type not defined" error:

Dim TestForm As Form
TestFormName = Listbox.ListIndex
Set TestForm = New TestFormName

No matter how I re-arrange things, some kind of error is created.

I can't seem to find a combination that works.

Any suggestions?
 
GWhiz,

I think you have to use a case statement. Something like this.

Code:
     Select Case Listbox.List(Listbox.ListIndex)
        Case "frmAnother1"
           frmAnother1.Show
        Case "frmAnother2"
           frmAnother2.Show
        Case "frmAnother3"
           frmAnother3.Show
        Case "frmAnother4"
           frmAnother4.Show
     End Select

The List Box contains strings and I don't know of any conversion from String to Form (eg CInt, CLng... - I don't think there is a CFrm. Although that would be an interesting, and in your case, useful function).



HyperEngineer
If it ain't broke, it probably needs improvement.
 
Hyper:

Thanks very much for your kind and helpful response.

It's a compact variation of the "brute force" approach, where I need to add a new case at the code level for each new form I create rather than let variables handle it all -- but it works perfectly, adding another case takes very little actual time, so maybe that's about as good as it gets.

I very much appreciate your input.

Respectfully,

GWhiz
 
GWhiz,

Glad to help and thanks for the star.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
You might want to check this out too: thread222-795815. strongm's code solves the issue you raise, allowing you to open a form given its name in a string variable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top