M
Mangler
I have successfully figured out how to retrieve data from a SQL server
from within an excel sheet. The thing I can figure out is how I can
use variables in that recordset from data in a particular cell.
Example : Select * from inventoryAudit
*that works currently in my sheet but I need to add a variable to the
query
Example : Select * from inventoryAudit Where transfer_dte Between
varStart And varEnd
* varStart & varEnd would be dates entered into 2 cells on the sheet,
than a button would be pushed that would run the query and return the
results into the sheet
This is what I have so far without variables and it works, can someone
help me get the variables working?
Sub Add_Results_Of_ADO_Recordset()
'This was set up using Microsoft ActiveX Data Components version
2.8
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stSQL As String
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnStart As Range
Const stADO As String = "Provider=SQLOLEDB.
1;Password=PASSWORD;User ID=USER;" & _
"Persist Security Info=True;" & _
"Initial Catalog=DATABASE;" & _
"Data Source=SERVER"
Set wbBook = ActiveWorkbook
Set wsSheet = wbBook.Worksheets(1)
With wsSheet
Set rnStart = .Range("A2")
End With
stSQL = "SELECT p.phoneModel, p.partDescription , i.quantity ,
p.sku FROM dbo.partsListing p INNER JOIN partsInventory i ON p.sku =
i.sku WHERE isActive = 1"
Set cnt = New ADODB.Connection
With cnt
.CursorLocation = adUseClient
.Open stADO
.CommandTimeout = 0
Set rst = .Execute(stSQL)
End With
rnStart.CopyFromRecordset rst
'Cleaning up.
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
End Sub
from within an excel sheet. The thing I can figure out is how I can
use variables in that recordset from data in a particular cell.
Example : Select * from inventoryAudit
*that works currently in my sheet but I need to add a variable to the
query
Example : Select * from inventoryAudit Where transfer_dte Between
varStart And varEnd
* varStart & varEnd would be dates entered into 2 cells on the sheet,
than a button would be pushed that would run the query and return the
results into the sheet
This is what I have so far without variables and it works, can someone
help me get the variables working?
Sub Add_Results_Of_ADO_Recordset()
'This was set up using Microsoft ActiveX Data Components version
2.8
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stSQL As String
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnStart As Range
Const stADO As String = "Provider=SQLOLEDB.
1;Password=PASSWORD;User ID=USER;" & _
"Persist Security Info=True;" & _
"Initial Catalog=DATABASE;" & _
"Data Source=SERVER"
Set wbBook = ActiveWorkbook
Set wsSheet = wbBook.Worksheets(1)
With wsSheet
Set rnStart = .Range("A2")
End With
stSQL = "SELECT p.phoneModel, p.partDescription , i.quantity ,
p.sku FROM dbo.partsListing p INNER JOIN partsInventory i ON p.sku =
i.sku WHERE isActive = 1"
Set cnt = New ADODB.Connection
With cnt
.CursorLocation = adUseClient
.Open stADO
.CommandTimeout = 0
Set rst = .Execute(stSQL)
End With
rnStart.CopyFromRecordset rst
'Cleaning up.
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
End Sub