Here is my code. If you notice, down within my CrossingFiles function, where I set up my strSQL, I am also performing a select [variable]. The variable (in SQL) will display my error message (set up in the stored procedure). How can I get the results of the error message within
VB and then post each message (For each execution of the stored proc) in an error log? Is it possible?
Dim FileToOpen As String
Private Sub Form_Load()
Dim MyXL As Excel.Application 'create MyXL
Set MyXL = New Excel.Application
Dim WkbkName As String
On Error GoTo ErrorHandler
Set MyXL = GetObject(, "Excel.Application")
If Err.Number = 429 Then
Set appExcel = CreateObject("Excel.Application")
End If
FileToOpen = Application.GetOpenFilename 'Opens up explorer
If FileToOpen = "False" Or FileToOpen = "" Then
MsgBox "Ensure file is chosen correctly"
Else
Workbooks.Open FileName:=FileToOpen 'Opens up the file selected
Application.Visible = True 'in Excel
End If
Application.ActiveWorkbook.Save
Call CrossingFiles 'Calls function
Application.ActiveWorkbook.Save
Application.DisplayAlerts = False
Application.ActiveWorkbook.Close
Set MyXL = Nothing
Unload OpenFile
ErrorHandler: ' Error-handling routine.
Dim StrErr As String
StrErr = Err.Number & " - " & Err.Description
If Err = 364 Then
Exit Sub
End If
MsgBox (StrErr), vbOKOnly, Error
End Sub
Sub CrossingFiles()
Dim wsData As Worksheet
Dim fund As String
Dim trans_type As String
Dim security_id As String
Dim shares As String
Dim strRangeA As String
Dim strRangeB As String
Dim strRangeC As String
Dim strRangeE As String
Dim strSQL As String
Dim i As Integer
Dim adoConn As ADODB.Connection
Set wsData = ActiveSheet
Set adoConn = New ADODB.Connection
i = 1
wsData.Rows(1).Delete
adoConn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=ODBCsrc;Initial Catalog=main"
adoConn.Open
'This will use the new worksheet as the 'trading ground' and will create all of the values
'which will then be copied over to the original D column
Dim MyColumns_Range As Range
Set MyColumns_Range = Range(wsData.Cells(1, "A"), wsData.Cells(1, "A").End(xlDown))
For Each c In MyColumns_Range
strRangeA = "A" & i
strRangeB = "B" & i
strRangeC = "C" & i
strRangeE = "E" & i
fund = Range(strRangeA).Value
trans_type = Range(strRangeB).Value
security_id = Range(strRangeC).Value
shares = Range(strRangeE).Value
strSQL = "declare @flag varchar(1), @desc varchar(255) "
strSQL = strSQL + "EXECUTE stored_proc '" & fund & "', '" & trans_type & "', '" & security_id & "', '" & shares & "', '@flag output', '@desc output' "
strSQL = strSQL + "select @desc"
adoConn.Execute strSQL, , adCmdText
i = i + 1
fund = " "
trans_type = " "
security_id = " "
shares = " "
Next c
Set wsData = Nothing
adoConn.Close
'deletes the newly created worksheet and sets everything to original status
'yet with new data
Application.DisplayAlerts = False
ActiveWorkbook.Save
Application.DisplayAlerts = True
End Sub