473,408 Members | 1,821 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,408 software developers and data experts.

Stop form closing after error checking

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_password 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.cancelevent and cancel=true but cant sort this out p
please can someone help

thanks liam
Dec 5 '10 #1
7 3678
gershwyn
122 100+
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
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,556 Expert Mod 16PB
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,556 Expert Mod 16PB
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
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,556 Expert Mod 16PB
No worries Liam. I'm glad it's sorted anyway.
Dec 6 '10 #7
Lysander
344 Expert 100+
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
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...
2
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...
2
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...
2
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...
6
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
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...
16
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...
2
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...
19
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...
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.