Hi All
I have a click event in an Access form which basically does three subs.
They are like: - Private Sub Process_Click()
-
-
If MsgBox("Did you copy the data ?", vbCritical + vbYesNo, "Data") = vbNo Then
-
Exit Sub
-
Else
-
-
Process1
-
-
Process2
-
-
Process3
-
-
-
-
Me.lbl.Visible = False
-
DoCmd.Hourglass False
-
-
If IsNull(DLookup("NotinMaster", "lcmstr", "NotinMaster")) Then
-
DoCmd.OpenForm "FinalProcess"
-
Else
-
MsgBox "New codes found in data. Please update 'Codes' Table and process again", vbInformation + vbOKOnly, "New Codes"
-
DoCmd.OpenForm "lcadd"
-
Exit Sub
-
-
End If
-
-
End If
-
-
Me.lbl.Visible = False
-
DoCmd.Hourglass False
-
-
-
End Sub
I like to stop the code execution if there is an error in any of the subs and pop up a message box.
The process1 sub opens an excel template to write some data. So if error occurred then kill excel application as well.
Hope I explained well.
11 1533 twinnyfo 3,653
Recognized Expert Moderator Specialist
rajeevs,
I always recommend adding error trapping/handling on all procedures within a project. The basics are as follows: - Private Sub ThisIsMySub()
-
On Error GoTo EH
-
-
'Add all your Code here......
-
-
Exit Sub
-
EH:
-
MsgBox "There was an error ....! " & _
-
"Please contact your Database Administrator.", vbOKOnly, "WARNING!"
-
Exit Sub
-
End Sub
With this basic template, you can do all sorts of things. For example, if your Process1 is to open Excel and performs some functions there, in the error handling portion, just add code to close your Excel Application there. You could also have another call, which some prefer to have, which closes everything out. I.e.: - ExitMySub:
-
xlSheet.Application.ActiveWorkbook.Close
-
Set xlSheet = Nothing
-
Set xlApp = Nothing
-
Exit Sub
Then, at any point in your sub, if you want to exit your Sub and close everything (without an error), then you just add:
Very simple, really. But it does take some skill to identify errors that will kill your project and just errors that will interrupt a Sub, but still allow you to continue.
Hope this hepps!
Thank you twinnyfo
You are so kind as always.
I will try and let you know the progress
Hi twinnyfo
Actually the error occurs always in process1. So I would like to stop the Process_Click() sub to stop and show a message to the user without proceeding to process2 and further. So could you tell me how can I put the error trap in Process_Click() .
twinnyfo 3,653
Recognized Expert Moderator Specialist
Use the templates in Post #2 and incorporate those into your code.
Keep in mind that I use most of my error trapping in the development of my code so that I can avoid any errors ever popping up. There are some errors that one cannot avoid, such as trying to find a file that does not exist, but that can be prevented as well.
The whole point is to determine what the Error that is occurring, then, build code into your project to prevent any errors (if at all possible).
So, in your instance, find out the error that is being tripped in Process1. Then you must figure out why it is causing an error, then programmaticall y prevent the error from occurring.
Dear twinnyfo
I was trying the error trap in the process1 sub as well as in the button click event. Still nothing happens. How can we stop the code running further from Process1 in the click event Process_Click() if there is an error in the process1 sub. I need to stop the Process_Click() and get the warning message.
twinnyfo 3,653
Recognized Expert Moderator Specialist
You could have a class-specific global Boolean variable that is set to true on the event of any error. Then, if Process1 generates an error, it returns to the main sub. As soon as you return to the main sub, check the status of the variable. If it is true, then exit the sub.
Example: - Option Compare Database
-
Option Explicit
-
-
Private fErr as Boolean
-
-
Private Sub MainSub()
-
On Error GoTo EH
-
Process1
-
If fErr Then GoTo ExitSub
-
Process2
-
If fErr Then GoTo ExitSub
-
Process2
-
If fErr Then GoTo ExitSub
-
-
ExitSub:
-
Exit Sub
-
-
EH:
-
MsgBox "You had an error!" & vbcrlf & vbcrlf & _
-
Err.Number & ": " & Err.Description
-
Exit Sub
-
End Sub
-
-
Private Sub Process1
-
On Error GoTo EH:
-
'Your Code
-
Exit Sub
-
EH:
-
MsgBox "You had an error!" & vbcrlf & vbcrlf & _
-
Err.Number & ": " & Err.Description
-
Exit Sub
-
End Sub
Hope this hepps!
Thank you twinnyfo
I have tried it and it is working fine now. Sorry for the delayed reply because I am still stuck with the other issue posted in a different thread. I will post the progress on the other issue in that thread
twinnyfo 3,653
Recognized Expert Moderator Specialist
I just noticed something in my code. A minor tweak to the Process Subs: - Private Sub Process1
-
On Error GoTo EH:
-
'Your Code
-
Exit Sub
-
EH:
-
MsgBox "You had an error!" & vbcrlf & vbcrlf & _
-
Err.Number & ": " & Err.Description
-
fErr = True
-
Exit Sub
-
End Sub
This sets the Boolean value of the Flag we created whenever there is an Error.
Also, add:
To the MainSub
NeoPa 32,567
Recognized Expert Moderator MVP
An alternative way to detect whether or not a procedure has fallen over an error is to use a Function procedure. That way you can pass back the information however you like. A Boolean can say whether or not, while a Long can return an error number or a String can return a message.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Warren |
last post by:
Below is the script. The problem is when I simulated the Oracle link
drop, my SQL2K never have to a chance to head to the GOTO section as it
dies with this error msg and exit. Any idea on a workround? Thanks.
Server: Msg 7399, Level 16, State 1, Procedure USP_Link_Check, Line 8
OLE DB provider 'MSDAORA' reported an error.
OLE DB error trace .
|
by: Rebecca Smith |
last post by:
I'd like to trap an error if a user inputs a bogus time. The input requires
a short time format. If the user enters someting like 20:78, Access will
catch that but I'd like something of my own.
Thank.
Rebecca
|
by: MLH |
last post by:
I'm thinking that maybe error trapping code could be
in a global module (standard procedure) and a Master
"OnError" statement could be set. Is anything like this
in use with VBA for A97?
|
by: Fox/Net West |
last post by:
Currently using .NET v1.1.4322 and v1.0.3705.
Starting a C# console application for the office - I wanted to <gracefully>
via structured error handling trap the condition where the .NET framework
(neither of the above) is not installed on the server/workstation - instead
of the default messagebox with the ok button that shows up - is there a way
for me to provide better messaging to the operator/user?
Tia,
|
by: rod.weir |
last post by:
Hello, I have the following code to iterate through each view in a SQL
Server and call the "sp_refreshview" command against it. It works
great until it finds a view that is damaged, or otherwise cannot be
refreshed. Then the whole routine stops working.
Can someone please help me re-write this code so that any views that
fail the "sp_refreshview" command get skipped. I'm sure it's just a
matter of putting some basic error trapping...
| |
by: Doug |
last post by:
Found this thread, but code is C# (would rather have VB.NET).
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q306355
Can anyone point me to a simple solution? We don't want the error
codes appearing to the end-user, but obviously we want to be able to
log and e-mail them, etc.
According to what I've been reading, the way to accomplish this is to
change the "CustomErrors" mode and the 'defaultRedirect' in web.config,
|
by: eric.nguyen312 |
last post by:
Hi, I'm trying to write an error trap that won't let you continue
unless a price is entered. This is what I have so far.
Private Sub ForecastProductSale_BeforeUpdate(Cancel As Integer)
If IsNull(Me.ForecastProductSale) Then
MsgBox "A Product Sale needs to be entered.", vbOKOnly
Exit Sub
End If
End Sub
|
by: eric.nguyen312 |
last post by:
Hi, I'm trying to write code that will not allow users to continue
unless a Product Sale is entered. This is what I have so far:
Private Sub ForecastProductSale_BeforeUpdate(Cancel As Integer)
If Nz(JobEstimatedProductSale, "") = "" Then
MsgBox "Warning: A Product Sale must be entered.", vbOKOnly,
"Warning"
Exit Sub
End If
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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,...
| |
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...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |