Results 1 to 9 of 9

Thread: Can I call a method of an object in runtime by "object name" string?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Can I call a method of an object in runtime by "object name" string?

    Private Sub FormShow(FormName as string)

    dim f as Form

    f = FormName //?????

    load f
    f.show 1

    End Sub

    Can I reference to an object by help of its name (string) in runtime?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Can I call a method of an object in runtime by "object name" string?

    Quote Originally Posted by ultra2
    Private Sub FormShow(FormName as string)

    dim f as Form

    f = FormName //?????

    load f
    f.show 1

    End Sub

    Can I reference to an object by help of its name (string) in runtime?
    In order to reference an object...
    VB Code:
    1. Dim f As Form
    2. Set f = Form2
    3. f.Show

  3. #3
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Can I call a method of an object in runtime by "object name" string?

    Why not just do

    Private Sub FormShow(FormName as Form)
    FormName.Show
    End Sub

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Can I call a method of an object in runtime by "object name" string?

    For that matter, why the sub at all. Form1.Show is less typing than creating the sub and typing FormShow Form1

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Re: Can I call a method of an object in runtime by "object name" string?

    The form name is a string and I get it from a database field IN RUN TIME.

    It could work, but I have 200 form in my app so I need a more generally solution than this:

    Private Sub FormShow(FormName as string)
    if From1.name = FormName then From1.show
    if From2.name = FormName then From2.show
    if From3.name = FormName then From3.show
    ...
    End Sub

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Can I call a method of an object in runtime by "object name" string?

    Why would you need 200 forms? if you dont mind me asking.
    It seems a little too much.

  7. #7
    Junior Member
    Join Date
    Jul 2005
    Posts
    26

    Re: Can I call a method of an object in runtime by "object name" string?

    I don't know of any way to open a form in the way you want. If you want to open a form, use a select case statment:
    VB Code:
    1. Select Case FormName
    2.      Case "Form1"
    3.           Form1.show
    4.      Case "Form2"
    5. ...
    6. End Select
    This may not be easy, but I know of no other way (and it is easier than 200 if ... then statments)

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Re: Can I call a method of an object in runtime by "object name" string?

    I also could use the "Forms" array, but it only consist of the loaded forms and I dont want to load all the forms of my App.

    Any other way to loop through all the forms of the app?

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Can I call a method of an object in runtime by "object name" string?

    No, sorry. Only way apart from the Forms collection is to use a Select Case as Mikewitt posted.

    VB Code:
    1. Dim frm As Form
    2. For Each frm In Forms
    3.     If frm.Name = strName Then frm.Show
    4. Next frm

Posting Permissions

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



Click Here to Expand Forum to Full Width