mohammadagul
PrinceAtif
- Local time
- Tomorrow, 00:20
- Joined
- Mar 14, 2004
- Messages
- 298
Hello friends
I have a small problem here that I cannot fix.
In my customer table I want to control that how user close a form.
I got this from Microsoft Site . this is how it is done
This example prevents the user from using ALT+F4 to close a form:
1. Open the sample Microsoft Access database file Northwind.mdb or the sample Microsoft Access Project file NorthwindCS.adp.
2. Open the Customers form in Design view.
Change the following property:
Form: Customers
-------------------------
CloseButton: No
3. On the View menu, click Code.
4. Type the following lines in the Declarations section:
Option Explicit
Public blnClose As Boolean
Add the following line of code to the Load event of the form:
blnClose = False
5. Add the following control to the form:
Command Button
-------------------
Name: cmdCloseForm
Caption: &Close
6. Add the following line of code to the Click event of the command button, cmdCloseForm:
blnClose = True
DoCmd.Close acForm, "Customers", acSaveNo
7. Add the following code to the UnLoad event of the form:
Dim strMessage As String
Dim intStyle As Integer
Dim strTitle As String
strMessage = "You are attempting to close this form incorrectly." & _
vbCrLf & "Please try again using the designated Close Button"
intStyle = vbOKOnly + vbCritical
strTitle = "Closing Customers?"
If blnClose = False Then
MsgBox prompt:=strMessage, buttons:=intStyle, Title:=strTitle
Cancel = True
End If
8. Open the Customers form in Form view.
Note that the Close Button icon button remains visible but appears dimmed (grayed). Clicking Close on the File menu causes the custom message to be displayed. This message is also displayed if the user presses ALT+F4
Now Instead of using a command button I have use the custom toolbar close button to close the form. This is the function I have developed to close the form with a prompt message i.e either to save a record or not to save it.
Function saveClose()
On Error GoTo Err_SaveClose
DoCmd.SetWarnings False
Dim msg, Style, Title, Response, MyString
msg = "Do you want to update Database and close current Transaction...?" & Chr(13) & Chr(13) & "Click Yes to update Database!" & Chr(13) & Chr(10) & "Click No to abort updation!"
Style = vbYesNo + vbInformation + vbDefaultButton1
Title = "Authentication Require......!"
Response = MsgBox(msg, Style, Title)
If Response = vbYes Then
MyString = "Yes"
DoCmd.RunCommand acCmdSave
DoCmd.RunCommand acCmdClose
Else
If Response = vbNo Then
MyString = "No"
DoCmd.RunCommand acCmdUndo
DoCmd.RunCommand acCmdClose
End If
End If
Exit_SaveClose:
DoCmd.SetWarnings True
Exit Function
Err_SaveClose:
If err = 2046 Then 'The command or action 'Delete Record' isn't available now
DoCmd.SetWarnings True
Exit Function
ElseIf err = 2501 Then 'The Delete Record action was canceled
DoCmd.SetWarnings True
Exit Function
Else
MsgBox err.Number & " - " & err.Description
Resume Exit_SaveClose
End If
End Function
If I put the
blnClose = True
in the declaration section of this function, It does not work. Can anyone tell me what am I doing wrong here. This is the function I am trying to use
I have a small problem here that I cannot fix.
In my customer table I want to control that how user close a form.
I got this from Microsoft Site . this is how it is done
This example prevents the user from using ALT+F4 to close a form:
1. Open the sample Microsoft Access database file Northwind.mdb or the sample Microsoft Access Project file NorthwindCS.adp.
2. Open the Customers form in Design view.
Change the following property:
Form: Customers
-------------------------
CloseButton: No
3. On the View menu, click Code.
4. Type the following lines in the Declarations section:
Option Explicit
Public blnClose As Boolean
Add the following line of code to the Load event of the form:
blnClose = False
5. Add the following control to the form:
Command Button
-------------------
Name: cmdCloseForm
Caption: &Close
6. Add the following line of code to the Click event of the command button, cmdCloseForm:
blnClose = True
DoCmd.Close acForm, "Customers", acSaveNo
7. Add the following code to the UnLoad event of the form:
Dim strMessage As String
Dim intStyle As Integer
Dim strTitle As String
strMessage = "You are attempting to close this form incorrectly." & _
vbCrLf & "Please try again using the designated Close Button"
intStyle = vbOKOnly + vbCritical
strTitle = "Closing Customers?"
If blnClose = False Then
MsgBox prompt:=strMessage, buttons:=intStyle, Title:=strTitle
Cancel = True
End If
8. Open the Customers form in Form view.
Note that the Close Button icon button remains visible but appears dimmed (grayed). Clicking Close on the File menu causes the custom message to be displayed. This message is also displayed if the user presses ALT+F4
Now Instead of using a command button I have use the custom toolbar close button to close the form. This is the function I have developed to close the form with a prompt message i.e either to save a record or not to save it.
Function saveClose()
On Error GoTo Err_SaveClose
DoCmd.SetWarnings False
Dim msg, Style, Title, Response, MyString
msg = "Do you want to update Database and close current Transaction...?" & Chr(13) & Chr(13) & "Click Yes to update Database!" & Chr(13) & Chr(10) & "Click No to abort updation!"
Style = vbYesNo + vbInformation + vbDefaultButton1
Title = "Authentication Require......!"
Response = MsgBox(msg, Style, Title)
If Response = vbYes Then
MyString = "Yes"
DoCmd.RunCommand acCmdSave
DoCmd.RunCommand acCmdClose
Else
If Response = vbNo Then
MyString = "No"
DoCmd.RunCommand acCmdUndo
DoCmd.RunCommand acCmdClose
End If
End If
Exit_SaveClose:
DoCmd.SetWarnings True
Exit Function
Err_SaveClose:
If err = 2046 Then 'The command or action 'Delete Record' isn't available now
DoCmd.SetWarnings True
Exit Function
ElseIf err = 2501 Then 'The Delete Record action was canceled
DoCmd.SetWarnings True
Exit Function
Else
MsgBox err.Number & " - " & err.Description
Resume Exit_SaveClose
End If
End Function
If I put the
blnClose = True
in the declaration section of this function, It does not work. Can anyone tell me what am I doing wrong here. This is the function I am trying to use
Code:
option compare
option explicit
Public blnClose As Boolean
Function saveClose()
On Error GoTo Err_SaveClose
blnClose = True
DoCmd.SetWarnings False
Dim msg, Style, Title, Response, MyString
msg = "Do you want to update Database and close current Transaction...?" & Chr(13) & Chr(13) & "Click Yes to update Database!" & Chr(13) & Chr(10) & "Click No to abort updation!"
Style = vbYesNo + vbInformation + vbDefaultButton1
Title = "Authentication Require......!"
Response = MsgBox(msg, Style, Title)
If Response = vbYes Then
MyString = "Yes"
DoCmd.RunCommand acCmdSave
DoCmd.RunCommand acCmdClose
Else
If Response = vbNo Then
MyString = "No"
DoCmd.RunCommand acCmdUndo
DoCmd.RunCommand acCmdClose
End If
End If
Exit_SaveClose:
DoCmd.SetWarnings True
Exit Function
Err_SaveClose:
If err = 2046 Then 'The command or action 'Delete Record' isn't available now
DoCmd.SetWarnings True
Exit Function
ElseIf err = 2501 Then 'The Delete Record action was canceled
DoCmd.SetWarnings True
Exit Function
Else
MsgBox err.Number & " - " & err.Description
Resume Exit_SaveClose
End If
End Function
Last edited by a moderator: