Solved

Converting a string to a variable.

Posted on 2004-04-02
12
417 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 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…
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now