Cmd Btn to Cancel and Close (1 Viewer)

ANE002

New member
Local time
Today, 06:19
Joined
Aug 24, 2022
Messages
11
Hello - I am VERY new to Access, as in day 3 self learning. I am currently modifying the template for Call Tracking. The layout is very very similar to our current needs. On one of the forms i updated several fields to be required. There is already a "Close" button but with the required fields being blank i just get an error and have to go to the table to delete the record.

I need to make a "Cancel" button that will clear the form, close the form, and NOT make a new record. I already have a cmd btn created that says Cancel. Just need to know how to modify it.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:19
Joined
Oct 29, 2018
Messages
21,542
Hi. Welcome to AWF!

Try doing a search on "data validation." Basically, you would use the Form's BeforeUpdate event to cancel the record if the data entered is not valid.
 

ANE002

New member
Local time
Today, 06:19
Joined
Aug 24, 2022
Messages
11
Hi. Welcome to AWF!

Try doing a search on "data validation." Basically, you would use the Form's BeforeUpdate event to cancel the record if the data entered is not valid.
Hhmmm. ok. i dont have a data validation option in any of the tool bars and if i type it in the search bar at the very top of the window nothing returns.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:19
Joined
Oct 29, 2018
Messages
21,542
Hhmmm. ok. i dont have a data validation option in any of the tool bars and if i type it in the search bar at the very top of the window nothing returns.
Data validation would be done using some code. You'll have to specify which fields you want to validate. Try to do a search in this forum for some discussions on "data validation." I'll post a link if I find a good example.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:19
Joined
Oct 29, 2018
Messages
21,542
This should be a good place to start.
 

ANE002

New member
Local time
Today, 06:19
Joined
Aug 24, 2022
Messages
11
This should be a good place to start.
Thanks for that. I think it will be more helpful later as i progress the skills. however, right now the whole video completely went over my head. I did a little more research on data validation in Access but im not sure how it connects to what I am trying to do. It seems like it would be a fairly simple task. Just a simple button that cancels the form and closes without creating a new record.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:19
Joined
Oct 29, 2018
Messages
21,542
It seems like it would be a fairly simple task. Just a simple button that cancels the form and closes without creating a new record.
That's true, but what might also happen is users forget to click on your button and you end up getting bad data in the database.

Here's a sample code for your button.
Code:
Me.Undo
DoCmd.Close acForm, Me.Name
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:19
Joined
Feb 28, 2001
Messages
27,320
Here is the low-down and at a very low level.

Access wants to save data on forms that are bound to tables or queries. That is one of the purposes of forms. Any action that would move the focus of operation to a different form (including a sub-form of the main form) will trigger Access to save what it has. Closing a form that has some data also triggers a save operation. It is designed that way because it doesn't want to lose data.

When you have a form with some but not all of the required data, you have a couple of options. The most common one we recommend has to do with event programming. You have admitted you are very new to Access. So... an event - to Access - is a situation that is relevant to the things Access does. Like the Form_Open event, which "fires" when you open a form. Or the Form_Load event, when the form starts loading data. Or the Form_Current event, when a form bound to a record loads all of the data from that record into the controls on the form. That is to say, the form and record currently match.

In each of these events, Access allows you to drop a bit of code in VBA "behind" the event. In form design mode, open the form properties display, which has four tabs, and open the EVENTS tab. The idea is that Access performs all of the events you see. Whether or not you drop in some code, those events WILL happen. BUT if you follow the procedures to do so, you can add a bit of your own code such that when that particular event "fires" then Access will call your event subroutine that is specific to that event and that form.

The event you want is the Form_BeforeUpdate event. I mentioned that Access will save data automatically. It will also do so if you have a command button to implement the acSaveRecord operation. Any of the automatic or manual cases, what is ABOUT to happen is that Access will write back the data on the form to make the corresponding record match the form. However, that particular event has the option that you can cancel it. Here is a link to the event and it has some code samples.


The idea is that if you write a Form_BeforeUpdate event, you will have a chance to examine the controls on your form to see if there is something wrong. If not, you just let things happen. But if there is an error, you can cancel the event, which will cancel the update. The form will not do whatever it was about to do. For example, you can't close a form that needs saving if your code cancels the update. You can't navigate away from the current record if your code cancels the update.

There are other problems here as well. If you try to close the form and it is "dirty" (data needs saving), the question is how you would know that the attempt was being made to close the form rather than navigate it or simply leave the form for a sub-form or another form. This becomes extremely complex. I can't advise you farther than this because you have to decide what way you want to handle the problems in a procedural sense. That is I can't give you code samples because at the user level, you have to decide what should be done. Do you want to Undo all changes? Do you want to block closure? Do you have some other action you want to take, not at a code level but at an operations level? That is why I can't tell you what to do.
 

ANE002

New member
Local time
Today, 06:19
Joined
Aug 24, 2022
Messages
11
Thank you everyone for the helpful feed back. I took some time to research a bit more into Access as a whole starting with small tasks and trying to learn the verbiage a bit more. I am pretty proficient with Excel but Access is a whole new world and will take a bit more time to learn than i thought as it seems pretty much everything is customizable.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:19
Joined
Oct 29, 2018
Messages
21,542
Thank you everyone for the helpful feed back. I took some time to research a bit more into Access as a whole starting with small tasks and trying to learn the verbiage a bit more. I am pretty proficient with Excel but Access is a whole new world and will take a bit more time to learn than i thought as it seems pretty much everything is customizable.
Good luck. Come back if you get stuck.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:19
Joined
Feb 19, 2002
Messages
43,484
If you search for BeforeUpdate and my name in the forum here, you will find dozens of posts with explanations and sample code.

The validation code does NOT belong in the Close button's events. The close button event should save the record and close the form.

The validation code goes into the Form's BeforeUpdate event because that event runs when you tell Access to save the record, but more importantly, it runs when Access thinks it needs to save the record even though you didn't specifically tell it to. So, it also runs when you close the form if the record is dirty or if you scroll from one record to another or if you have master/child forms when you click from the master to the child, the master record is saved. When you click from the child to the master, the child record is saved, etc. If you do not use the correct event - the Form's BeforeUpdate event, you will need code in multiple events because you have to somehow catch all the situations were Access saves of its own accord. So, the choice comes down to validation code in ONE event or duplicate code in multiple events with still the possibility that you missed something.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:19
Joined
Feb 19, 2002
Messages
43,484
Here's a link to a video about the BeforeUpdate event.

 

Users who are viewing this thread

Top Bottom