any wrong with the code?

aabbbcc

Registered User.
Local time
Yesterday, 23:31
Joined
Nov 14, 2015
Messages
28
Hi, I want a form to do the following:
1, default new record when i open the form
2, message box asking save or not save when i click save button and before updated event.
3,default not allowing editing and after updated(even save or not save) ,and a button to unlock.

the codes are as follows:

_____________________________________________________
Option Compare Database
Option Explicit
__________________________________________________ __
Private Sub Lock_Records(blnLock As Boolean)
Me.AllowEdits = blnLock
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acSubform
ctl.Form.AllowEdits = blnLock
End Select
Next
End Sub
__________________________________________________ ___
Private Sub Lock_New_Records(blnLock As Boolean)
Me.AllowAdditions = blnLock
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acSubform
ctl.Form.AllowAdditions = blnLock
End Select
Next
End Sub
__________________________________________________ _____
Private Sub cmdUE_Click()
Call Lock_Records(True)
Call Lock_New_Records(True)
End Sub
__________________________________________________ ___
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strResponse As String
If Me.Dirty Then
Select Case MsgBox( _
Prompt:="Data updated" & _
vbCrLf & "Saved?", _
Buttons:=vbExclamation Or vbYesNo)
Case vbYes
DoCmd.RunCommand acCmdSave
Call Lock_Records(False)
Call Lock_New_Records(False)
Case vbNo
DoCmd.RunCommand acCmdUndo
Call Lock_Records(False)
Call Lock_New_Records(False)
End Select
End If
End Sub
__________________________________________________ ___
Private Sub Form_Current()
If Me.NewRecord = True Then
Call Lock_Records(True)
Call Lock_New_Records(True)

Else
Call Lock_Records(False)
Call Lock_New_Records(False)
End If
End Sub
_________________________________________________
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
_________________________________________________
Private Sub cmdSave_Click()
If Me.Dirty Then
Select Case MsgBox( _
Prompt:="Data updated" & _
vbCrLf & "Saved?", _
Buttons:=vbExclamation Or vbYesNo)
Case vbYes
DoCmd.RunCommand acCmdSave
Call Lock_Records(False)
Call Lock_New_Records(False)
Case vbNo
DoCmd.RunCommand acCmdUndo
Call Lock_Records(False)
Call Lock_New_Records(False)
End Select
End If
End Sub
______________________________________________

But some problems pop up again sadly,

1,it only works on the main form, but ignore the update of subform when ticked the save button

2, when i click the save button, and i save "Yes", it will pop up the messages box again to ask me save it or not. But No problem when i say No.

3, When i enter data to the entired new record and click save button. There is a message appearing that data cant be saved because of errors.

It is hoped that you could kindly help.
Thanks again!
 
then copy your code to the pertinent event in your subform.
 
then copy your code to the pertinent event in your subform.

got it ..
thanks
how about
2, when i click the save button, and i save "Yes", it will pop up the messages box again to ask me save it or not. But No problem when i say No.

3, When i enter data to the entired new record and click save button. There is a message appearing that data cant be saved because of errors.
 
that is because you have two event that handles the saved record. Form_BeforeUpdate and cmdSave_Click. choose only one to use.
 
could I use them at the same time and what code could I use to avoid the conflict ?!
Thanks
 
insert a private variable after Option Explicit

Private bolSaved As Boolean

on your cmdSave_Click event:

Private Sub cmdSave_Click()
If Me.Dirty Then
Select Case MsgBox( _
Prompt:="Data updated" & _
vbCrLf & "Saved?", _
Buttons:=vbExclamation Or vbYesNo)
Case vbYes
DoCmd.RunCommand acCmdSave
Call Lock_Records(False)
Call Lock_New_Records(False)
Case vbNo
DoCmd.RunCommand acCmdUndo
Call Lock_Records(False)
Call Lock_New_Records(False)
End Select
End If
bolSaved = True
End Sub

on Before_Update:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strResponse As String
If Not bolSaved
If Me.Dirty Then
Select Case MsgBox( _
Prompt:="Data updated" & _
vbCrLf & "Saved?", _
Buttons:=vbExclamation Or vbYesNo)
Case vbYes
DoCmd.RunCommand acCmdSave
Call Lock_Records(False)
Call Lock_New_Records(False)
Case vbNo
DoCmd.RunCommand acCmdUndo
Call Lock_Records(False)
Call Lock_New_Records(False)
End Select
End If
Else
bolSaved=False
End If
End Sub
 
_______________________________________________
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strResponse As String
If Not bolSaved <-the system sends error of this code.
If Me.Dirty Then
Select Case MsgBox( _
Prompt:="記錄已更新" & _
vbCrLf & "是否確定儲存?", _
Buttons:=vbExclamation Or vbYesNo)
Case vbYes
DoCmd.RunCommand acCmdSave
Call Lock_Records(False)
Call Lock_New_Records(False)
Case vbNo
DoCmd.RunCommand acCmdUndo
Call Lock_Records(False)
Call Lock_New_Records(False)
End Select
End If
Else
bolSaved = False
End If
End Sub
_______________________________________________
 
declare the a module-wide variable bolSaved:

Option Compare Database
Option Explicit

Private bolSaved As Boolean
...
 
Still not working sorry:(
___________________________
Option Compare Database
Option Explicit
Private bolSaved As Boolean
_______________________________
Private Sub cmdUnlock_Click()
Me.AllowEdits = True
End Sub
Private Sub Form_AfterUpdate()
Me.AllowEdits = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strResponse As String
If Not bolSaved
If Me.Dirty Then
Select Case MsgBox( _
Prompt:="Data updated" & _
vbCrLf & "Saved?", _
Buttons:=vbExclamation Or vbYesNo)
Case vbYes
DoCmd.RunCommand acCmdSave
Case vbNo
DoCmd.RunCommand acCmdUndo
End Select
End If
Else
bolSaved = False
End If
End Sub
__________________________________
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
_______________________________
Private Sub Save_Click()
If Me.Dirty Then
DoCmd.RunCommand acCmdSave
End If
bolSaved = True
End Sub
 

Users who are viewing this thread

Back
Top Bottom