Page 1 of 4 1234 LastLast
Results 1 to 15 of 46
  1. #1
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664

    Restoring VBA code to a form

    I accidentally erased all of the VBA code for one of my forms.



    That is okay. I have a backup of the db with that form included.

    Now I know that I cannot just do a wholesale copy of the form's VBA code
    since many of the subprograms are associated with a button on the form.

    That cannot be ignored.

    So I just find a button on the codeless form, then go to
    design view and press f4. That will bring up the properties of the button
    and immediately tell me (when pressing the event tab) whether or not
    the button has VBA code associated with it. Then decide on action and
    get the first and last lines of VBA code for the button; then go to the backup db get the
    complete code for that button and copy and paste into the existing db right
    at the point where the two lines (first and last) are located.

    Then with a little editing and you have got it. The VBA code is associated with that button.

    That works most of the time.

    However, in the backup db on the form discussed I have noticed that the are some sub programs
    in VBA code that do not seem to obviously have a button associated with them.

    It is easy to find the VBA code associated with the button, but how do you find the button
    associated with the VBA code? That is the reverse.

    I just have some orphan sub programs whose names end in click that I assume are associated
    with a button. I just need to find that button.

    How do I do it?

    Any help appreciated. Thanks in advance.

    Respectfully,

    Lou Reed

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    the best way is to make a db copy often.
    restore the entire FE db.

    you can extract only the code to text files, but its just as easy to open an old db, get the event code,
    and paste into the broken db. Sort of a lose-lose situation.

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I just have some orphan sub programs whose names end in click that I assume are associated
    with a button. I just need to find that button.
    The first part of the subprogram name gives the name of the form control, and the second part is the name of the event. Spaces are replaced with Underscores (_).

    So, for a sub called MOS_Search_Text_AfterUpdate, this is the code for the After Update event of a control called MOS_Search_Text. (I actually used the underscores in the control name. You cannot change this Sub naming convention - it is how MS Access associates VBA code with events.

    As for "finding" the control, the only way I know of is to just look for it in form design view - I don't think Access can point it out to you.

  4. #4
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Code:
    you can extract only the code to text files, but its just as easy to open an old db, get the event code,
     and paste into the broken db.  Sort of a lose-lose situation.
    If I do that the code is not associated with a button. It must be associated with a button or other control
    for it to operate when the button is activated.

    Any help appreciated. Thanks in advance.

    Respectfully,

    Lou Reed

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    So, for a sub called MOS_Search_Text_AfterUpdate, this is the code for the After Update event of a control called MOS_Search_Text.
    this is one of the reasons for not using spaces because the sub could be associated with a control called 'MOS Search Text'

  6. #6
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    If I do that the code is not associated with a button. It must be associated with a button or other control
    for it to operate when the button is activated.
    As long as the names of the buttons on both forms are the same, it is a quick fix. Just go in to the events properties for the button control on the new form, click the ... button, and select "Code Builder". Access will link the existing code to that button.

  7. #7
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Yes it will, but would it not be easier to let MS Access 2010 put in the first and last lines of the sub then you can paste the remaining code
    and do some editing and there you have it.

    It just seems so much easier this way.

    Respectfully.

    Lou Reed

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    That works just as well, especially if control names have changed, in which case you have to be careful you don't refer to the old name somewhere.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by John_G View Post
    As for "finding" the control, the only way I know of is to just look for it in form design view - I don't think Access can point it out to you.
    There is a way actually. With a form/report in design view and the properties window open, there's a dropdown at the top of the properties window. You can select the desired control there and Access will select it in design view.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Gosh, Paul, in all my years and struggling with this issue I never even noticed that drop down! Brilliant.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Yes, it comes in handy with "busy" forms!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Thanks, Paul - I had completely overlooked that, because I almost never use that functionality.

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help John!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Code:
    There is a way actually.  With a form/report in design view and the properties window open, there's a dropdown at the top of the properties window.  You can select the desired control there and Access will select it in design view.
    Please explain on this more elaborately. I am interested in doing this.

    Respectfully,


    Lou Reed

  15. #15
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Never mind. I have the answer. Thanks anyway.

    Respectfully,

    Lou Reed

Page 1 of 4 1234 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Restoring another form
    By LonghronJ in forum Modules
    Replies: 1
    Last Post: 07-17-2015, 10:43 AM
  2. Replies: 3
    Last Post: 10-16-2014, 08:49 AM
  3. Saving/Restoring Modified Datasheet Form
    By EddieN1 in forum Programming
    Replies: 1
    Last Post: 01-20-2012, 09:48 PM
  4. Restoring a lost field with a Unique Identifier
    By DBinazeski in forum Access
    Replies: 5
    Last Post: 12-20-2010, 08:02 AM
  5. Pound signs restoring tables
    By Djacsnp in forum Access
    Replies: 2
    Last Post: 04-20-2010, 05:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums