Solved

Problem enabling button after record update Access 2007

Posted on 2014-09-17
2
389 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 35

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

825 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