megnin
Active Member
- Joined
- Feb 27, 2002
- Messages
- 340
Hi, I've tried adapting some VBA code from other posts and I'm not having any luck with this. I could really use some help.
I have a Stored Procedure in my SQL 2005 database called: sp_Count_All_YN_Answers_WTP
It contains these parameters:
@OneStop varchar(50) = '%',
@CaseManagerLName varchar(50) = '%',
@FromDate smalldatetime = '2000-05-08 12:35:29',
@ToDate smalldatetime = '2011-10-14 12:35:29',
@ReviewerName varchar(50) = '%',
@ReviewType varchar(50) = '%',
@Yes VarChar(1) = 'Y',
@No VarChar(1) = 'N'
I have some code working to just execute a Query in the VBA, but this one only gives me one count value and puts it in cell C6. That's all good, but now I need to execute this long stored procedure, with basically the same parameters as the SQL query, but the sp returns a three column table of data instead of just one value.
Above is the Stored Procedure information and below is the code I currently have which includes my connection string and such:
I'm not very good with the VBA code, so I appreciate all the help I can get. Thank you!!
I have a Stored Procedure in my SQL 2005 database called: sp_Count_All_YN_Answers_WTP
It contains these parameters:
@OneStop varchar(50) = '%',
@CaseManagerLName varchar(50) = '%',
@FromDate smalldatetime = '2000-05-08 12:35:29',
@ToDate smalldatetime = '2011-10-14 12:35:29',
@ReviewerName varchar(50) = '%',
@ReviewType varchar(50) = '%',
@Yes VarChar(1) = 'Y',
@No VarChar(1) = 'N'
I have some code working to just execute a Query in the VBA, but this one only gives me one count value and puts it in cell C6. That's all good, but now I need to execute this long stored procedure, with basically the same parameters as the SQL query, but the sp returns a three column table of data instead of just one value.
Above is the Stored Procedure information and below is the code I currently have which includes my connection string and such:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Sheet7
If Not Intersect(Target, Range("F1:F3, M1:M2")) Is Nothing Then
''Connection Variables:
Dim dbConnection As ADODB.Connection
Set dbConnection = New ADODB.Connection
Dim connStr As String
''Connection String (for SQL Database):
connStr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=SigmaTools;Data Source=BETASERVE;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=WFO-14433;Use Encryption for Data=False;Tag with column collation when possible=False"
dbConnection.ConnectionString = connStr
dbConnection.Open
''Recordset variables:
Dim rsData As ADODB.Recordset
Set rsData = New ADODB.Recordset
Dim sql As String
Dim Center As Range
Set Center = ActiveSheet.Range("I1")
Dim fromDate As Range
Set fromDate = ActiveSheet.Range("F2")
Dim toDate As Range
Set toDate = ActiveSheet.Range("H2") '("F3") "H2 is FromDate" plus one month. F3 is FromDate plus three months.
Dim Reviewer As Range
Set Reviewer = ActiveSheet.Range("M2")
Dim CaseManager As Range
Set CaseManager = ActiveSheet.Range("M1")
''SQL Query (for SQL Database):
sql = "SELECT COUNT(1) AS Mandatory " & _
"FROM WTP " & _
"WHERE (Mandatory = 'y') AND (OneStop ='" & Center & "') AND (ReviewDate BETWEEN '" & fromDate & "' AND '" & toDate & "')"
''Put the results of the Query into cell "C6". I need to put appropriate data in all the cells of the report.
With rsData
.ActiveConnection = dbConnection
.Open sql
If Not rsData.EOF Then
ActiveSheet.Range("C6").CopyFromRecordset rsData
End If
End With
'Cleanup
Set rsData = Nothing
Set dbConnection = Nothing
End If
End Sub
I'm not very good with the VBA code, so I appreciate all the help I can get. Thank you!!