Avatar of james252
james252Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Error when more than one person is in the database and the save button is clicked.

When more than one person is in the database, data can't be saved. It appears that the database is shared without locks. The debug takes me to the code snippet attached and highlights the first DoCmd.Save action. This is not my code but appears to be a sort of 2nd chance before saving data.
Can anyone help?
J
Private Sub Form_BeforeUpdate(Cancel As Integer)
 
    If MsgBox("Are you sure you want to save?", vbYesNo + vbInformation) = vbYes Then
 
        DoCmd.Save acForm, "frmBPREdit"
        MsgBox "Your changes have been saved!  ", vbInformation
        Check = 1
                    
    Else
           
        Exit Sub
        MsgBox "No changes have been recorded!  ", vbInformation
        DoCmd.Close acForm, "frmBPREdit", acSaveNo
    
    End If
 
End Sub

Open in new window

Microsoft Access

Avatar of undefined
Last Comment
james252
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

The code looks completely wrong to me.

The Save is saving the form design not the data in the form.

The code will only run when there is data to be saved, but a No answer looks as if it is bound to produce an error message because the cancel command has not been issued.
Avatar of james252
james252
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks for that....
My programming is just about up to adding a save button! Is there a simple way of holding the save until the "Are you sure?" question has been answered and to continue with save if yes and to cancel change/deletion if no?
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Well it's tricky because I don't know what the user is doing to trigger this event.  It could be anything that causes Access to save the current record.

This code will save or abandon the save but in either case leaves the data as it was before the event occurred - so changes are still pending.
Obviously I have no idea what 'Check=1' is for so I have left it in.  Also, you could still get an error message, depending on what the user did to trigger the code.


Private Sub Form_BeforeUpdate(Cancel As Integer)
 
    If MsgBox("Are you sure you want to save?", vbYesNo + vbInformation) = vbYes Then
        MsgBox "Your changes have been saved!  ", vbInformation
        Check = 1
    Else
         Cancel = true ' abandon the save
        MsgBox "No changes have been recorded!  ", vbInformation

    End If
 
End Sub
Avatar of james252
james252
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks very much. I'll give it a go.
J
Avatar of james252
james252
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Didn't work but have just found rest of code which might shed some light, see snippet...
J
Option Compare Database
Dim Check As Integer
 
Private Sub Form_AfterUpdate()
 
If Check = 1 Then
 
        Dim stDocName As String
 
        DoCmd.SetWarnings False
        stDocName = "qryUpdateBPR"
        DoCmd.OpenQuery stDocName, acNormal, acEdit
        DoCmd.SetWarnings True
 
End If
 
End Sub
 
 
Private Sub Form_BeforeUpdate(Cancel As Integer)
 
    If MsgBox("Are you sure you want to save?", vbYesNo + vbInformation) = vbYes Then
 
        DoCmd.Save acForm, "frmBPREdit"
        MsgBox "Your changes have been saved!  ", vbInformation
        Check = 1
                    
    Else
           
        Exit Sub
        MsgBox "No changes have been recorded!  ", vbInformation
        DoCmd.Close acForm, "frmBPREdit", acSaveNo
    
    End If
 
End Sub
 
 
Private Sub cmdSave_Click()
 
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  
End Sub
 
Private Sub cmdClose_Click()
 
    DoCmd.Close
    DoCmd.Close acForm, "frmBPRSearch", acSaveNo
    
End Sub
 
 
 
Private Sub Form_Load()
 
Check = 2
 
End Sub

Open in new window

Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

What does 'didn't work' mean?  What happens?
Avatar of james252
james252
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

It seems to save ok when a change has been made and yes replied to "Are you sure" but I still get runtime error 2501 if I select No to "Are you sure" and debug screen kicks in with message:
"Run-time error '2502' The DoMenuItem action was cancelled"
J
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Modify this bit...


Private Sub cmdSave_Click()
 on error resume next
    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  if err= 2501 then exit sub   'user cancelled save
msgbox  err.number & ":" & err.description
End Sub
Avatar of james252
james252
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Works fine, but the save option brings up an extra message box with "0:" in it. I can see where this comes from but can it be suppressed?
J
PS I appreciate your patience!
ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of james252
james252
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks very much. Works fine now. Thanks for the time and effort.
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo