Consulting

Results 1 to 8 of 8

Thread: Prevent form from closing after programmatically adding code

  1. #1

    Prevent form from closing after programmatically adding code

    I'm generating a module programmatically when a command button is clicked on a modeless form. At the end of the code the form closes automatically without me explicitly unloading or hiding it. The form will stay open if I make it modal, but then the next modeless form that I open will do the same thing. I'm guessing this is happening to bring focus onto the new code in the VBA editor window at the first opportunity, but I want my program to keep running instead! Any ideas on how to get the form to stay open?

    I'm using Excel 2010 Pro, VBA 7.0

    Private Sub cmdGenerateModule_Click() 
        Dim mdlUserDefined As Object 
        Set mdlUserDefined = ActiveWorkbook.VBProject.VBComponents.Add(1) 
        mdlUserDefined.CodeModule.AddFromString "Public Sub Test_Code()" & Chr(13) & "End Sub" 
         
        UserForm1.Show ' this doesn't help
        Debug.Print "debug test" ' this line prints in the debugger, so the code executes before closing the form
    End Sub

  2. #2
    It seems that this is happening because the VBE will reset every time code is changed during run-time. As a workaround, I'm going to generate and use the new procedure in a temporary workbook. The downside is that the procedure doesn't have access to the code in the main workbook, but at least the program is able to keep running.

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Why do you need create a module in the same workbook?

    Better question: What exactly are you trying to accomplish that needs a new module?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    The idea is to allow the user to type in some code that will be used in the program, without disrupting the flow of the application. My workaround appears to do the trick, although it is a little more roundabout than I initially thought it would be.

  5. #5
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    I don't think it's ever a good idea to use code to write code into the same workbook but you could use an Application.OnTime call to reload the form if necessary. That would allow the code to finish, the project to reset and then the form to reappear. It would however re-run any initialize event code unless you use a flag variable to prevent it.
    Be as you wish to seem

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Design a hardcoded Modeless UserForm to get the info.
    Show it with the get info button on the first form.
    With its OK button, save the info and close the form
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Quote Originally Posted by Aflatoon View Post
    I don't think it's ever a good idea to use code to write code into the same workbook but you could use an Application.OnTime call to reload the form if necessary. That would allow the code to finish, the project to reset and then the form to reappear. It would however re-run any initialize event code unless you use a flag variable to prevent it.
    Great suggestion. This works.


    Quote Originally Posted by SamT
    Design a hardcoded Modeless UserForm to get the info.
    Show it with the get info button on the first form.
    With its OK button, save the info and close the form.
    This was my approach initially. It works so long as you are going from modal form to modal form. However, as soon as you open up another modeless form, the program will stop.

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    How many Forms are you showing at one time?

    I tested it with two "ShowModal = False" forms and it worked ok for me.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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