+ Reply to Thread
Results 1 to 10 of 10

Form nightmare - project resets = global variables lost

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Form nightmare - project resets = global variables lost

    And this was all working so well up to now

    I am working on a project which requires extensive user input by forms. I've recently broken the code and I can't work out why (I have backups but I don't want to lose my recent changes).

    Any one have any ideas to help solve this mystery?


    One of the main forms in the project is called frmMenu2. It is explicitly declared in a calling sub and shown modeless. Calling sub code below:

    Please Login or Register  to view this content.
    One of the events on frm_Menu2 is cmdEdit_Click. It's code looks something like this:
    Please Login or Register  to view this content.
    Load_frm_Edit shows a modal form (explicitly declared as new instance) to allow the user to input edits.
    Depending on the users input, this sub then unloads frm_Edit and then either 1. loads Menu2 (by calling Load_frm_Menu2 again) or 2. wipes all variables and loads Menu1.

    However something has gone wrong. After a successful entry, Menu2 is loaded and but I then receive a project reset message (and lose all global variables - thus breaking the project).

    I have stepped through the code and the problem is occurring on the
    Please Login or Register  to view this content.
    line of sub cmdEdit_Click. But that instance of Menu2 should be unloaded and gone, no? And assuming that the original Menu2 form is still in memory, why would it crash here?

    Any ideas?
    Last edited by mc84excel; 02-18-2014 at 12:47 AM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Form nightmare - project resets = global variables lost

    I need help. So to make it easier for anybody offering to debug this, I have created a dummy workbook which replicates the issue. Step through after the code reaches the
    Please Login or Register  to view this content.
    to see what I mean.
    Attached Files Attached Files

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Form nightmare - project resets = global variables lost

    Hello mc84excel,

    That is some crazy code. This code should be scrapped and redone.

    I am not sure what you are trying to achieve but I can tell you this is not the way to do it.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Form nightmare - project resets = global variables lost

    Quote Originally Posted by Leith Ross View Post
    Hello mc84excel,

    That is some crazy code. This code should be scrapped and redone.

    I am not sure what you are trying to achieve but I can tell you this is not the way to do it.
    I agree that the code needs improving but I'm not so sure about scrap & redo. I don't suppose you could provide specifics as to what you consider "crazy" about it?

    Please take into account that the code I've provided in this thread is only a "dumbed-down" portion of only a small fraction of the overall project code. So of course it will look unusual since it's taken out of context and missing all practical functionality and form features/design.'

    I can't provide the entire code unfortunately so if you wish to help, please use the mock-up I uploaded - this is sufficient to recreate the issue I'm experiencing.
    Last edited by mc84excel; 02-19-2014 at 08:33 PM.

  5. #5
    Registered User
    Join Date
    03-01-2013
    Location
    Garden Grove, California
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Form nightmare - project resets = global variables lost

    Well I'm no expert but it looks as though you might have some syntax errors in your DoWhile Statement. Shouldn't ".Show vbModal" be vbModal.Show? (Object.Method) And there is a dot separator on the next line that doesn't separate anything. This would probably screw something up.

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Form nightmare - project resets = global variables lost

    Quote Originally Posted by kierstin View Post
    Well I'm no expert but it looks as though you might have some syntax errors in your DoWhile Statement.
    Hi kierstin. I'm no expert either so I appreciate any suggestions to what is causing this.

    Quote Originally Posted by kierstin View Post
    Shouldn't ".Show vbModal" be vbModal.Show? (Object.Method)
    Please Login or Register  to view this content.
    has the options of
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Leaving the attribute blank causes it to default to vbModal. I removed it anyway to see if it helped but I still receive the project reset.

    Quote Originally Posted by kierstin View Post
    And there is a dot separator on the next line that doesn't separate anything.
    You mean the
    Please Login or Register  to view this content.
    ? That's a property of the edit form.


    UPDATE
    kierstin - I think you are onto something with the Do While Loop. I commented this out (see below) and ran the test. And the project wasn't reset!

    Please Login or Register  to view this content.
    Last edited by mc84excel; 02-19-2014 at 08:45 PM.

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Form nightmare - project resets = global variables lost

    +1 for kierstin for giving me the clue to solving this.

    The Do While loop to handle the edit form button response was causing the project reset on Menu2.

    I recently implemented this Loop as part of my goal of moving towards better coding practices (In this case: Avoiding all use of GoTo except on labels for error handling and exit procedures).

    Below is my fix to the mockup code. By reverting to using GoTo again.
    (Sorry GoTo haters. This was the only way to avoid this issue. I'll rep you if you can prove me wrong using the mock-up provided...)
    Please Login or Register  to view this content.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Form nightmare - project resets = global variables lost

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Form nightmare - project resets = global variables lost

    +1 to shg

    If you know why the Do Loop worked but the Do While triggered a project reset, I'd be interested to learn.


    P.S. re the 2x Resume. It's something I stumbled across while studying up on error handling.

    'Note that second Resume. This is a trick I learned recently:
    'It will never execute in normal processing, since the Resume <label> statement will send the execution elsewhere.
    'It can be a boon for debugging, though.
    'When you get an error notification, choose Debug (or press Ctl-Break, then choose Debug when you get the "Execution was interrupted" message).
    'The next (highlighted) statement will be either the MsgBox or the following statement.
    'Use "Set Next Statement" (Ctl-F9) to highlight the bare Resume, then press F8.
    'This will show you exactly where the error was thrown.

    (The mockup code is lacking a debug.assert false or msgbox as a line before the first resume - which this trick needs for it to work).
    Last edited by mc84excel; 02-19-2014 at 09:30 PM.

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Form nightmare - project resets = global variables lost

    I recently had this problem again on another form. I googled and found a way to work round it
    HTML Code: 
    Using Me.Hide wasn't a drawback for me as the form I wanted to return to didn't need anything reset in between switching forms.

    No idea why the Unload Me sometimes triggers project resets.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Global Variables
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-07-2006, 05:55 AM
  2. keep the global variables
    By Mike Archer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-03-2006, 02:40 PM
  3. [SOLVED] global Variables
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2005, 03:10 PM
  4. global variables
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-16-2005, 07:50 PM
  5. Global VBA Project?
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-04-2005, 01:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1