Solved

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

Posted on 2003-11-14
10
159 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

831 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