Solved

Problem enabling button after record update Access 2007

Posted on 2014-09-17
2
387 Views
Last Modified: 2014-09-17
I have a form that uses the following code when it loads:

Private Sub Form_Current()
Me!btn_cancel_time.Enabled = False
Me!btn_nevermind_time.Enabled = True
End Sub

Open in new window


Then does this when a change is made:

Private Sub Form_Dirty(Cancel As Integer)
Me!btn_cancel_time.Enabled = True
Me!btn_nevermind_time.Visible = False
End Sub

Open in new window


This works great. Problem is I have a button for saving the record and adding another:

Private Sub btn_add_time_Click()
DoCmd.GoToRecord acDataForm, "time_entry", acNewRec
Me.tar_abs.Visible = False
Me.excused.Visible = False
Me.reason.Visible = False
Me.Label44.Visible = False
Me.Label46.Visible = False
Me.Label48.Visible = False
Me!btn_cancel_time.Enabled = False
Me!btn_nevermind_time.Enabled = True
End Sub

Open in new window


All of the .visible code is working but I cant make it enable and disable the buttons back to the way they were when the form loaded the first time.
0
Comment
Question by:princeservice
2 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 250 total points
ID: 40327760
What do you do with these controls if you are not on a new record?  I assume that those labels refer to the associated controls (cannot tell what type).

1.  First thing I would do is change the names of all those controls so that they have meaning.  Use one of the standard naming conventions so that when you look at your code, you can tell what labels are associated with which controls by their name and so that you can tell the type of control, by its name.  Example:

Text box: txt_something
Combo: cbo_something
Listbox: lst_something
Label   : lbl_something

2.  Then I would determine what you want to do with the visibilty of those controls if you are not on a new record.  

3.  Then I would move all of that code, except for the:
DoCmd.GoToRecord acDataForm, "time_entry", acNewRec

Open in new window

into the Form_Current event.  Use an if statement to determine whether the current record is new or not, and set the visibility of the controls based on that, something like:
Private Sub Form_Current

    if me.newrecord then
        Me.tar_abs.Visible = False
        Me.excused.Visible = False
        Me.reason.Visible = False
        Me.Label44.Visible = False
        Me.Label46.Visible = False
        Me.Label48.Visible = False
    Else
       'something else here
    End If

    Me!btn_cancel_time.Enabled = False
    Me!btn_nevermind_time.Enabled = True

End Sub

Open in new window

0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 250 total points
ID: 40327984
To finish Dale's thought - then to toggle the buttons after an update, use the Form's AfterUpdate event and call the Current event so you don't need to duplicate the code.

So you'll end up with code in
Current
Dirty
AfterUpdate (call Current)

None of the toggle code should go into your own button events.

The other reason for properly naming controls is so your code will be clear whether you are referring to a CONTROL or a field in the recordsource.  When you are setting a property, you are referring to the control.

And finally, using the dot syntax will give you intellisense which you don't get with the bang.
So use Me.btn_cancel_time.Enabled = True
 rather than Me!btn_cancel_time.Enabled = True
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now