Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to create an object by getting its name from a variable?

Posted on 2003-11-14
10
160 Views
Last Modified: 2010-05-03
hi
how to create an object by getting its name from a variable?
for example

dim sObjName as string
sObjName="Form1"

Now the variable sobjname has the name of the form object. How can i access and use the properties and methods of the object from this?
0
Comment
Question by:accentin
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9753259
do this instead

Dim YourVar As Object
Set YourVar = Form1

0
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9753262
btw, you wont get the nice little dropdown menu for events and properties by declaring that in a sub.

if you would rather have that dropdown menu then do this

'In the declaration section of a form or module
Public YourVar As Form1

that will bring the dropdown menu out for that variable.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 9753371
Create yourself a new project and add a textbox.  Paste the code and hit run.

There are two collections you can deal with to find your desired object.  The Forms collection, which holds all the loaded forms in the currently running app, and the Controls Collection, which holds all the controls for a given component.

The app finds the correct form/control by iterating through the appropriate collection.  You can also get a control directly by name like this:

Dim targetControl as control
Set targetControl = Form1.Controls("Text1")

You can only do this with controls, NOT forms!

Dim targetForm as Form
Set targetForm = Forms("Form1") ' <---- Error! Inconsistent usage...thanx Microsoft!

If the control you are trying to find does not exist, an error will be thrown.  So if you attempt to get a control by using a string directly, be sure to use some kind of On Error trapping.

Also, you can check to see if an object has a property by just attempting to use it.  Again, use On Error to gracefully continue if the object doesn't have the property you are looking for.

Regards,

Idle_Mind

Private Sub Form_Load()
    Dim a As Integer, b As Integer
    Dim targetForm As Form
    Dim targetControl As control
    Dim sObjName As String
   
    sObjName = "Form1"
    For a = 0 To Forms.Count - 1
        If Forms(a).Name = sObjName Then
            Set targetForm = Forms(a)
            targetForm.Caption = "Found it!"
           
            sObjName = "Text1"
            For b = 0 To targetForm.Controls.Count - 1
                If targetForm.Controls(a).Name = sObjName Then
                    Set targetControl = targetForm.Controls(a)
                    targetControl.Text = "Gotcha"
                   
                    Exit For
                End If
            Next b
           
            Exit For
        End If
    Next a
End Sub
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9754436
This is a duplicate account see here http://www.experts-exchange.com/help/mistakes.jsp#4.  Please contact Community Support at http://www.experts-exchange.com/Community_Support/ to have this account closed.

If we do not do this in the next 24 hours you risk having both accounts suspended.

Thanks,
Anthony
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9828358
Can we force an accept on these two questions?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 10164689
I believe I answered the question correctly and should get the points.

Idle_Mind
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10165272
>>I believe I answered the question correctly and should get the points.<<
I agree.  However it should be noted that the CleanupPing is just an automated script that is attempting to wake up the thread and it  does not guarantee final cleanup let alone a Moderator will be around anytime soon.  It may be months before points are awarded.  If you feel strongly about it, post a message in Community Support to get this question closed.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 10548395
Moderator, my recommended disposition is:

    Accept Idle_Mind's comment(s) as an answer.

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question