473,396 Members | 1,847 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,396 software developers and data experts.

How to check for SQL Errors

26
Background:
New to SQL
Front end - MS Access 2010
Back end - MS SQL Server 2008
________________________________

How is the best way to check if a SQL command works? For example:
Expand|Select|Wrap|Line Numbers
  1. zSQL = _
  2.     "INSERT INTO [dbo_DB02001] " & _
  3.         "(status,statusdate, buyer, supplier, mdDateTime, dWho,mdAction)" & _
  4.         " VALUES(" & _
  5.         "'" & Status & "'" & _
  6.         ", '" & StatusDate & "'" & _
  7.         ", '" & Me.fBuyer.Value & "'" & _
  8.         ", '" & Me.fSupplier.Value & "'" & _
  9.         ", '" & mdDateTime & "'" & _
  10.         ", '" & gblUser & "'" & _
  11.         ", '" & mdAction & "')"
  12.  'Debug.Print zSQL
  13.  DoCmd.SetWarnings False
  14. DoCmd.RunSQL zSQL
Can I insert some code to see if the command worked or not? It would be beneficial to display an error code or something via a MsgBox.
Thanks in advanced for you help.
Aug 26 '14 #1
4 1506
twinnyfo
3,653 Expert Mod 2GB
The best way is to run a test of the query and then open the table you are trying to modify. If it worked, you will find the record updated properly.
Aug 26 '14 #2
jforbes
1,107 Expert 1GB
I use the following:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SetWarnings False
  2. CurrentDb.Execute sSQL, dbFailOnError + dbSeeChanges
  3. DoCmd.SetWarnings True
This will throw an error that you can capture or if you rather you can let it bubble up to the user.
Aug 26 '14 #3
zmbd
5,501 Expert Mod 4TB
J' you do not need lines 1 and 2 in your code.
Line 2 should be altered to use an object variable instead of currentdb for example
Expand|Select|Wrap|Line Numbers
  1. ...
  2. set zdb = currentdb
  3. zdb.execute...
  4. ...
The other issue with Line 2 is that we do not know how CLSkcab has connected to the SQL-Server nor how CLSkcab is interacting with the server.
Line 2 doesgive the advantage that so long as you are not using a stored-procedure or a passthru query to the SQL-Server one can use the zdb.RecordsAffected to return the record count.
Aug 26 '14 #4
jforbes
1,107 Expert 1GB
Very true Zmdb. Using SetWarning is an old habit. An old habit that has no affect on an Execute statement and really shouldn’t be in the code that I posted.

I tend to use a global variable in place of CurrentDB, but didn’t want to include it as it might cause confusion. You are right though, that if a variable is used in place of CurrentDB, it gives the ability to check the RecordCount property after running and insert, update or delete query, were as using CurrentDB is a one time instance of the database connection and will always return a 0 on the RecordCount property.

Lastly, what I was attempting to demonstrate was that by using an Execute with the dbFailOnError option an error can be raised in Access that can be handled with an error handler.
Aug 26 '14 #5

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

Similar topics

11
by: mikey_boy | last post by:
Hello! Curious if anyone could give me a hand. I wrote this PHP script with makes a simple connection to a mysql database called firstdb and just pulls back the results and displays on the...
8
by: OHM | last post by:
I know this is not purely a VB.NET question, but I thought I would ask tho ! First create a typed-dataset with an integer ID primary-key field and a string Name field. Then create a form with a...
2
by: soccertl | last post by:
I wrote code that parses the db2diag.log to look for errors that would require us to generate a message to a service center indicating something is wrong. My problem is trying to figure out what...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
8
by: Simon Willison | last post by:
Hi all, I have an API design question. I'm writing a function that can either succeed or fail. Most of the time the code calling the function won't care about the reason for the failure, but...
25
by: JJ | last post by:
I only want to catch 404 errors at the application level (the rest are will be handled by the customerrors section of the web.config). How do I check for the error code in the Application_Error...
14
by: Martin Wells | last post by:
When I have errors in a program, whether they be compiler errors, linker errors, or if the program crashes when running, I have a list of things I check for initially. If I get an error for...
0
by: geethahathikal | last post by:
i'm getting the following errors while installing kfs.. i'm not able to install the required softwares for kfs thats is cmake, log4cpp. the errors i'm getting are.. error while installing...
55
by: lovecreatesbea... | last post by:
Do you check all error conditions for all library calls in you code? Is it necessary to check all errors? Is it convenient to check all errors (or how to make code clean and readable with mass of...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.