Solved

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

Posted on 2003-11-14
10
163 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
[X]
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
  • 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 86

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 86

Expert Comment

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

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

690 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