473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Form closes before errors or notifications can be posted

Belimisimus
On close command I have code which ask me if I want to save changes before closing (yes, no, cancel).

Expand|Select|Wrap|Line Numbers
  1. Private Sub Close_Click()
  2.     bSaving = True
  3.     If Me.Dirty Then
  4.         answer = MsgBox("Do you want to save changes before closing?", vbYesNoCancel + vbQuestion, "Confirm Save")
  5.         If answer = vbNo Then
  6.             Me.Undo
  7.             DoCmd.Close acForm, Me.Name
  8.  
  9.         ElseIf answer = vbYes Then
  10.             On Error Resume Next
  11.             DoCmd.RunCommand acCmdSaveRecord
  12.             DoCmd.Close acForm, Me.Name
  13.         End If
  14.     Else
  15.         DoCmd.Close acForm, Me.Name
  16.     End If
  17.     bSaving = False
  18. End Sub

If I say yes then in before update there is a code which checks if all required fields are filled.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. If IsNull(Me![cbo_AssignedFrom]) Then
  3. MsgBox "You must enter who is Assign from before this Record can be saved"
  4. Cancel = True: Me![cbo_AssignedFrom].SetFocus
In case they aren't it notify me but still form is close. How to stop closing in that case?
Mar 26 '10 #1
6 2765
NeoPa
32,556 Expert Mod 16PB
The Cancel parameter in a Form_BeforeUpdate() event procedure simply specifies whether the Update should proceed. It has no effect on any related closing of the form. There wouldn't necessarily even be a related close if the save were called from elsewhere for instance.

Furthermore, there is no BeforeClose event to handle. The facility to cancel such a close is not supported. You need to ensure your code doesn't call for a close when it is not ready for one. You could do this by trying to save the record explicitly on dirty before the close. If the save failed (Check the Err object) then clearly you wouldn't want the close to proceed.
Mar 26 '10 #2
NeoPa
32,556 Expert Mod 16PB
By the way, your Close_Click() procedure closes even when the operator chooses Cancel (vbCancel). This makes little sense. If you don't want to offer the facility to cancel then use the vbYesNo option instead.
Mar 26 '10 #3
@NeoPa
The cancel option is working. It stops closing and there is no questions from before update code!?


You need to ensure your code doesn't call for a close when it is not ready for one. You could do this by trying to save the record explicitly on dirty before the close. If the save failed (Check the Err object) then clearly you wouldn't want the close to proceed.
I'm newbie about vba, and most of codes I find (learn) surfing the net... could you give me an example of code for this solution?
Mar 29 '10 #4
NeoPa
32,556 Expert Mod 16PB
@Belimisimus
You are quite right. My mistake. I misread your code.
@Belimisimus
What are you talking about? This is after a quote which doesn't mention this. I'm confused about what you're trying to say.
@Belimisimus
I'm happy to work with you, but I won't simply produce code for you. That said, if you make a try and tell me where you get confused or stuck I can certainly help.
Mar 29 '10 #5
I really appreciate your help and I didn't want from you to write a code instead of me. All I didn't know what the "Check the Err object" means so I ask for example (some times better way of explanation).

One way or another your solution was helpfull. Code below is working for me...hope that this is what you sugesst!

Expand|Select|Wrap|Line Numbers
  1. "On Error GoTo Close_Click_Err" 
  2. ...code...
  3. Close_Click_Exit:
  4.     Exit Sub
  5. Close_Click_Err:
  6.     'MsgBox Error$
  7.     Resume Close_Click_Exit
Thx again!
Mar 29 '10 #6
NeoPa
32,556 Expert Mod 16PB
That makes better sense. I will explain the Err object further (but for a better understanding simply type in "Err" on a new line in your code and press F1 for context-sensitive help).

Err is set after any executed piece of code that can fail. There are many possible failures, and Err will give a Number and Description for whatever caused the problem.

The following code gives a very brief illustration :
Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2. Call DoCmd.OpenForm(Forms(-1))
  3. If Err.Number = 2456 Then
  4.     Call MsgBox(Err.Description)
  5. End If
Mar 30 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now...
1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
10
by: Doug O'Leary | last post by:
Hey, all. I have a perl script which generates the html listed below. I cleaned it up a bit since the perl CGI module creates some really ugly html code. I've looked at this thing 8 ways to...
5
by: Navillus | last post by:
Hey gang, I have a login form that is empty by default, but can be filled with values from a previous form: <input type=text maxlength="40" size="40" name="user" value="`usr`"> <input...
7
by: Bob | last post by:
I have a winforms app written in Vs2005 Vb.Net, The setiings are to Enable the application Framework and I defined a splashform. Works fine if no errors occur. I do a checking on the mainform load...
3
by: steve | last post by:
Hi All I have a VB.net 2005 App which has a form set as the Application splash screen in Project properties Another form is set as the startup form All works great until the splash screen...
6
by: tony | last post by:
Hello! When exactly is it important or advisable to use this form load event handler compare to using the C-tor. For example here I create an event handler called dataBoundGridForm that is...
2
by: dkoco | last post by:
I have a C#.Net program that once a button is clicked it goes to a public class(called Tal) and gets data in a vector, then exits the class, and goes back to the main form(program). I will do this...
4
by: Jimmy | last post by:
Quick question for you. I'm using a small form for users to input a range of dates for a report (similar to the date range forms used in MS templates) that has an ok and a cancel button. How can I...
3
by: John Kotuby | last post by:
Hey guys, In IE7 where I am tesing my Web Apps written with VB in VS 2005 I have set the IE option: "Display Notification of all script errors" to be active. Now that I have changed some...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.