Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New record overwrites existing record

Status
Not open for further replies.

burritonator

IS-IT--Management
Jun 15, 2004
133
0
0
US
I have a module that contains the following two lines of code:

-----------------------------------------------------------
DoCmd.GoToRecord , , acNewRec
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70
-----------------------------------------------------------

The reason that these two lines of code appear together in the module with nothing between them is because the fields on the form already contain the data that I want to write to the table, so I am just trying to create a new record in the table and write the form's data to it.

The problem is that every time this code executes, it always overwrites the first record in the table instead of adding a new record to the table, so that there is never actually more than one record in the table at a given time. I can't figure why this is happening, as I thought the "DoCmd.GoToRecord , , acNewRec" line of code would ensure that the data gets written to a new record in the table. In addition, I've used the same code in other places in my project, and it works just fine. Granted, I usually have several other lines of code between the two shown above, but none of it is code that has any effect on record navigation or manipulation, so I don't understand why the two lines above do not work properly when they appear side by side in the code.

Thanks in advance for any help that anyone can provide.

Burrito
 
Burritonator,

If you have a record open on the screen, all you need to do is save it.

The command- "DoCmd.GoToRecord , , acNewRec" opens up a new blank record - and it is abandoning your existing record because you have not saved it.

You are telling it to create a new blank record, then you are telling it to save that blank record.

All you need to execute is: "DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,acMenuVer70" that will save your open record.

Kramerica
 
One thing that I forgot to mention in my original post is that when the record is saved, the correct data is written to the table, not a blank record. However, since the same record always gets overwritten, the only record that ever exists in the table is the last one that was written to it.

I removed the "DoCmd.GoToRecord , , acNewRec" line from the code as suggested and left only "DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,acMenuVer70", but it had no effect - the first record in the table still gets overwritten each time.

However, even as I typed the two paragraphs above, I realized what was causing the problem. When the form in question loads, the values for all of its fields are set through code. The user of the application does not enter any data on this particular form. The mistake that I made was that I placed the code that sets the values of the fields at the beginning of the procedure, followed by the lines to add and save a new record. Since the code was setting the values of bound fields without ever having navigated to any record, the first record was always getting overwritten.

All I had to do to solve the problem was to move the "DoCmd.GoToRecord , , acNewRec" line to the top of the procedure so that the new record was created before the values of the bound fields were set. A simple solution to a simple problem, but it was just one of those things that I overlooked.

Thanks,
Burrito
 
Burrito
(Man, do I have the munchies all of a sudden)

Code:
DoCmd.GoToRecord , , acNewRec
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70

Your new record may not be written to the table because it seems you are saving a blank record. Data integrity rules may prevent anything from happening. You are probably not saving the record, or just saving the first record which probably does not have any updates, etc. (To test, this you would have to have an autonumber field, and see if the autonumber changes)

Why not just use...[tt]
DoCmd.GoToRecord , , acNewRec[/tt]

Have the end user click on a save button, or advance to the next record which will also save the record.

It seems that you are not allowing the end user to enter any data.

Richard

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top