473,385 Members | 1,279 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.

Error Trap in VBA

171 100+
Hi All
I have a click event in an Access form which basically does three subs.
They are like:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Process_Click()
  2.  
  3. If MsgBox("Did you copy the data ?", vbCritical + vbYesNo, "Data") = vbNo Then
  4. Exit Sub
  5. Else
  6.  
  7. Process1
  8.  
  9. Process2
  10.  
  11. Process3
  12.  
  13.  
  14.  
  15. Me.lbl.Visible = False
  16. DoCmd.Hourglass False
  17.  
  18. If IsNull(DLookup("NotinMaster", "lcmstr", "NotinMaster")) Then
  19. DoCmd.OpenForm "FinalProcess"
  20. Else
  21. MsgBox "New codes found in data. Please update 'Codes' Table and process again", vbInformation + vbOKOnly, "New Codes"
  22. DoCmd.OpenForm "lcadd"
  23. Exit Sub
  24.  
  25. End If
  26.  
  27. End If
  28.  
  29. Me.lbl.Visible = False
  30. DoCmd.Hourglass False
  31.  
  32.  
  33. 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.
Jan 28 '15 #1

✓ answered by twinnyfo

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:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private fErr as Boolean
  5.  
  6. Private Sub MainSub()
  7. On Error GoTo EH
  8.     Process1
  9.     If fErr Then GoTo ExitSub
  10.     Process2
  11.     If fErr Then GoTo ExitSub
  12.     Process2
  13.     If fErr Then GoTo ExitSub
  14.  
  15. ExitSub:
  16.     Exit Sub
  17.  
  18. EH:
  19.     MsgBox "You had an error!" & vbcrlf & vbcrlf & _
  20.         Err.Number & ": " & Err.Description
  21.     Exit Sub
  22. End Sub
  23.  
  24. Private Sub Process1
  25. On Error GoTo EH:
  26.     'Your Code
  27.     Exit Sub
  28. EH:
  29.     MsgBox "You had an error!" & vbcrlf & vbcrlf & _
  30.         Err.Number & ": " & Err.Description
  31.     Exit Sub
  32. End Sub
Hope this hepps!

11 1517
twinnyfo
3,653 Expert Mod 2GB
rajeevs,

I always recommend adding error trapping/handling on all procedures within a project. The basics are as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub ThisIsMySub()
  2. On Error GoTo EH
  3.  
  4.     'Add all your Code here......
  5.  
  6.     Exit Sub
  7. EH:
  8.     MsgBox "There was an error ....!  " & _
  9.         "Please contact your Database Administrator.", vbOKOnly, "WARNING!"
  10.     Exit Sub
  11. 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.:

Expand|Select|Wrap|Line Numbers
  1. ExitMySub:
  2.     xlSheet.Application.ActiveWorkbook.Close
  3.     Set xlSheet = Nothing
  4.     Set xlApp = Nothing
  5.     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:

Expand|Select|Wrap|Line Numbers
  1. GoTo ExitMySub
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!
Jan 28 '15 #2
rajeevs
171 100+
Thank you twinnyfo
You are so kind as always.
I will try and let you know the progress
Jan 29 '15 #3
rajeevs
171 100+
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().
Jan 29 '15 #4
twinnyfo
3,653 Expert Mod 2GB
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 programmatically prevent the error from occurring.
Jan 29 '15 #5
rajeevs
171 100+
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.
Feb 12 '15 #6
twinnyfo
3,653 Expert Mod 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private fErr as Boolean
  5.  
  6. Private Sub MainSub()
  7. On Error GoTo EH
  8.     Process1
  9.     If fErr Then GoTo ExitSub
  10.     Process2
  11.     If fErr Then GoTo ExitSub
  12.     Process2
  13.     If fErr Then GoTo ExitSub
  14.  
  15. ExitSub:
  16.     Exit Sub
  17.  
  18. EH:
  19.     MsgBox "You had an error!" & vbcrlf & vbcrlf & _
  20.         Err.Number & ": " & Err.Description
  21.     Exit Sub
  22. End Sub
  23.  
  24. Private Sub Process1
  25. On Error GoTo EH:
  26.     'Your Code
  27.     Exit Sub
  28. EH:
  29.     MsgBox "You had an error!" & vbcrlf & vbcrlf & _
  30.         Err.Number & ": " & Err.Description
  31.     Exit Sub
  32. End Sub
Hope this hepps!
Feb 19 '15 #7
rajeevs
171 100+
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
Mar 4 '15 #8
twinnyfo
3,653 Expert Mod 2GB
I just noticed something in my code. A minor tweak to the Process Subs:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Process1
  2. On Error GoTo EH:
  3.     'Your Code
  4.     Exit Sub
  5. EH:
  6.     MsgBox "You had an error!" & vbcrlf & vbcrlf & _
  7.         Err.Number & ": " & Err.Description
  8.     fErr = True
  9.     Exit Sub
  10. End Sub
This sets the Boolean value of the Flag we created whenever there is an Error.

Also, add:

Expand|Select|Wrap|Line Numbers
  1. fErr = False
To the MainSub
Mar 4 '15 #9
NeoPa
32,556 Expert Mod 16PB
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.
Mar 4 '15 #10
twinnyfo
3,653 Expert Mod 2GB
Ahh, nice touch, NeoPa. Again, I am reminded of another useful trick!
Mar 4 '15 #11
rajeevs
171 100+
Thank you twinnyfo for the quick response. I will add the lines as mentioned. But the code is worked ok while tested.
Mar 4 '15 #12

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

Similar topics

1
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...
2
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....
13
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?
2
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...
5
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...
5
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...
1
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...
4
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)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
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...

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.