Link to home
Start Free TrialLog in
Avatar of pdvsa
pdvsaFlag for United States of America

asked on

Save record code but only if need to

Experts:  I am not sure if the fbelow makes sense.  I only want to save the record if there is something to save.  I figure if Dirty then Save.  I dont want to save every time I click the button because it takes a few seconds to save and if nothing to save then do nothing and open the form.  

Private Sub btnOpenLCForm_Click()

 If Me.Dirty Then
        Me.Dirty = False
    DoCmd.RunCommand acCmdSaveRecord
   
    End If
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

That makes sense, but you don't need both of those lines.  I generally use:

Private Sub btnOpenLCForm_Click()

    If Me.Dirty Then Me.Dirty = False
   
End If
But if that is all you are going to do in that click event, you might even consider disabling that button altogether, then using the Form_Dirty event to enable it.  Then you know that it isn't even available until the form has been dirtied.
Avatar of pdvsa

ASKER

OK I see.  I would need the button all time active.  I have heard that Me.Dirty = False does not save the record though but on the other hand i have heard it does.  I dont know the best way.  
Avatar of pdvsa

ASKER

I also have on the form level on the Dirty Event :  If Me.Dirty Then Me.Dirty = False
but I find this not working sometimes.  If I had the Dirty on the Dirty Event then I should not need it anywhwere else I would think.  

If you used:

Private Sub Form_Dirty

    me.Dirty = false

End sub

You would have no way of cancelling the changes you have made to a record.  This would fire every time you make a change to any field, and if you wanted to abandon your changes (with me.undo or the Escape key), neither of those would work.

I suspect you have other code in that button click event, since the name of the button is btnOpenLCForm, so I would recommend just checking to see if it is dirty before opening that form.  Something like

Private Sub btnOpenLCForm_Click

    if me.dirty then me.dirty = false

    docmd.openform "frmLC"

End Sub
Avatar of pdvsa

ASKER

Fyed:
< suspect you have other code in that button click eventYes
yes I do...

does it look ok?  Please pay attn to the first IF...the Dirty section.  I am not sure if having 2 if's if correct too.  Thank you.

Private Sub txtLCNo_Click()

        If Me.Dirty Then
        Me.Dirty = False
    DoCmd.RunCommand acCmdSaveRecord
    
    End If
    
    If CurrentProject.AllForms("frmLetterOfCreditAdd").IsLoaded = False Then
         DoCmd.OpenForm "frmLetterOfCreditAdd", , , , , , OpenArgs:=Me.ID
        'Cancel = True
    Else
    
        If Forms!frmLetterOfCreditAdd.Filter = "[LetterOfCreditID] = " & Me.ID Then
            Forms!frmLetterOfCreditAdd.SetFocus
            Else
            DoCmd.Close acForm, "frmLetterOfCreditAdd"
            DoCmd.OpenForm "frmLetterOfCreditAdd", , , , , , OpenArgs:=Me.ID
        End If
    End If

Open in new window

Avatar of pdvsa

ASKER

Do you think that in a multi user environment
Private Sub Form_Dirty

    me.Dirty = false

End sub

--woudl not be recommended?  I seem to get an error when >1 user is in the same record.  
Avatar of pdvsa

ASKER

the btn names are different... Private Sub txtLCNo_Click() and  Private Sub btnOpenLCForm_Click()    (makes no difference though but I do realize they are different...actually made mistake and copied wrong one)
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pdvsa

ASKER

Much appreciated Dale.  Wish i could give you more pts...
Avatar of pdvsa

ASKER

Dale:  I have a follow up.  I have had this on my mind for sometime and since we are talking about it I thought I would ask.  Do you recommend the below?  Doesnt by having the below take the place of putting the code on a single button like on Private Sub txtLCNo_Click()

thanks fro the advice.  I want to know what experts do.

Private Sub Form_Dirty(Cancel As Integer)

    If Me.Dirty Then
        'Cancel = 1
        Dirty = False
    End If
   
    DoCmd.RunCommand acCmdSaveRecord
   
   
End Sub
I do not recommend putting code in the Dirty event which saves the current record.  Doing so prevents me from cancelling my changes.  I generally include "Save" and "Cancel" buttons on most of my forms where the user will do data entry.  I don't allow the user to navigate off the record unless they have click or use shortcuts to activate the Click event of one of these buttons.

BTW, you don't need both of the following:

me.dirty = False
docmd.runcommand acCmdSaveRecord

they both do the same thing, so use one or the other, but not both.
Avatar of pdvsa

ASKER

Dave ... Sure do appreciate this.  I was under the assumption that
me.Dirty = False and  
docmd.runcommand acCmdSaveRecord

did perform the same function but i did hear that me.Dirty = False does not save the record but i believe now i had misinformation.  

I am also going to remove the Dirty Event code so i can undo an action like you mentioned.  Maybe this is the reason why i get an #error sometimes in a comments field when there is >1 user in same record. When i get this error what happens is the data in the comments field is deleted and replaced with a #error and cannot recover the deleted data.  I have to make a new record.  Maybe the error has something to do with the Dirty Event code i have now.  

Thanks again.... Enjoy the weekend.        

Why would you have more than one user using the same record?  This is pretty rare, the way most organizations are structured, it would be rare that you would have more than one user working on the same record, and form, at the same time.