Solved

If an insert statement does not run correctly

Posted on 2011-09-19
4
268 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
  • 3
4 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modal form 11 31
unable to create table-based data macro in MS ACCESS 2013 11 25
User Level Security 6 39
Microsoft Access Write errors seem to be caused by bit fields 4 38
Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

825 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