Save Button Not working as planned

cjmitton

Registered User.
Local time
Today, 17:59
Joined
Mar 25, 2011
Messages
70
Below is my code for a 'save' button. It works except for one element, When I click the button the message appears as expected, then saves the record as directed but when it returns to the form it goes to the first record?

Code:
Private Sub cmd_Save_Click()
Dim Response, Msg, Style, Title
    Msg = "Once Saved Contact will be read-only to edit again you must click edit or click no to continue editing Contact"
    Style = vbYesNo
    Title = "Saving?"
    If (Me.Dirty = True) Then
        Response = MsgBox(Msg, Style, Title)
            If Response = vbYes Then
                Me.Dirty = False
            Else
                Me.Undo
            End If
    End If
    Me.C_Salutation.SetFocus
    Me.AllowEdits = False
    Me.DataEntry = False
    Me.AllowAdditions = True
End Sub

How do I direct it to return to the original record that was being edited?
 
You may wish to avoid the last 4 lines in case of No
 
True... I'll adjust that part of the code.

Thanks
 
Moved the 4 line to after the Me.Dirty = False part but it still goes back to the first record?
 
I am not sure - what happens if you comment out those lines temporarily?
 
It Works! but How do i set the record to being read only so they can not make further changes?

Just so your aware, I'm trying to open the forms as read-only so to edit it the have to actual 'click a button' so they are aware they are changing it! then once ave returning to read only so they cannot accidentally change something else by mistake!
 
try this, untested

dim myLine
myLine=me.recordset.bookmark ' insert this at the beginning of your button handler

me.recordset.bookmark=myline ' insert this as the last line
 
So Close!, On a normal save it worked (i.e. I opened the record, clicked Edit, made a change then clicked Save then Yes).

If I was creating a new record it failed. I'm guessing that as the record had not been save it could not 'bookmark' it.

Just so you can see what I did here's the code:
Code:
Private Sub cmd_Save_Click()
Dim Response, Msg, Style, Title
Dim myLine
    Msg = "Once Saved Contact will be read-only to edit again you must click edit or click no to continue editing Contact"
    Style = vbYesNo
    Title = "Saving?"
    If (Me.Dirty = True) Then
        Response = MsgBox(Msg, Style, Title)
            If Response = vbYes Then
                Me.Dirty = False
                myLine = Me.Recordset.Bookmark
                Me.C_Salutation.SetFocus
                Me.AllowEdits = False
                Me.DataEntry = False
                Me.AllowAdditions = True
            Else
                Me.Undo
            End If
    End If
    Me.Recordset.Bookmark = myLine
End Sub

And incase your wondering my 'Add New' button is like this:

Code:
Private Sub cmd_add_Click()
Dim Response, Msg, Style, Title
    Msg = "Contact Not Saved! Do you wish to Save before adding a new Contact?"
    Style = vbYesNo
    Title = "Contact not Saved"
    Me.C_Salutation.SetFocus
    If (Me.Dirty = True) Then
        Response = MsgBox(Msg, Style, Title)
            If Response = vbYes Then
                Me.Dirty = False
                Me.AllowAdditions = True
                Me.DataEntry = True
            Else
                Me.Undo
            End If
    End If
End Sub
 
Nearly works, if I click on my add new button to add a new contact then click the save button it saves then goes to the first record instead of showing the current record just added.

If I edit then save it works fine.

I'm trying to get a single form to Add, edit, view contacts.
 
But your Add-thingy does the same job as your Save-thingy - you have already SAved inside the Add-handler, so the form is not dirty, and so the bookmark does not get set in SAve-Click
 
Did not realise that, Never used recordset bookmark before! I'll try to remove the 'dirty' Parts!
 
Tried again with out the dirty save and if I edit / save its fine.
If I'm adding a new record I get a error:
Error 3021, no current record.
Code:
Private Sub cmd_Save_Click()
Dim Response, Msg, Style, Title
Dim myLine
    Msg = "Once Saved Contact will be read-only to edit again you must click edit or click no to continue editing Contact"
    Style = vbYesNo
    Title = "Saving?"
    If (Me.Dirty = True) Then
        Response = MsgBox(Msg, Style, Title)
            If Response = vbYes Then
'                Me.Dirty = False
                myLine = Me.Recordset.Bookmark
                Me.C_Salutation.SetFocus
                Me.AllowEdits = False
                Me.DataEntry = False
                Me.AllowAdditions = True
                Me.Recordset.Bookmark = myLine
            Else
'                Me.Undo
                Exit Sub
            End If
    End If
End Sub
 
I am not sure now what you save and where - your Add does seemingly same stuff as Save

To check if you are on new record you can use

If Me.NewRecord Then ' you are on new record

I suspect this should be enough to sort you out :)
 
The form this is running from is a data input form for a single table, there's no links / queries to other table. I'm just trying to do a one form to do 'all' for my contacts, either add a new contact or update / view contacts.

With your If Me.NewRecord then...
Would that replace the "If (Me.Dirty = True) Then..." line?
 
I have given you the means to check if you are on a new record. You could eg use it to NOT execute the line or lines code that gives you the error.
 

Users who are viewing this thread

Back
Top Bottom