Samo
02-20-2016, 02:07 PM
Hi guys,
I am new to VBA and need your help with the following module, i can't figure out what the problem is despite everything seems fine to me, I have a worksheet in which i wrote a private sub in which i call the main sub routine (RunReport) from the below module, i have the SQL query within the worksheet and i run it from the module below, as the DEBUG shows, all goes well until the debug happens at this point (rs.Open Sql, conn) meaning there is no problem with SQL query at all or in any of the steps before this step, so what is the problem at this step? here is the module:
Option Explicit
Dim sql_string As String
Dim Sql As String
Dim setup_row As Integer
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sConnString As String
Sub RunReport ()
Application.ScreenUpdating=False
Call ConnectSqlServer
Call Build_SQL
rs.Open Sql, conn 'here is the debug, meaning there is no problem with the two sub routines above but it debugs here when i keep clicking F8**/
If not rs.EOF Then
Sheets("Data"). Range("A10").CopyFromRecordset rs
rs.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
If CBool(conn.Stste And adStateOpen) Then conn.Close
Set conn=Nothing
set rs =Nothing
End Sub
Sub Build_SQL
Sql=""
For setup_row = 3 To sheets("Setup").Range("A65536").End(x1Up).Row
If Sheets("Setup").Cells(setup_row, 2)= sql_string Then
Sql= Sql & Sheets("Setup").Cells(setup_row, 1) & Chr(13)
End If
Next setup_row
End Sub
Sub ConnectSqlServer()
sConnString ="Provider=xxxx;Integrated Security=SSPI; Persist Security Info=True;Data Source=xxxxx"
conn.Open sConnString
End Sub
*********
Thanks
Samo
I am new to VBA and need your help with the following module, i can't figure out what the problem is despite everything seems fine to me, I have a worksheet in which i wrote a private sub in which i call the main sub routine (RunReport) from the below module, i have the SQL query within the worksheet and i run it from the module below, as the DEBUG shows, all goes well until the debug happens at this point (rs.Open Sql, conn) meaning there is no problem with SQL query at all or in any of the steps before this step, so what is the problem at this step? here is the module:
Option Explicit
Dim sql_string As String
Dim Sql As String
Dim setup_row As Integer
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sConnString As String
Sub RunReport ()
Application.ScreenUpdating=False
Call ConnectSqlServer
Call Build_SQL
rs.Open Sql, conn 'here is the debug, meaning there is no problem with the two sub routines above but it debugs here when i keep clicking F8**/
If not rs.EOF Then
Sheets("Data"). Range("A10").CopyFromRecordset rs
rs.Close
Else
MsgBox "Error: No records returned.", vbCritical
End If
If CBool(conn.Stste And adStateOpen) Then conn.Close
Set conn=Nothing
set rs =Nothing
End Sub
Sub Build_SQL
Sql=""
For setup_row = 3 To sheets("Setup").Range("A65536").End(x1Up).Row
If Sheets("Setup").Cells(setup_row, 2)= sql_string Then
Sql= Sql & Sheets("Setup").Cells(setup_row, 1) & Chr(13)
End If
Next setup_row
End Sub
Sub ConnectSqlServer()
sConnString ="Provider=xxxx;Integrated Security=SSPI; Persist Security Info=True;Data Source=xxxxx"
conn.Open sConnString
End Sub
*********
Thanks
Samo