Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Eval command in VB (VB 6)

    After some research, my findings seem to indicate that the Eval command from VBA doesn't work in VB.

    Would anyone know how to use a "created" object reference in a command.

    i.e.
    dim str as string, arrayname as string, x as long
    x = 0
    arrayname = "TestControl"
    str = "frmAccountRecords." & arrayname & "(" & x & ").text"
    Eval(str) = "tested" & x

    This would have the same result as running frmaccountrecords.testcontrol(0).text = "tested0"

    The plan is to pass a controls name to a function, and use that function to populate the control.

    Is there an alternative to the Eval command, or another method I can use? I've read a few things on Microsoft Script Control, but I haven't found an example or worked out how to do it myself yet.

    Any help would be much appreciated.

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Eval command in VB (VB 6)

    Does VB6 have the CallByName function, just like VBA(6) has?

    From Excel XP Help:

    CallByName Function


    Executes a method of an object, or sets or returns a property of an object.

    Syntax

    CallByName(object, procname, calltype,[args()])

    The CallByName function syntax has these named arguments:

    Part Description
    object Required; Variant (Object). The name of the object on which the function will be executed.
    procname Required; Variant (String). A string expression containing the name of a property or method of the object.
    calltype Required; Constant. A constant of type vbCallType representing the type of procedure being called.
    args() Optional: Variant (Array).



    Remarks

    The CallByName function is used to get or set a property, or invoke a method at run time using a string name.

    In the following example, the first line uses CallByName to set the MousePointer property of a text box, the second line gets the value of the MousePointer property, and the third line invokes the Move method to move the text box:

    CallByName Text1, "MousePointer", vbLet, vbCrosshair
    Result = CallByName (Text1, "MousePointer", vbGet)
    CallByName Text1, "Move", vbMethod, 100, 100
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Eval command in VB (VB 6)

    Yes, CallByName is in VB6 too (VBA in Office 2000 and up is based on VB6)

  4. #4
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Eval command in VB (VB 6)

    Thanks for your reply Jan,
    VB6 does have the callbyname function, but so far i've been unable to get it to work.

    I've been unable to set a control's .text or .caption value with it, and I can't seem to set its index if im passing an array to it.

    If it helps at all, I have a form with a textbox array called Taddress (with 7 textboxes on the form)

    I plan to make a function that will accept any textbox array, open a recordset and populate the next empty array with each field.

    i.e. With Establishment, address 1, add2, add3, town, county, postcode:
    Establishment: "Tower Building" will populate Taddress(0)
    Address1: "4 Short Road" will populate Taddress(1)
    Address2 and 3 are null
    Town: "Gillingham" will populate Taddress(2)
    County is null
    Postcode: "ME3 4DT" will populate TAddress(3)

    The plan was to follow this design(not neccesarily correct code):
    x = 0
    For each fld in rec.fields
    if not isnull(fld) then
    eval(textboxarray(x).text) = fld
    x = x+1
    end if
    next fld

    If I manage to set a texbox using callbyname, ill post again. If anyone manages to find an example of setting a textbox's value (specifically an array) please let me know.
    Thanks for pointing me in the direction of that command Jan.

    Regards,
    Phil

  5. #5
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Eval command in VB (VB 6)

    My immediate panic is over as I solved the problem by looping through each control and checking the control.index value instead of passing the exact control & index to my module.

    It solved the overall problem without finding the method for the question I posted, so I feel a bit cheap, but i'll just have to live with it for now as I have a deadline. If I have time after the deadline I will return to my original question and solve it.

    Thanks for the tip anyway Jan, I will look into it when I have a minute spare.

    Regards,

  6. #6
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Eval command in VB (VB 6)

    I've finally had a chance to have a more in depth look into this.
    Using the example provided - CallByName Text1, "Move", vbMethod, 100, 100 - it would be the Text1 control that I want to pass as a string, rather that the event/property on the specified control.

    Can I assume there isn't a similar callbyname function for the actual control itself i.e.
    dim ObjectVariable as object
    set ObjectVariable = ControlByName("frmLastSearch.Text1")
    The result would leave me an object variable populated with the control that was specified in the string.

    I've only ever seen examples using a loop of every control on a form a successful match with the string ends the loop with that control selected, so i'm guessing it isn't possible.
    i.e.
    For Each objPrinter In Printers
    If objPrinter.DeviceName = DefaultPrinterString Then Exit For
    Next
    'This leaves objPrinter set as the object specified in DefaultPrinterString

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Eval command in VB (VB 6)

    For a control on a form, you can use

    Dim ctl As Control
    Dim strFormName As String
    Dim strControlName As String
    strFormName = ...
    strControlName = ...
    Set ctl = Forms(strFormName).Controls(strControlName)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •