Solved

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

Posted on 2003-11-14
10
157 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
Comment Utility
do this instead

Dim YourVar As Object
Set YourVar = Form1

0
 
LVL 3

Expert Comment

by:NBrownoh
Comment Utility
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
Comment Utility
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
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 85

Expert Comment

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

Expert Comment

by:Mike Tomlinson
Comment Utility
I believe I answered the question correctly and should get the points.

Idle_Mind
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>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
Comment Utility
Moderator, my recommended disposition is:

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

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now