How to create a new form instance using the name of the form as a String

后端 未结 3 442
遥遥无期
遥遥无期 2021-01-24 23:46

Code to create new form instance of a closed form using form name

I want to replace the long Select Case list with a variable.

Full code of modu

相关标签:
3条回答
  • 2021-01-25 00:03

    I've also done some testing of my own and some reading online about this. As near as I can tell, it isn't possible to create a new form object and set it to an instance of an existing form using a string that represents the name of that form without using DoCmd.OpenForm.

    In other words, unless someone else can prove me wrong, what you are trying to do cannot be done.

    0 讨论(0)
  • 2021-01-25 00:12

    Here's an ugly hack I found:

    DoCmd.SelectObject <acObjectType>, <YourObjectsName>, True
    DoCmd.RunCommand acCmdNewObjectForm
    

    The RunCommand step doesn't give you programmatic control of the object, you'll have to Dim a Form variable and Set using Forms.Item(). I usually close the form after DoCmd.RunCommand, then DoCmd.Rename with something useful (my users don't like Form1, Form2, etc.).

    Hope that helps.

    0 讨论(0)
  • 2021-01-25 00:15

    I think you are looking for something like this MS-Access 2010 function. (The GetForm sub is just for testing):

    Function SelectForm(ByVal FormName As String, ByRef FormExists As Boolean) As Form
        For Each f In Application.Forms
          If f.Name = FormName Then
            Set SelectForm = f
            FormExists = True
            Exit Function
          End If
        Next
        FormExists = False
    End Function
    
    Sub GetForm(ByVal FormName As String)
    
      Dim f As New Form
      Dim FormExists As Boolean
      Set f = SelectForm(FormName, FormExists)
      If FormExists Then
        MsgBox ("Form Found: " & f.Caption) 
      Else
        MsgBox ("Form '" & FormName & "' not found.")
      End If
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题