Solved

Converting a string to a variable.

Posted on 2004-04-02
12
415 Views
Last Modified: 2013-11-14
If I was using VBA id have used Eval(stringname & "." controlname) to pass my control to an object variable, however, im using VB 6 therefore:

I have a form called frmAccountRecords with a frame control on it called FInner.
Im reading the form name from a table and I need to pass FInner to a function.

Therefore, essentially I have a string variable containing "frmAccountRecords" and I want to convert that string to a form so I can pass Accountrecords.FInner to my function.

I've tried using the Eval command supplied with MS Script Control but havent had any luck so far.

Any help would be much appreciated.
0
Comment
Question by:kinton
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 9

Expert Comment

by:dancebert
ID: 10741043
No need for Eval, just pass the frame as a variable of type frame

Private Sub Form_Load()
    Frame1.Caption = "I was passed!"
    MsgBox ReadCaption(Frame1)
End Sub

Private Function ReadCaption(fr As Frame) As String
    ReadCaption = fr.Caption
End Function
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 250 total points
ID: 10741083
Something like this maybe...


'get reference to passed in form
Dim frm As Form

For Each frm In Forms
    If frm.Name = "frmAccountRecords" Then
        Call MyFunction(frm.FInner)
    End If
Next
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10741098
Dim myFormName As String, myForm As Form

    myFormName = "frmAccountRecords" '<<< or any other string from your forms table
    Forms.Add (myFormName)
    Set myForm = Forms(Forms.Count - 1)
    myForm.Show

S
0
 
LVL 2

Author Comment

by:kinton
ID: 10741250
Thanks for all of your replies.

Shauli: Does your code create a new form from the variable myFormName?

dancebert: I think you missed my point.  
The frame's location is determined by a column in a table.   Im reading the name of the form in from a table, therefore to pass the object I need to convert the string to a form variable.  The frame is always called the same thing.

AzraSound: I thought of looping through each form but was hoping there would be an equivalent function to the old VBA/JavaScript Eval function.
I thought looping round all of the forms every time I needed to refence one from a table would slow things down?

If I can't find an equivalent for the Eval command, looping round the forms will have to do!  Thanks for the info tho.

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 10741302
Yes, Forms.Add will simply create a new instance of your form.  The only way I know for grabbing a reference to a form given only its name is to iterate over the open forms.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 10741309
BTW, I think you will find any hit on performance would be negligible
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:kinton
ID: 10741373
It looks like that is what I will have to do then.  I wonder why they didn't inlcude the Eval command???  That was one of the handiest commands ever if you made a habbit of storing lots of info in tables or .ini files!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 10741425
There is a nifty function called CallByName which lets you pass in strings representing properties/methods that you can call against an object.  You may look into that as it may prove useful in other aspects of your application.

The problem here is that the Forms collection does not behave like "normal" collections and give us access to its contents via something like an Item property/method directly.  We are forced to iterate over each item and then inspecting that item to see if it's the one we are looking for.
0
 
LVL 2

Author Comment

by:kinton
ID: 10741579
Thanks for the tip AzraSound.

I've discovered a minor problem. Looping through the forms only seems to detect the ones that are loaded!

Any ideas on how to get round this?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 10741631
Yes, the Forms collection only returns the loaded forms.  If you are saying that you need to access a form that may not yet be loaded, then you need to load that form using the Forms.Add method mentioned above.  So perhaps something like:


Dim blnFormFound As Boolean
Dim frm As Form


For Each frm In Forms
    If frm.Name = "frmAccountRecords" Then
        blnFormFound = True
        'process as needed
    End If
Next


If blnFormFound <> True Then
    'form is not yet loaded
    Set frm = Forms.Add("frmAccountRecords")
    'process as needed
End If
0
 
LVL 19

Expert Comment

by:Shauli
ID: 10741835
"Shauli: Does your code create a new form from the variable myFormName? "

Yes, it does.

S
0
 
LVL 2

Author Comment

by:kinton
ID: 11107700
Sorry for my late reply.  I eventually used AzraSounds method for referencing the object. Thanks for all of your help AzraSound and Shauli.

If anyone happens to find an equivalent for the Eval command i'd still be insterested in hearing about it!  

If you wondered what took me so long in posting back, our installshield guy left so I had to learn it and create an installer, which wouldnt be so much of a problem if the communtiy for that was as big as the VB one!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
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…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now