Solved

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

Posted on 2003-11-14
10
161 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

685 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