Solved

Problem enabling button after record update Access 2007

Posted on 2014-09-17
2
386 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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

744 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

18 Experts available now in Live!

Get 1:1 Help Now