Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2002
    Location
    Norway
    Posts
    239

    Question Unanswered: Convert a string to a form reference

    The following function as expected:
    Dim MyForm As Form
    Set MyForm = Forms![Main]![frm-Reports].Form


    But I like to create the reference from a string, like:

    Dim MyString
    MyString = “Forms! & “[Main]” & “!” & “[frm-Reports]” & “.Form”
    ........ and then use:
    Dim MyForm As Form
    Set MyForm = MyString

    But, it fails. ….. Is it at all possible? -can anyone please help me?

    Thanks

    Sincerely
    Vestigo

  2. #2
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    You can reference forms by their name. like:
    Code:
    Forms("FormName").Caption="Change caption to this"
    ghozy.

  3. #3
    Join Date
    Mar 2002
    Location
    Bielefeld, Germany
    Posts
    69
    If you have the names of the forms as strings, you can do something along the lines of:

    set myForm = Forms([Main])([frm-reports]).Form

    i.e. referencing the items of the form collections by key. The resulting code is more or less unreadable, especially if you have serveral levels of subforms, but it should work.

  4. #4
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    Try the Eval() function:

    Set MyForm = Eval(MyString)

  5. #5
    Join Date
    Nov 2002
    Location
    Norway
    Posts
    239

    Unhappy No Success

    Hi.
    Can’t get any of your proposals to function.
    Has anyone tested their proposal with success?
    What am I doing wrong?

    Sincerely
    Vestigo

  6. #6
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    I tried the Eval suggestion and it was able to evaluate the expression but it could find the form because I didn't have a form with that name.

    One thing about the suggestion, since you are using the Forms collection the form needs to be open.

    Maybe if you described what you are trying to do then we could give better assitance. Why do you want to use the reference? How will you use it?

  7. #7
    Join Date
    Nov 2002
    Location
    Norway
    Posts
    239

    Post

    OK, let me try to explain.

    My objective is to refer to controls in sub forms and subsub forms from a modul.
    To do this I need to have the complete reference to the form. (yes it’s open)
    like “Forms![Main]![Subform1].Form![Subform2].Form!ControlName”

    The above string will be send to the module as a parameter while opening the actual form. The string will be build up of Me.Name, Me.Parent.Name and Me.Parent.Parent.Name

    Is this understandable for others than me?
    Thanks for your patient.

    Sincerely
    Vestigo
    Last edited by vestigo; 08-05-04 at 05:59.

  8. #8
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    instead of trying to create full string for reference a control, you could pass form and control name to module and reference to control. Follwing function gets a form and control name and returns a reference for that control.

    Code:
    'Usage:
    ' GetControl("FormName","ControlName")
    ' GetControl("FormName","ControlName","SubformName")
    ' GetControl("FormName","ControlName","SubformName","SubSubformName")
    'Example to read CustomerName textbox value from Form Serials:
    ' debug.print GetControl("Serials","CustomerName").Value
    Public Function GetControl(AForm As String, AControl As String, Optional ASubform As String = "", Optional ASubSubform As String = "") As Variant
    Dim format As Integer
    	format = 0
    	If Not ASubSubform = "" Then format = format + 2
    	If Not ASubform = "" Then format = format + 1
    	Select Case format
    		Case 0:
    			Set GetControl = Forms(AForm).Controls(AControl) 'return Form!control
    		Case 1:
    			Set GetControl = Forms(AForm).Controls(ASubform).Controls(AControl) 'return form!subform!control
    		Case 3:
    			Set GetControl = Forms(AForm).Controls(ASubform).Controls(ASubSubform).Controls(AControl) 'return form!subform!subsubform!control
    		Case Else:
    			Set GetControl = Null 'wrong number of parameters return null
    	End Select
    End Function
    ghozy.

  9. #9
    Join Date
    Nov 2002
    Location
    Norway
    Posts
    239

    Talking Success !

    Brilliant ghozy !!!!!!!
    This code did the job with flying colours.
    Thanks a lot
    Now I can get some sleep again :-)


    Sincerely
    Vestigo

  10. #10
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    I'm glad I could help.
    ghozy.

Posting Permissions

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