Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Posts
    30

    match string name to form name in vba

    I have a form that updates with a list box. A button on the form opens a chart. The code I have works great and both forms update when the selection changes in the list box.
    What I would like to do is have it work for any form I specify in a variable
    without having to hard code the form name in the requery statment.
    All tries at variable substitution fail. Of course a string doesn't work when a
    form is required. Is there some way around this? Is there a way to get the form name to change with the string name.

    Public Function move_up(ByVal frmvar As Form)

    Dim varnames As String
    varnames = "frmchtfld"

    Dim rs As Object
    If CurrentProject.AllForms(varnames).IsLoaded Then
    Set rs = frmvar.Recordset.Clone
    rs.FindFirst "[well name] = '" & frmvar![wells] & "'"
    If Not rs.EOF Then frmvar.Bookmark = rs.Bookmark
    Forms!frmchtfld.Requery

    Else
    Set rs = frmvar.Recordset.Clone
    rs.FindFirst "[Well name] = '" & frmvar![wells] & "'"
    If Not rs.EOF Then frmvar.Bookmark = rs.Bookmark


    End If
    End Function

    Any help would be appreciated.

  2. #2
    Join Date
    Mar 2009
    Posts
    5,251
    You could try something like this:
    Code:
        Dim strFName As String
        
        strFName = "F_Billing_Customers_List"
        Eval "Forms!" & strFName & ".Requery"
    Have a nice day!

  3. #3
    Join Date
    Oct 2004
    Location
    Oxfordshire, UK
    Posts
    89
    You can use a string variable in place of hard coded names. You've just got to lose the bang operator. Let's say you want to reference a control called "x" on a form named "y" then you can do this:

    Forms("x").Controls("y").Value = "z" In fact default class properties mean that you can shorten this to:
    Forms("x")("y") = "z"

    All of the quoted characters could be replaced with variables.

    In the same way you might pass a variable a form sub procedure like this
    x = "MyForm"
    y = 1
    Call Forms(x).MySub(y)

  4. #4
    Join Date
    Jul 2005
    Posts
    30
    Sinndho:

    Code works great with eval function.
    Eval "Forms!" & strVariable & ".Requery"

    Thanks
    Darejamr

Posting Permissions

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