A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

How to prevent form from closing



 
 
Thread Tools Display Modes
  #1  
Old May 18th, 2010, 09:22 AM posted to microsoft.public.access.forms
McHammer
external usenet poster
 
Posts: 4
Default How to prevent form from closing

Hello everybody,
I use a modal popup form to enter data in a db on which there a 2 buttons
"save and close" and "cancel". I'd like to prevent the from from closing
through the "X" button on the top left corner with unwanted/incomplete data.
Do do that I trap the use of these buttons in before update event so in the
else case I put a warning msgbox (vbOKonly) and cancel = true to prevent any
other event. So far so good. After this event an extra message of Access
advises me that the record can't be saved and if I want to close anyway the
form or not. I want to avoid this message and of course the closure of the
form. Why cancel = true is not enough to do that and what else I could do?

Thks in advance
  #2  
Old May 18th, 2010, 12:53 PM posted to microsoft.public.access.forms
BruceM via AccessMonster.com
external usenet poster
 
Posts: 448
Default How to prevent form from closing

It usually helps to post the actual code when there is a question about the
code. Why show them the X button (top right corner, not left, but I assume
you meant that) at all? You should be able to use the form's Property Sheet
to set Control Box to No (or set it to Yes, with MinMax buttons set to Yes
and Close Button to No if you want to keep the minimize and maximize buttons),
or you could set Border Style to None.

It may be possible, from what I can tell, to put something together to get
rid of the error message when the X button is clicked, but it would be a lot
of work for something that doesn't work as well as what you could have with
your own command buttons.

McHammer wrote:
Hello everybody,
I use a modal popup form to enter data in a db on which there a 2 buttons
"save and close" and "cancel". I'd like to prevent the from from closing
through the "X" button on the top left corner with unwanted/incomplete data.
Do do that I trap the use of these buttons in before update event so in the
else case I put a warning msgbox (vbOKonly) and cancel = true to prevent any
other event. So far so good. After this event an extra message of Access
advises me that the record can't be saved and if I want to close anyway the
form or not. I want to avoid this message and of course the closure of the
form. Why cancel = true is not enough to do that and what else I could do?

Thks in advance


--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...forms/201005/1

  #3  
Old May 18th, 2010, 05:10 PM posted to microsoft.public.access.forms
McHammer
external usenet poster
 
Posts: 4
Default How to prevent form from closing



"BruceM via AccessMonster.com" wrote:

It usually helps to post the actual code when there is a question about the
code. Why show them the X button (top right corner, not left, but I assume
you meant that) at all? You should be able to use the form's Property Sheet
to set Control Box to No (or set it to Yes, with MinMax buttons set to Yes
and Close Button to No if you want to keep the minimize and maximize buttons),
or you could set Border Style to None.

It may be possible, from what I can tell, to put something together to get
rid of the error message when the X button is clicked, but it would be a lot
of work for something that doesn't work as well as what you could have with
your own command buttons.

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...forms/201005/1

.

Yes, "X" on the right...
Form is modal popup type, no min/max button, dialog, ctrl box yes.
Here below the code

Public MyAction As Variant

I've added MyAction = "save" to Save button and MyAction = "Cancel" to
Cancel button On click events

Private Sub Form_Current()
MyAction = Null
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MyAction
Case "Cancel"
'do nothing
Case "Save"
'do nothing
Case Else
MsgBox "Push Save or Cancel", vbOKOnly
Cancel = True
Exit Sub
End Select
End Sub

In this way, if a new record is entered it's possible to "save & close" the
form or "cancel" a partial unwanted data and then close the form with "X"
button. If you try to close the form with unwanted data through "X" button,
Cancel = True should stop any other event, or at least it was my target.

  #4  
Old May 18th, 2010, 07:25 PM posted to microsoft.public.access.forms
BruceM via AccessMonster.com
external usenet poster
 
Posts: 448
Default How to prevent form from closing

Why fight the built-in Close button? Just set Close Button and/or Control
Box to No, and make your own button to close the form:

Me.Dirty = False
DoCmd.Close acForm, Me.Name

How are you performing the validation? It doesn't seem to be in the form's
Before Update event, which is the usual place for form-level validation. Or
are you validating at the table level (i.e. setting the Required property of
some fields to Yes)?

I assume the Cancel button has something like:
MyAction = Cancel
Me.Form.Undo

How about the Save button?

McHammer wrote:
It usually helps to post the actual code when there is a question about the
code. Why show them the X button (top right corner, not left, but I assume

[quoted text clipped - 7 lines]
of work for something that doesn't work as well as what you could have with
your own command buttons.


Yes, "X" on the right...
Form is modal popup type, no min/max button, dialog, ctrl box yes.
Here below the code

Public MyAction As Variant

I've added MyAction = "save" to Save button and MyAction = "Cancel" to
Cancel button On click events

Private Sub Form_Current()
MyAction = Null
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MyAction
Case "Cancel"
'do nothing
Case "Save"
'do nothing
Case Else
MsgBox "Push Save or Cancel", vbOKOnly
Cancel = True
Exit Sub
End Select
End Sub

In this way, if a new record is entered it's possible to "save & close" the
form or "cancel" a partial unwanted data and then close the form with "X"
button. If you try to close the form with unwanted data through "X" button,
Cancel = True should stop any other event, or at least it was my target.


--
Message posted via http://www.accessmonster.com

  #5  
Old May 19th, 2010, 09:48 AM posted to microsoft.public.access.forms
McHammer
external usenet poster
 
Posts: 4
Default How to prevent form from closing



"BruceM via AccessMonster.com" wrote:

Why fight the built-in Close button? Just set Close Button and/or Control
Box to No, and make your own button to close the form:

Me.Dirty = False
DoCmd.Close acForm, Me.Name

How are you performing the validation? It doesn't seem to be in the form's
Before Update event, which is the usual place for form-level validation. Or
are you validating at the table level (i.e. setting the Required property of
some fields to Yes)?

I assume the Cancel button has something like:
MyAction = Cancel
Me.Form.Undo

How about the Save button?

Here below my button on clickcommands. Some words are in Italian.

Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click
If IsNull(Me.NominativoCliente) Or IsNull(Me.DataChiamata) Or
IsNull(Me.Autorizzato) = True Then
Exit Sub
End If
'do nothing if the minimum importand data are filled in. I could complete
the others later in the main form.

MyAction = "Save"
If Me.NewRecord Or Right(Year(Me.DataChiamata), 4) Left(Me.Contatore, 2)
Then
DoCmd.RunCommand acCmdSaveRecord
Me.Contatore = fctAnnoNr(Me.DataChiamata)
End If
'this is to create an autonumber for new records (e.g format 20100001)
through a module
DoCmd.Close
Forms!Main.Requery
DoCmd.GoToRecord , , acLast
Exit_cmdSave_Click:
Exit Sub
Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click
End Sub

Private Sub cmdCancel_Click()
On Error GoTo Err_cmdCancel_Click
MyAction = "Cancel"
If MsgBox("Vuoi annullare il record?", vbYesNo, "Annullare?") = vbYes Then
Me.Undo
Cancel = True
Me.Requery
End If
Exit_cmdCancel_Click:
Exit Sub
Err_cmdCancel_Click:
MsgBox Err.Description
Resume Exit_cmdCancel_Click
End Sub

It would be easier to use an Exit button on the form. I tryed this solution
just to have the same style of all other forms that hasn't got an Exit
button. I'm quite a newbie in Access so I'll accept any advice to make the
job easier.
  #6  
Old May 19th, 2010, 01:01 PM posted to microsoft.public.access.forms
BruceM via AccessMonster.com
external usenet poster
 
Posts: 448
Default How to prevent form from closing

You could try something like this in the cmdSave Click event:

On Error GoTo cmdSave_Click_Error

If Me.Dirty Then Me.Dirty = False
Me.Contatore = fctAnnoNr(Me.DataChiamata)

ProcExit:
End Sub

cmdSave_Click_Error:
If Err.Number 2101 Then
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in cmdSave_Click, Form_Table1"
End If
Resume ProcExit

End Sub

Note the error trapping, which eliminates the useless (in this context) "The
setting you entered isn't valid for this property" error.


Then in the form's Before Update event, perform the validation:

If IsNull(Me.NominativoCliente) Or _
IsNull(Me.DataChiamata) Or _
IsNull(Me.Autorizzato) = True Then
MsgBox "Missing Data"
Cancel = True
End If

Or to be more specific:

If IsNull(Me.NominativoCliente) Then
MsgBox "NominativoCliente needs data"
Me.txtNomCliente.SetFocus
Cancel = True
Elseif IsNull(Me.DataChiamata) Then
MsgBox "DataChiamata needs data"
Me.txtDataChiamata.SetFocus
Cancel = True
ElseIf IsNull(Me.Autorizzato) Then
MsgBox "Autorizzato needs data"
Me.txtAutorizzato.SetFocus
Cancel = True
End If

I have suggested you eliminate the built-in Close button on the top right.
You have not responded to that suggestion. Is there a reason you don't want
to eliminate the button? It will be very difficult, at best, to eliminate
the built-in error message. It will be more difficult yet, if it is possible
at all, to choose to save the record at that point.

I don't understand this part:
If Me.NewRecord Or Right(Year(Me.DataChiamata), 4) Left(Me.Contatore, 2)

You seem to be performing more validation. If so, you don't need to test for
a new record. Access will try to save the record at some point anyhow. If
you need to check DataChiamata against Contatore before assigning the number
you could do:

If Right(Year(Me.DataChiamata), 4) Left(Me.Contatore, 2) Then
Me.Contatore = fctAnnoNr(Me.DataChiamata)
End If

This next part is confusing also:

DoCmd.Close
Forms!Main.Requery
DoCmd.GoToRecord , , acLast

Are you closing the form? If so, why have more code after closing the form?
Also, you could try:
Me.Refresh
instead of requery if necessary to see the new Contatore value. This should
eliminate the need to move to the last record (which you can't do anyhow
because the form is closed).



McHammer wrote:
Why fight the built-in Close button? Just set Close Button and/or Control
Box to No, and make your own button to close the form:

[quoted text clipped - 12 lines]

How about the Save button?


Here below my button on clickcommands. Some words are in Italian.

Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click
If IsNull(Me.NominativoCliente) Or IsNull(Me.DataChiamata) Or
IsNull(Me.Autorizzato) = True Then
Exit Sub
End If
'do nothing if the minimum importand data are filled in. I could complete
the others later in the main form.

MyAction = "Save"
If Me.NewRecord Or Right(Year(Me.DataChiamata), 4) Left(Me.Contatore, 2)
Then
DoCmd.RunCommand acCmdSaveRecord
Me.Contatore = fctAnnoNr(Me.DataChiamata)
End If
'this is to create an autonumber for new records (e.g format 20100001)
through a module
DoCmd.Close
Forms!Main.Requery
DoCmd.GoToRecord , , acLast
Exit_cmdSave_Click:
Exit Sub
Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click
End Sub

Private Sub cmdCancel_Click()
On Error GoTo Err_cmdCancel_Click
MyAction = "Cancel"
If MsgBox("Vuoi annullare il record?", vbYesNo, "Annullare?") = vbYes Then
Me.Undo
Cancel = True
Me.Requery
End If
Exit_cmdCancel_Click:
Exit Sub
Err_cmdCancel_Click:
MsgBox Err.Description
Resume Exit_cmdCancel_Click
End Sub

It would be easier to use an Exit button on the form. I tryed this solution
just to have the same style of all other forms that hasn't got an Exit
button. I'm quite a newbie in Access so I'll accept any advice to make the
job easier.


--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...forms/201005/1

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:16 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.