Pass a series of integer variables in VBScript to a SQL Server INSERT INTO
statement. This is part of a nightly-run script, handled by Windows Scheduler, to grab data from a Cerner-based system and insert it into a table in my web app's (we'll call it "MyApp's") own database. I'm currently running the script in "test mode," manually, through Command Prompt.
I get errors firing at On Error Resume Next
, and each time, I have the code WriteLine
to the log file: cstr(now) & " - Error occurred on [insert argument here]: " & Err.Description
.
However, every time, in every instance, Err.Description
is just an empty string and doesn't tell me what's going on. On top of that, Err.Number
gives me...nothing!
NOTE (12/30/21): It was suggested that my question is similar to this one; however, the accepted answer there is to use Err.Number
, which did not work for me, as it kept returning empty/null. My problem was with Err.Description
and Err.Number
not giving me any information in my log that I can work with.
Set DataConnExt = CreateObject("ADODB.Connection")
DataConnExt.CommandTimeout = 90
DataConnExt.ConnectionTimeout = 90
If testmode then 'tesmode = True, in this case
sDsn = "MyAppTST"
else
sDsn = "MyApp"
End If
sUser = "pseudonym"
sPWD = "***********"
On Error Resume Next
If testmode then
objErrLogFile.WriteLine " "
objErrLogFile.WriteLine cStr(now()) & " Error occurred on connection to MyApp_DB: "
objErrLogFile.WriteLine " " & Err.Description
End If
DataConnExt.Open "DSN=" & sDSN , sUser, sPWD
If testmode then
MidnightMailingLog.WriteLine cstr(now) & " - MyApp Export Query: " & sql
End If
errMessage = ""
If Err.Number <> 0 then
errMessage = Err.Description
errDesc = "ERROR occurred while executing export to MyApp DB: " & errMessage
LogError(errDesc)
SendErrorNotificationEmail
WScript.Quit 99
End If
DataConnExt.Close
Set DataConnExt = Nothing
rsExport.Close
Set rsExport = Nothing
On Error GoTo 0
12/28/2021 12:03:22 PM Error occurred on connection to MyApp_DB:
12/28/2021 12:03:22 PM - ERROR occurred while executing export to MyApp DB:
SendErrorNotificationEmail
runs correctly, as I do receive the email. However, it also has its own instance of On Error Resume Next
that does fire, and it too has the same commands to write cStr(now()) & " error occurred during email notification of script error: " & Err.Description
to the log file if Err.Number <> 0
, and the rest of the error log reads as follows:12/28/2021 12:03:22 PM error occurred during email notification of script error:
Credit to @DavidBrowne-Microsoft for helping me restructure some things and reduce redundant Error handling. Below is a revision of my code. The good news is, the INSERT statement is now successfully being executed when I run the script. The bad news: Err.Number
is still <> 0, and still no Description
.
One thing I DID learn is that WScript.Quit shouldn't have been there. The whole file is now structured so that my code runs last, and correctly adds its Errors to the error log; upon test-running, the only Errors found were my "phantom Errors."
The revised code:
' (Beginning of Sub -- Everything you see here is within an "If testmode" condition)
' ...
On Error Resume Next
ExportDataToMyApp(sqlA)
If Err.Number <> 0 then
errMessage = Err.Description & "; Err.Number: " & Err.Number
errDesc = "ERROR occurred while executing export to MyApp DB: " & errMessage
LogError(errDesc)
ErrCnt = ErrCnt + 1 'necessary for error logging
'SendErrorNotificationEmail 'don't worry about this one
Else
MidnightMailingLog.WriteLine cstr(now) & " - SQL Server INSERT to MyApp executed successfully."
End If
CloseObjects()
End Sub
' *********************************
Sub ExportDataToMyApp(ByRef sql)
Set DataConnExt = CreateObject("ADODB.Connection")
DataConnExt.CommandTimeout = 90
DataConnExt.ConnectionTimeout = 90
If testmode then
sDsn = "MyAppTST"
else
sDsn = "MyApp"
End If
sUser = "pseudonym"
sPWD = "***********"
DataConnExt.Open "DSN=" & sDSN , sUser, sPWD
' ACTUALLY, I THINK THE FIX FOR THE INSERT STATEMENT WAS HERE, BECAUSE I WAS REFERENCING THE WRONG sql VARIABLE (there are several in this vbs file).
Set rsExport = DataConnExt.Execute(sql,Recs,1)
End Sub
' **************************
Sub LogError(eDesc)
objErrLogFile.WriteLine " "
objErrLogFile.WriteLine(cstr(now()) & " - " & eDesc)
End Sub
' ***************************************
Sub CloseObjects()
rsExport.Close
Set rsExport = Nothing
DataConnExt.Close
Set DataConnExt = Nothing
End Sub
The error log:
12/28/2021 2:52:59 PM - ERROR occurred while executing export to MyApp DB:
David Browne was not kidding when he says that "Error handling is probably the worst thing about VBScript." It is a fickle, fickle creature.
Come to find out: there was no Error at all. Here are the steps I took to solve the problem:
ExportDataToMyApp
.On Error GoTo 0
after each time. It now handles DataConnExt.Open
and DataConnExt.Execute
separately.Select Case Err
, with only two cases: 0 and Else.CloseObjects
Sub within the Error handlers.' (Beginning of Sub -- Everything you see here is within an "If testmode" condition)
' ...
ExportDataToMyApp(sqlA)
End Sub
' *********************************
Sub ExportDataToMyApp(ByRef sql)
Set DataConnExt = CreateObject("ADODB.Connection")
DataConnExt.CommandTimeout = 90
DataConnExt.ConnectionTimeout = 90
If testmode then
sDsn = "MyAppTST"
else
sDsn = "MyApp"
End If
sUser = "pseudonym"
sPWD = "***********"
On Error Resume Next
DataConnExt.Open "DSN=" & sDSN , sUser, sPWD
Select Case Err
Case 0
MidnightMailingLog.WriteLine cstr(now) & " - Open Connection to MyApp_DB executed successfully."
Case Else
LogError(Err.Description)
ErrCnt = ErrCnt + 1
SendErrorNotificationEmail
On Error Goto 0
CloseObjects
End Select
On Error GoTo 0
On Error Resume Next
Set rsExport = DataConnExt.Execute(sql,Recs,1)
Select Case Err
Case 0
MidnightMailingLog.WriteLine cstr(now) & " - SQL Server INSERT to MyApp executed successfully."
Case Else
LogError(Err.Description)
ErrCnt = ErrCnt + 1
SendErrorNotificationEmail
On Error Goto 0
CloseObjects
End Select
On Error Goto 0
End Sub
Lo and behold, the error log file vanished from the folder, the database table was still accepting new rows, and all "success" lines were written to the log. Thank you to all who helped.