473,804 Members | 4,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stop form closing after error checking

3 New Member
Hi
i am fairly new to vba and have a taxi booking database.
every time you book a job the cust_name field must be filled out and same if account job for account_ref and account_passwor d fields but everytime the message box appears and you select ok the form closes
does any body no of a solution the code for the ok button is:

Expand|Select|Wrap|Line Numbers
  1. Sub ClickOKButton()
  2.  
  3. Dim rsJobs As DAO.Recordset
  4. Dim rsCust As DAO.Recordset
  5. Dim NewCustId As Long
  6. Dim FromLoc
  7.  
  8. '***Error checking
  9. If IsNull(cust_name) Then
  10.   MsgBox "Name must be entered"
  11.   cust_name.SetFocus
  12.   Exit Sub
  13. ElseIf Len(cust_name) < 2 Then
  14.   MsgBox "Name must be minimum of 2 characters"
  15.   cust_name.SetFocus
  16.   Exit Sub
  17. ElseIf cboCashAccount = "Account" Then
  18.   If IsNull(account_reference) Then
  19.     MsgBox "Account reference must be entered because this is an account job"
  20.     account_reference.SetFocus
  21.     Exit Sub
  22.     ElseIf cboCashAccount = "Account" Then
  23.       If IsNull(account_password) Then
  24.       MsgBox "Account password must be entered because this is an account job"
  25.       account_password.SetFocus
  26.       Exit Sub
  27.     End If
  28.   End If
  29. End If
  30. '***Error checking end
  31.  
  32.  
  33. 'Add customer record if new customer
  34. NewCustId = 0
  35. If Not chkExistingCustomer Then 'match not found
  36.   Set rsCust = CurrentDb.OpenRecordset("CUSTOMERS", dbOpenDynaset)
  37.   rsCust.AddNew
  38.   If Left(phone_number, 2) = "07" Then
  39.     rsCust("mobile_phone") = phone_number
  40.   Else
  41.     rsCust("home_phone") = phone_number
  42.   End If
  43.   rsCust("cust_name") = cust_name
  44.   rsCust("premises") = premises
  45.   rsCust("house_number") = house_number
  46.   rsCust("flat") = Flat
  47.   rsCust("street") = street
  48.   rsCust("area") = area
  49.   rsCust("town") = town
  50.   rsCust("postcode") = postcode
  51.   rsCust("account_reference") = account_reference
  52.   rsCust.Update
  53.   rsCust.MoveLast
  54.   NewCustId = rsCust("cust_id")
  55. Else 'existing customer
  56.   Set rsCust = CurrentDb.OpenRecordset("SELECT * FROM CUSTOMERS WHERE cust_id = " & cust_id, dbOpenDynaset)
  57.   rsCust.Edit
  58.   If Left(phone_number, 2) = "07" Then
  59.     rsCust("mobile_phone") = phone_number
  60.   Else
  61.     rsCust("home_phone") = phone_number
  62.   End If
  63.   rsCust("cust_name") = cust_name
  64.   rsCust("premises") = premises
  65.   rsCust("house_number") = house_number
  66.   rsCust("flat") = Flat
  67.   rsCust("street") = street
  68.   rsCust("area") = area
  69.   rsCust("town") = town
  70.   rsCust("postcode") = postcode
  71.   rsCust("account_reference") = account_reference
  72.   rsCust.Update
  73. End If
  74. Set rsCust = Nothing
  75.  
  76. 'Add job record
  77. Set rsJobs = CurrentDb.OpenRecordset("JOBS", dbOpenDynaset)
  78. rsJobs.AddNew
  79. If NewCustId = 0 Then 'existing customer
  80.   rsJobs("cust_id") = cust_id
  81. Else 'new customer
  82.   rsJobs("cust_id") = NewCustId
  83. End If
  84. rsJobs("when_logged") = when_logged
  85. rsJobs("when_needed") = when_needed_Date & " " & Format(when_needed, "hh:nn")
  86. If from_location = to_location Then
  87.   FromLoc = from_location & " (W/R)"
  88. Else
  89.   FromLoc = from_location
  90. End If
  91. If cboVehicleType <> "car" Then
  92.   rsJobs("vehicle_type") = cboVehicleType
  93. End If
  94. rsJobs("from_location") = FromLoc
  95. rsJobs("to_location") = to_location
  96. If Not cboDelay = "ASAP" Then
  97.   rsJobs("mins_add_delay") = cboDelay
  98. Else
  99.   rsJobs("mins_add_delay") = 0
  100. End If
  101. rsJobs.Update
  102. Set rsJobs = Nothing
  103.  
  104. End Sub
how can i cancel the code after the error checking keeping the form open to make the changes
i have tried the docmd.canceleve nt and cancel=true but cant sort this out p
please can someone help

thanks liam
Dec 5 '10 #1
7 3708
gershwyn
122 New Member
How is the code being called? I assume it is the result of the clicking on a button somewhere. Is there any other code being executed as well? I don't see anything in the code you posted that would cause the form to close.
Dec 6 '10 #2
liam sheerin
3 New Member
Sorry the code was being called here:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOK_Click()
  2.  ClickOKButton
  3.  
  4.   DoCmd.Close
  5.   Form_Jobs.Requery
  6.  
  7. End Sub
I have put the error checking in this section and removed it from the job section.it now looks like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOK_Click()
  2.  
  3.  '***Error checking
  4.   If IsNull(cust_name) Then
  5.    MsgBox "Customer Name Must Be Entered !", vbQuestion, "CustomerName"
  6.    cust_name.SetFocus
  7.        Exit Sub
  8.   ElseIf Len(cust_name) < 2 Then
  9.     MsgBox "Name must be minimum of 2 characters", vbQuestion, "Customer Name"
  10.     cust_name.SetFocus
  11.     Exit Sub
  12.   ElseIf cboCashAccount = "Account" Then
  13.     If IsNull(account_reference) Then
  14.         MsgBox "Account reference must be entered because this is an account job", vbQuestion, "Account Reference"
  15.         account_reference.SetFocus
  16.         Exit Sub
  17.         ElseIf cboCashAccount = "Account" Then
  18.     If IsNull(account_password) Then
  19.         MsgBox "Account password must be entered because this is an account job", vbQuestion, "Account Password"
  20.         account_password.SetFocus
  21.         Exit Sub
  22.         End If
  23.         End If
  24.   End If
  25.   '***Error checking end
  26.   ClickOKButton
  27.  
  28.   DoCmd.Close
  29.   Form_Jobs.Requery
  30.  
  31. End Sub

It has sorted the problem i had the form now stays open bit of a strange one really.
thanks for your help
liam
Dec 6 '10 #3
NeoPa
32,579 Recognized Expert Moderator MVP
I can't see how this code would produce the result you describe. Are you simply clicking on an OK button on your form? Or is something else going on we're not told about?

You may want to look at Debugging in VBA to see if you can narrow down where we should be looking for the problem.
Dec 6 '10 #4
NeoPa
32,579 Recognized Expert Moderator MVP
Ah. It seems we overlapped in our posts.

From that information it's quite obvious what the problem was. You call ClickOKButton then, regardless of the outcome, you close the form with DoCmd.Close. Hardly surprising that it closes then ;-)
Dec 6 '10 #5
liam sheerin
3 New Member
Been stressing me out for a couple weeks now then its one of those problems you sit back and look at and you can see the problem
thanks again for all your inputs and help
liam
Dec 6 '10 #6
NeoPa
32,579 Recognized Expert Moderator MVP
No worries Liam. I'm glad it's sorted anyway.
Dec 6 '10 #7
Lysander
344 Recognized Expert Contributor
I use form validation code a lot and there is one other thing you might want to consider.

ATM, your code validates the form and either saves and closes if valid, or leaves the form open if invalid to correct.

All our data is entered by data-entry operators and if say, using your example, the account password is just not available, they have to close the form somehow, and move onto the next record.

All of our data entry forms have a button with the following code (titled 'Close without Saving')

Expand|Select|Wrap|Line Numbers
  1. If Me.Dirty Then
  2.     If MsgBox(strTranslate("TR000000132", "Closing this form will lose all changes. Are you sure?"), vbYesNo) = vbYes Then
  3.         Me.Undo
  4.         DoCmd.Close
  5.     Else
  6.         Exit Sub
  7.     End If
  8. Else
  9.     DoCmd.Close
  10. End If
  11.  
Dec 6 '10 #8

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

Similar topics

3
1895
by: Guy Robinson | last post by:
I have the code below which parses an expression string and creates tokens. Can anyone suggest the best of error checking for things like: Valid variable only obj.attribute -whitespace allowed test( "ff*2/dd.r..ss r") #additional ..ss -invalid variable. test( "ff*$24..55/ddr") #double .. and $ -invalid number test( "ff*2/dd.r.ss r") #variable with double . -invalid variable
2
4468
by: Marc S. Gibian | last post by:
I am putting together a Perl tool to manage a large set of tasks related to building a rather complex set of software. One of the goals for moving to Perl is to provide very rigorous error checking and handling. One area that has me puzzled is error checking some of the file system operations. Just simple things such as open, rename, unlink, print and some others ... all the sample code I've seen uses them in a form of: open(...) || die...
2
1605
by: Alistair | last post by:
hey peoples. I have a simple form (currently two fields) which is then passed to an ASP page thats stores the data in a dbase I have an error checking script which I can't get to work. the script is : <script language="JavaScript">
2
1727
by: khan | last post by:
Hi guys, I have a form called frminvoice, On this form I have a button to ptint report or invoice where report will get invoice number from !. Now I want when I click print invoice button, it should close form frminvoice. this is the code I have. but it is not closing the form. On Error GoTo Err_Command18_Click
6
10127
by: John S | last post by:
When the user clicks the "X" in the upper right-hand corner of a form, how can I stop the form from closing?
6
1680
by: **Developer** | last post by:
I've been looking but can't find out how in a form Closing event to know if the closing is because the form's "X" had been clicked or the main form's "X" was clicked. That is, I need to know if just the form is closing or the application is closing. Do you know how to tell?
16
2638
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project where someone suggested to me I was spending too much time writing error messages, and that I was therefore missing the benefit of using a scripting language. The idea, apparently, is that the PHP interpreter writes all the error messages that are...
2
3750
by: Mike | last post by:
Hello, Ok I have 2 classes in my project, one is the main form and one is a connection class, at a certain event on my main form a new instance is made of the connection class, and a reference to the main form is passed to its constructor. The connection class opens up a new thread and starts doing work in it, and adds collected data to the main form via a (cross-thread) control invoking delegate.
19
3229
by: zacks | last post by:
I have a .NET 2.0 MDI application where the child form has a Tab Control. Each of the Tab in the Tab Control has a Validating event to handle what it should do when the user changes tabs. But these Validating Events are also fired when either the child form or the main (parent) form Close icon is clicked. And I need for these events to know if they are being invoked because the app (or child window) is being closed. I have set a boolean...
2
1111
by: Shoaibkhattak | last post by:
Hy when i define a new form and attach this form at a buuton on old form and debug the application.so when i open new form by clicking button new form opens and i close it by close button of control box.but when i try to open 2nd time that form it not open.and vb.net show error in show.newfor. so i take a button in end of form and i code there me.hide.and false the control box.but i want if a person want to use close button on control box...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.