I already include in my form the command button NEW RECORD and i want to be able to add a new record only when i press this button and not through my mouse
15 2213 NeoPa 32,556
Expert Mod 16PB
The code to move the current record to the New Record is : - Call DoCmd.GoToRecord(Record:=acNewRec)
To make it so that your New Record button is the only way to go to the new record, you need to make your form's Allow Additions property to false. Unfortunately, this stops NeoPa's code from running, so you need to add the following right before NeoPa's code:
You would then need to reset this property to false in the form's AfterUpdate event: - Me.AllowAdditions = False
I have tested this out on one of my test databases and it works. There might be a better way to do this, but I can't think of one.
where i m going to write this code in my form?
zmbd 5,501
Expert Mod 4TB
annakalli:
This should be in the on_click event of your CMD button.
NeoPa's code will go in the button's OnClick event. If you go to your button's properties, go to the events tab click the elipsis button (the three dots), select Code builder and click OK. If you are going to implement my code, then my first code block will go in the button's OnClick event right before NeoPa's code. The second block of code will then go in the form's AfterUpdate event.
NeoPa 32,556
Expert Mod 16PB AnnaKalli:
where i m going to write this code in my form?
Behind your button. That's what you asked for. I assume you know how to do this. If not, then let us know and we can give those details too.
@Seth.
Good catch. I somehow managed to miss the "only" part of the question :-(
Wow, a three-way cross posting :)
@NeoPa
Is what I described the best way to do this? I don't particularly like that any time that a record is modified, the form's Allow Additions property gets set to false. I know it isn't a huge thing, but I can't think of any simple way to keep this from happening or if it is nothing to worry about.
NeoPa 32,556
Expert Mod 16PB Seth:
I don't particularly like that any time that a record is modified, the form's Allow Additions property gets set to false.
Your code doesn't do that Seth. It sets the state (of Me.AllowAdditions) every time the CommandButton is clicked. Having thought about this some more I now realise it wouldn't work anyway exactly as you have it because as soon as Me.AllowAdditions is reset the New record will be cancelled.
Actually, what we need is for Me.AllowAdditions to be reset after a record is inserted (AfterInsert event) as well as when the record pointer is again changed. I must rush out the door now so I'll check on this when I get back. I'll provide some example code when I can, but that is unfortunately not now ;-)
I had the Me.AllowAdditions = False in the Form's AfterUpdate event which did work (I tested it). In this case the Me.AllowAdditions = False would run every time the record was updated. I wasn't familiar with the AfterInsert event, so I looked it up and that, as you stated, would be better as this would only run after a new record was inserted. One question though, is there a way to set the Allow Additions property to false if the user decides he/she doesn't want to make a new record after all? For example you accidentally click new record and then go back to the previous record. With the code setup as described, the form's Allow Additions property wouldn't have been set back to false.
NeoPa 32,556
Expert Mod 16PB NeoPa:
as well as when the record pointer is again changed.
Indeed there is Seth. This would be the Form_Current() event procedure.
There would have to be some sort of test done to make sure it wouldn't turn Allow Additions off when the form went to the new record when the button was clicked if the code was placed in the OnCurrent event. Or does the move to a new record not trigger the OnCurrent event?
NeoPa 32,556
Expert Mod 16PB Seth:
Or does the move to a new record not trigger the OnCurrent event?
Now you're talking. Indeed, that's exactly what triggers the OnCurrent event.
I'm confused. We have the button with the following code in the OnClick event: - Me.AllowAdditions = True
-
DoCmd.GoToRecord(Record:=acNewRec)
We have the form's AfterInsert event that will run after the new record is created: - Me.AllowAdditions = False
To make sure that the Allow Additions doesn't accidentally get left true, we have the form's OnCurrent event to turn it off: - Me.AllowAdditions = False
The problem that I see is that the OnCurrent event will also run when the button sends the form to a new record which will set Allow Additions to false and take the user back to the last record (leaving the new record) before the user has a chance to enter anything in the new record. So there would have to be a test in the OnCurrent event to know if it should set the Allow Additions property to false. I suppose, you could test for if the record is a new record. I believe that I'm remembering and understanding the MSDN website correctly that the OnCurrent event occurs AFTER the new record becomes the current record, so if the button was clicked, the record that would be tested in the OnCurrent event would be a new record. This is untested, but my thinking now is that the form's OnCurrent event should be: - If Not Me.NewRecord Then Me.AllowAdditions = False
Is this what you had in mind NeoPa?
NeoPa 32,556
Expert Mod 16PB Seth:
Is this what you had in mind NeoPa?
I hadn't actually gone through it in such detail Seth, as I was in a bit of a rush earlier, but what you've come up with seems about right. Before running it live I'd check the order of firing of the various events, but I suspect what you have is spot on. It was what I was thinking of as I read through your post.
Thank u very much, is working just fine now
Sign in to post your reply or Sign up for a free account.
Similar topics
by: MJW |
last post by:
Is there a way for me to know if or which command button was just
clicked that triggers the LostFocus event for the current control on a
Form?
I have a form that has many types of users who each...
|
by: Melissa |
last post by:
What is the code to delete a command button from a form? Can the code be run
from the click event of the button to be deleted?
Thanks!
Melissa
|
by: gbb0330 |
last post by:
hi all
first i would like to thank PC Datasheet, i got so far because of
him/her
i need some help again
i have this command button
the on-click event procedure is
|
by: Kevin |
last post by:
A couple of easy questions here hopefully. I've been working on two
different database projects which make use of multiple
forms.
1. Where's the best/recommended placement for command buttons...
|
by: Chas Large |
last post by:
Hi All,
I have written a small program which has command buttons to operate a device
by remote via RS232. It sends a series of commands when a command button is
clicked and held down and stops...
|
by: John Smith |
last post by:
I have a continuous form. there is a command button with the following code
for the OnClick event:
DoCmd.OpenForm "frmPlants", , , "PlantsID =" & Me!PlantsID
I click the button and frmPlants...
|
by: simonoficina |
last post by:
Hello all! I am a vb.net beginner in Spain. When I use VB6 ,the button
object has a property called "default" that can set this button like
press "ENTER" key. But in the VB.net I can't find this...
|
by: ramkishor |
last post by:
How can I change the color of button on mouse move over it.
help .
|
by: WestAussie |
last post by:
Hello
I'm new to .NET and have been working through some exercises to get a bit more fluent.
I have no idea what I have done to cause command buttons to have their background appearance...
|
by: pushrodengine via AccessMonster.com |
last post by:
I have a command button “AddIncident”. I would like to make it so that it
will not add the record to the database if textbox “Type” is empty. I also
would like to add a message that will...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |