سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top