Save a Record on Another Form

doulostheou

Registered User.
Local time
Today, 04:17
Joined
Feb 8, 2002
Messages
314
I have multiple forms open that are working in conjunction. A button click on Form1 will either create a new record or update a field in a particular record on Form2 (Form2 is a continuous form). I then reflect these changes by requerying Form2.

My problem is that if a user is editing Form2 but has not saved their changes, I have a problem because the code cannot requery until the changes have been saved or update the record if they are editing the record I am trying to update. I have widdled down the problem by detecting if the form is dirty. What I now want to do is save the record on that form, if it is dirty. I know how to do this, if I am on the form (Docmd.Runcommand acCmdSaveRecord), but I cannot figure out how to do this if I am on a different form, as the above would obviously try to save the record on Form1 and not Form2.

I have a few alternative approaches that I am going to try, but even if I figure out how to make this work, it would be helpful to know if there was a way to save the current record on Form2 from Form1.
 
DoCmd.Save acForm, "Form Name" will save the form and not the record
the correct one is Runcommand acCmdSaveRecord.
Why would you want to save changes to Form2 from Form1, the save command belongs on Form2, you should then Requery Form1
 
Again, I agree with Rich. However if the form's record is attached to a table you could save the table. Also, try doing a .recalc or a .refresh


Additionally, I don't know if this will work, but for shits and giggles try:
DataAccessPages("Form Name").Application.RunCommand acCmdSaveRecord
 
Last edited:
I don't know if I explained my issue in enough detail. Form1 actually makes updates to Form2. Form2 is a time log, and one of the things it records is activity that takes place on Form1. When certain things happen on Form1, I automatically make entries on the time log. People can also type information directly into their time log. The problem was that if somebody was editing a record on form2, and switched to form1 without saving their change, I would be thrown an error when I tried to edit a record/requery via code on form1.

One of my workarounds ended up working just fine. I automatically save the record from form2 when it deactivates, if it is dirty. However, I actually happened to stumble across the code I was looking for last night. I will post it here, in case anyone else ever wants to do something similar. I found this at http://www.mvps.org/access/forms/frm0021.htm. I might start using it, just because I have never been particularly fond of the Docmd.RunCommand method.

Code:
Private Sub DeleteCurrentRecord_Click()
    'Code to delete record from specified form
    'I haven't tested it, but I would believe that it would delete the record from any form specified in place of Me.
    If Not (fDelCurrentRec(Me)) Then
        MsgBox "An Error occurred!"
    End If
End Sub
 
Function fDelCurrentRec(ByRef frmSomeForm As Form) As Boolean
    On Error GoTo Err_Section
    
    With frmSomeForm
        If .NewRecord Then
            .Undo
            fDelCurrentRec = True
            GoTo Exit_Section
        End If
    End With
    
    With frmSomeForm.RecordsetClone
        .Bookmark = frmSomeForm.Bookmark
        .Delete
        frmSomeForm.Requery
    End With
    fDelCurrentRec = True
Exit_Section:
    Exit Function

Err_Section:
    fDelCurrentRec = False
    Resume Exit_Section
End Function
 
Last edited:
Well, your code you posted was going to be my final workaround for you...but I wanted to see if the runcommand would work (as I've never tried using it like that).

In my opinion using Access's save commands is a little bit better on resources. The work around that I was going to add as a last resort, which you happened to find last night, just creates a new record, forcing Access to save the previous one... then you can point back to the one you were on and delete the new(blank) records - making it seem as if you just saved the record.
 
The alternative runcommand did not work when I checked it. It's ashame that there isn't a built in access command to save the current record on a specified form.
 
I bet you there is... we just dont know what it is =)

Again, I dont know if this would work at all but you could try:

Dim rs As Recordset
Set rs = Forms("Form Name").Recordset
rs.Requery
rs.Update


I don't know if that would work, but generally a record is saved when you move to another records... or apply/remove a filter

It never hurts to give it a shot.
 
Last edited:
doulostheou said:
The problem was that if somebody was editing a record on form2, and switched to form1 without saving their change, I would be thrown an error when I tried to edit a record/requery via code on form1.


Am I missing something here?

If IsLoaded("Form1") And Me.Dirty Then
Runcommand acCmdSaveRecord
Forms!Form1. Requery
End If
 
Rich, what you typed was a little backwards from what I was looking to do. Form1 is attempting to save the current record on Form2 if Form2 is loaded and is dirty.

So what I want to do is initiate the following when a button is pressed/depressed on Form1,

Code:
If IsLoaded(Form2) then
    If Forms!Form2.Form.Dirty Then
       Docmd.Runcommand acCmdSaveRecord, "Form2" 'Save the Record on Form 2  
    End If

    Forms!Form2.Form.Requery
End If

What I ended up doing which worked fine, is automatically saving Form2 On_Deactivate when it is dirty. This solution works great for my applications purposes, but I was still wishing there was a built in Access command to save the record on Form2 from an event that occurs on Form1.
 
Had a similar requirement, this worked for me...

Forms!frmName.Dirty = false
 

Users who are viewing this thread

Back
Top Bottom