Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 427
  • Last Modified:

Converting a string to a variable.

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
kinton
Asked:
kinton
  • 5
  • 4
  • 2
  • +1
1 Solution
 
dancebertCommented:
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
 
AzraSoundCommented:
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
 
ShauliCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kintonAuthor Commented:
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
 
AzraSoundCommented:
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
 
AzraSoundCommented:
BTW, I think you will find any hit on performance would be negligible
0
 
kintonAuthor Commented:
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
 
AzraSoundCommented:
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
 
kintonAuthor Commented:
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
 
AzraSoundCommented:
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
 
ShauliCommented:
"Shauli: Does your code create a new form from the variable myFormName? "

Yes, it does.

S
0
 
kintonAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now