Problem saving records from code

J

Jean-Marie

In my DB (Access 2000 /Win X Home) I have 2 tables:
- tblTransactions to record transactions on transport vehicles
- tblIncidents linked to tblTransactions to record incidents related to a
transaction if any.
- I have a form frmTransactions to capture transaction data and a button on
that form to open the frmIncidents that captures incident for current
transaction
In the click event of the button I have inserted this code that I used the
assistant to obtain:
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
it should save the curent transaction record.
WhenI test the forms I get a message stating: "The command or action 'save
record' isn’t available now."
In an attempt to fix this I changed the code to:
DoCmd.RunCommand acCmdSaveRecord.
That it didn’t help, I am getting the same message. I don’t understand what
I did wrong.
I would very much appreciate if someone could help me fix this.
Many thanks in avance.
 
A

Allen Browne

In what event are you running this code?

For example, if you run it in the KeyDown of a control, there may be other
events that need to run first (such as the control's Change, BeforeUpdate,
AfterUpdate events).

There may also be reasons why the record cannot be saved, e.g.:
- You are at a new record.
- No changes have been made (it's not dirty).
- There is a validation rule that is not met.
- The BeforeUpdate event of the Form is cancelled.

If those suggestions are not useful, it is possible that the project is
corrupt, or there is a locking problem. Post back for more info.
 
J

Jean-Marie

Thank you Allen for your response

This is how I want the thing to work. After I type in transaction data in
the frmTransactions form, I would like to click on the cmdIncident button and
open the frmIncidents form to type in the incident information for the
transaction record I just created. So it needs to be saved.
Thus my record is new and as you stated it may not be saved. Can you please
help me understanding why? How I can get around this?

Many thanks.


Allen Browne said:
In what event are you running this code?

For example, if you run it in the KeyDown of a control, there may be other
events that need to run first (such as the control's Change, BeforeUpdate,
AfterUpdate events).

There may also be reasons why the record cannot be saved, e.g.:
- You are at a new record.
- No changes have been made (it's not dirty).
- There is a validation rule that is not met.
- The BeforeUpdate event of the Form is cancelled.

If those suggestions are not useful, it is possible that the project is
corrupt, or there is a locking problem. Post back for more info.
 
A

Allen Browne

Okay, so you are using the Click event of the command button.

That should be available, unless there is some reason why you cannot move
focus to the command button. For example, if you are entering a value into a
text box bound to a field that has its Required property set to True, and
you backspace out the entry, then you won't be able to get out of the box
until you enter something (or press Esc to undo the field).

Other than that, you should be able to code like this:
Private Sub cmdIncident_Click()
If Me.Dirty Then Me.Dirty = False
'whateever else you want your button to do.
End Sub

Let us know is that doesn't work for you.
 
J

Jean-Marie

Allen,
At first it did not work, then looking closely at my form I noticed that the
AllowEdits property was set to False. I don't remember when and why I did
this. I changed the settings to Yes and it worked.
I am very sorry about that.
Thanks you very much for your help.
By the way I went to your site and found a lot of tricks that will certainly
help me with my application. Thank you for taking your time to make all these
resources available to us.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top