[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

If an insert statement does not run correctly

Posted on 2011-09-19
4
Medium Priority
?
306 Views
Last Modified: 2012-05-12
I have written some VBA code that inserts information into a table.  I was curious if anyone could help me figure out how to catch the case that the insert fails, for any reason, and end the Sub so that everything else does not blow up.  Thanks for any help!
0
Comment
Question by:uconnfb13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 36561007
Run your insert using CurrentDB.execute, and use error handling:

On Error goto EH
CurrentDb.Execute strSQLYourInsertQuery, dbFailOnError

' Other code

' Thse should be at the very end of your function
Exit Sub ' (or function) <--- the point is to exit gracefully if there is no error

EH:
  msgbox "ERROR " & err.number & ": " & err.descriptiom
End Sub

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36561496
Typo in my code above...
<<  msgbox "ERROR " & err.number & ": " & err.descriptiom  >>

Should be:
msgbox "ERROR " & err.number & ": " & err.description

Also, If you don't want to see the actual error message, you could also do something along these lines for the error handler:

EH:
  msgbox "Insert failed"
End Sub
0
 

Author Closing Comment

by:uconnfb13
ID: 37097855
thank you!
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37097872
Glad to help :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question