Closing A Form

mohammadagul

PrinceAtif
Local time
Today, 15:35
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

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:
i downloaded you db but neither it open in access or with winzip. what format is it. it just says 5323 on the file

secondly whatif the user tries to close the form with CTRL+W or with ALT+F4
what option would i have to measure in this respect...

hope you couldhelp

thanks
 
mohammadagul said:
i downloaded you db but neither it open in access or with winzip. what format is it. it just says 5323 on the file

secondly whatif the user tries to close the form with CTRL+W or with ALT+F4
what option would i have to measure in this respect...

hope you couldhelp

thanks
Not sure what your problem is but I just opened the sample from the post above. It is a zipped file and the sample is in Access 97.

My sample will prevent a user from using the Ctrl + W and Alt + F4 key [and any other way to close] combos from closing the form and db unless they check my "check box".
 
AAAAAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!

I am Going To Kill Myself.....

evrytime i download anykind of data from this site it always tell me that it cannot be opened with zip and also the system could not find the mdb fild attached with this zip.. whats going on ... what is the problem. i dowloaded it again. instead of the file it gives me a number such as 5323... that all. no file no mdb nothing.

what should i do?
 
mohammadagul said:
evrytime i download anykind of data from this site it always tell me that it cannot be opened with zip and also the system could not find the mdb fild attached with this zip.. whats going on ... what is the problem. i dowloaded it again. instead of the file it gives me a number such as 5323... that all. no file no mdb nothing.

Could it be the cause of some security settings? Certain antivirus or firewall software might prevent you from downloading executable files even when zipped, mdb files are executable files.

Peter
 

Users who are viewing this thread

Back
Top Bottom