?
Solved

If an insert statement does not run correctly

Posted on 2011-09-19
4
Medium Priority
?
294 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

764 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