Running into an issue with a script I’ve been working on for some end-users, the relavant portion of which is pasted below.
In a nutshell, the script calls a stored procedure (append query) in an access database. This query is moving a row from one table to another(source to destination). The script will be run by an end-user, not myself, so simplification of the user experience is important. The annoyance that I'd like to solve deals with error checking.
When the script is run and the entry already exists in the destination table, I've been getting the very user-unfriendly message of "[Microsoft][ODBC Microsoft Access Driver] The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again." as a popup message. I'd like to capture this error and give an easier message to the user in the HTA GUI that's coming together for this script.
I added On Error Resume Next to the script and just after the call to the stored procedure I display Err.Number & " " & Err.Description. Here's where I'm having the issue. If the entry doesn't already exist in the dest table, then it is written from the src db and returns an Err.Number of 0. If the entry does exist in the destination then it still returns an Err.Number of 0, with the stuff in the paragraph above as the description. How can I trap this error in an If ... Then when the Err.Number is always 0?
I’ve been given a hint that this can be solved by using the extended error info in the connection object, but I’m really not sure how to go about doing this. Could anyone here who knows ADO well possibly provide some sample code to help me resolve this?
I suppose another way to take care of this would be to check if the record already exists in the destination before trying to run the stored procedure (append query). That would be in the same DB, in a table called Case_Tiff_Files, checking if the DBCaseFile variable exists in the column named CaseNumber. Thinking about it, this would probably be easier. Any help with sample code for doing this query and getting the resultant output (1 for exist and 0 for doesn’t exist I assume, but really don't know what it would return.) would be appreciated.
Thanks.
On Error Resume Next
Dim oConn, DBCaseFile, DBError
' Set File Number to UnPurge
DBCaseFile = Text1.value
DBError = err.Number
BlueMsg "Restoring Entry in CaseFile Database. This may take several minutes to complete."
Set oConn = CreateObject("ADODB.Connection")
oConn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=W:\Case_Files.mdb"
BlueMsg "Now connected to the Casefiles Database"
' execute query. Must have space after Procedure Name
oConn.Execute "exec RetPurgProc " & DBCaseFile
RedMsg DBError & " " & Err.Description
WhiteMsg "The entry for case number " & DBCaseFile & " has now been restored to the CaseFiles Database. It will be usable in Caseweb after the next Day-End run."
' Clean up
oConn.Close
' oConn.Quit
Set oConn = Nothing
In a nutshell, the script calls a stored procedure (append query) in an access database. This query is moving a row from one table to another(source to destination). The script will be run by an end-user, not myself, so simplification of the user experience is important. The annoyance that I'd like to solve deals with error checking.
When the script is run and the entry already exists in the destination table, I've been getting the very user-unfriendly message of "[Microsoft][ODBC Microsoft Access Driver] The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again." as a popup message. I'd like to capture this error and give an easier message to the user in the HTA GUI that's coming together for this script.
I added On Error Resume Next to the script and just after the call to the stored procedure I display Err.Number & " " & Err.Description. Here's where I'm having the issue. If the entry doesn't already exist in the dest table, then it is written from the src db and returns an Err.Number of 0. If the entry does exist in the destination then it still returns an Err.Number of 0, with the stuff in the paragraph above as the description. How can I trap this error in an If ... Then when the Err.Number is always 0?
I’ve been given a hint that this can be solved by using the extended error info in the connection object, but I’m really not sure how to go about doing this. Could anyone here who knows ADO well possibly provide some sample code to help me resolve this?
I suppose another way to take care of this would be to check if the record already exists in the destination before trying to run the stored procedure (append query). That would be in the same DB, in a table called Case_Tiff_Files, checking if the DBCaseFile variable exists in the column named CaseNumber. Thinking about it, this would probably be easier. Any help with sample code for doing this query and getting the resultant output (1 for exist and 0 for doesn’t exist I assume, but really don't know what it would return.) would be appreciated.
Thanks.
On Error Resume Next
Dim oConn, DBCaseFile, DBError
' Set File Number to UnPurge
DBCaseFile = Text1.value
DBError = err.Number
BlueMsg "Restoring Entry in CaseFile Database. This may take several minutes to complete."
Set oConn = CreateObject("ADODB.Connection")
oConn.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=W:\Case_Files.mdb"
BlueMsg "Now connected to the Casefiles Database"
' execute query. Must have space after Procedure Name
oConn.Execute "exec RetPurgProc " & DBCaseFile
RedMsg DBError & " " & Err.Description
WhiteMsg "The entry for case number " & DBCaseFile & " has now been restored to the CaseFiles Database. It will be usable in Caseweb after the next Day-End run."
' Clean up
oConn.Close
' oConn.Quit
Set oConn = Nothing