一遍又一遍地从用户表单打开用户表单 - 导致对象问题

发布于 2024-10-06 10:32:23 字数 979 浏览 6 评论 0原文

我目前在 Excel 2003 中构建了一个工具,它显示一系列数据输入表单。客户要求表单上有“上一个表单”和“下一个表单”按钮。

用于在表单之间移动的代码如下

Sub NextForm(strFormName As String)
    Dim intCurPos As Integer
    Dim strNewForm As String

    'Find out which form we are currently on from a list in a range
    intCurPos = WorksheetFunction.Match(strFormName, Range("SYS.formlist"), 0)
    If intCurPos = WorksheetFunction.CountA(Range("SYS.formlist")) Then
        'We want to use the first one
        intCurPos = 0
    End If

    'Get the name of the form to open
    strNewForm = WorksheetFunction.Index(Range("SYS.formlist"), intCurPos + 1)
    'Load the form into the Userforms Collection
    Set newForm = VBA.UserForms.Add(strNewForm)
    'Show the form
    newForm.Show
End Sub

我遇到的问题是,在执行此操作 25 次(是的,我知道)后,系统崩溃。我意识到这是因为每次到达上面的 newForm.Show 行时,代码都无法完成,因此位于内存中。

无模式表单可以阻止此问题,但用户可以加载其他表单并执行其他可能导致重大问题的操作。

有人有任何建议可以帮助解决这个问题吗?以某种方式强制执行代码但不停止表单的模式功能?

可能性不大,但感谢任何帮助。

I currently have built a tool in Excel 2003 which displays a series of data entry forms. The client has requested that there be "Previous Form" and "Next Form" buttons on the forms.

The code used to move between the forms is as follows

Sub NextForm(strFormName As String)
    Dim intCurPos As Integer
    Dim strNewForm As String

    'Find out which form we are currently on from a list in a range
    intCurPos = WorksheetFunction.Match(strFormName, Range("SYS.formlist"), 0)
    If intCurPos = WorksheetFunction.CountA(Range("SYS.formlist")) Then
        'We want to use the first one
        intCurPos = 0
    End If

    'Get the name of the form to open
    strNewForm = WorksheetFunction.Index(Range("SYS.formlist"), intCurPos + 1)
    'Load the form into the Userforms Collection
    Set newForm = VBA.UserForms.Add(strNewForm)
    'Show the form
    newForm.Show
End Sub

The issue I have is that after you do this 25 times (yeah I know) the system crashes. I realise that this is because everytime you get to the newForm.Show line above the code doesn't get to complete and so sits in memory.

Modeless forms would stop this issue but then the user could load other forms and do other things which would cause major issues.

Does anyone have any suggestions to help with this? Somehow to force code execution but not stop the modal ability of the form?

Long shot but appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2024-10-13 10:32:23

也许你应该改变你的方法。

我建议如下:

在主模块中使用循环来循环打开表单,并在用户每次按下“下一个表单”按钮时循环。

'This sub in your main Module
sub ShowAndCyleForms
   Dim FormName As String
   Dim MyForm as Object
   Dim CloseForm as Boolean

   FormName = "frmMyForm"         

   do while CloseForm=False
      set MyForm = VBA.UserForms.Add(FormName)           
      MyForm.Show
      CloseForm=MyForm.CloseStatus
      FormName=MyForm.strNewForm
      Unload MyForm
      Set MyForm = Nothing
   loop 

end sub

在每个表单中,声明:

Public CloseStatus as Boolean
Public strNewForm as String

在每个“下一个表单”按钮中,输入类似以下内容:

Private Sub btnNextForm_Click()
   CloseStatus=False
   strNewForm= NextForm(Me.Name)
   Me.Hide
End Sub

将您的子程序修改为提供下一个表单名称的函数

Sub NextForm(strFormName As String)
   Dim intCurPos As Integer

   'Find out which form we are currently on from a list in a range
   intCurPos = WorksheetFunction.Match(strFormName, Range("SYS.formlist"), 0)
   If intCurPos = WorksheetFunction.CountA(Range("SYS.formlist")) Then
       'We want to use the first one
       intCurPos = 0
   End If

   'Get the name of the form to open
   NewForm = WorksheetFunction.Index(Range("SYS.formlist"), intCurPos + 1)
   '
End Sub

您还需要修改您的 OK 以隐藏表单,而不是卸载它并设置CloseStatus 为 true。

这个想法是在一个过程中控制所有表单从外部加载/卸载。

希望它足够清楚。

Maybe you should change your approach.

I would suggest the following:

In the main module use a loop to cycle open the form and loop every time the user press the "Next Form" button.

'This sub in your main Module
sub ShowAndCyleForms
   Dim FormName As String
   Dim MyForm as Object
   Dim CloseForm as Boolean

   FormName = "frmMyForm"         

   do while CloseForm=False
      set MyForm = VBA.UserForms.Add(FormName)           
      MyForm.Show
      CloseForm=MyForm.CloseStatus
      FormName=MyForm.strNewForm
      Unload MyForm
      Set MyForm = Nothing
   loop 

end sub

In every form, declare:

Public CloseStatus as Boolean
Public strNewForm as String

In every "Next Form" button, put something like:

Private Sub btnNextForm_Click()
   CloseStatus=False
   strNewForm= NextForm(Me.Name)
   Me.Hide
End Sub

Modify your sub to be a function that delievers the next Form Name

Sub NextForm(strFormName As String)
   Dim intCurPos As Integer

   'Find out which form we are currently on from a list in a range
   intCurPos = WorksheetFunction.Match(strFormName, Range("SYS.formlist"), 0)
   If intCurPos = WorksheetFunction.CountA(Range("SYS.formlist")) Then
       'We want to use the first one
       intCurPos = 0
   End If

   'Get the name of the form to open
   NewForm = WorksheetFunction.Index(Range("SYS.formlist"), intCurPos + 1)
   '
End Sub

You will also need to modify your O.K. to just hide the form instead of unloading it and setting the CloseStatus to true.

The idea is to control all your forms loading/unloading from outside the from in a single procedure.

Hope it is clear enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文