DaveWillett
New Member
- Joined
- Apr 27, 2013
- Messages
- 1
Hi. I'm unsuccessful getting data between two cell references. The vba is as below and have tried several alternatives. I either get errors saying cannot convert to timestamp etc. Would a user be kind enough to assist? The code I'm trying to run is commented out below starting with:
'rst.Open Source:="SELECT * FROM DBA.INVOICE_DETAIL_VIEW WHERE InvDate >= '" & _
' Format(Worksheets("Dashboard").Range("I3").Value) & "' AND InvDate <= '" & _
' Format(Worksheets("Dashboard").Range("I4").Value) & "'", _
' ActiveConnection:=cnMTPS, Options:=adCmdText
'rst.Open Source:="SELECT * FROM DBA.INVOICE_DETAIL_VIEW WHERE InvDate >= '" & _
' Format(Worksheets("Dashboard").Range("I3").Value) & "' AND InvDate <= '" & _
' Format(Worksheets("Dashboard").Range("I4").Value) & "'", _
' ActiveConnection:=cnMTPS, Options:=adCmdText
Code:
Sub RunInvoiceData() Dim cnMTPS As ADODB.Connection
Dim rst As ADODB.Recordset
Dim i As Long
Dim SQL1 As String
Dim RowNumber As Integer
RowNumber = Worksheets("InvoiceData").Range("A65536").End(xlUp).Row
'RowNumber = ActiveSheet.Range("A65536").End(xlUp).Row
Range("InvoiceData!A1:CC0" & RowNumber).ClearContents
' Open connection to database
Set cnMTPS = New ADODB.Connection
cnMTPS.Open ConnectionString:="autoclaim"
' Open recordset on table
Set rst = New ADODB.Recordset
rst.Open Source:="SELECT * FROM DBA.INVOICE_VIEW where datediff(month, InvDate, GETDATE()) < " & Worksheets("Dashboard").Range("E4").Value, _
ActiveConnection:=cnMTPS, Options:=adCmdText
'rst.Open Source:="SELECT * FROM DBA.INVOICE_DETAIL_VIEW WHERE InvDate >= '" & _
' Format(Worksheets("Dashboard").Range("I3").Value) & "' AND InvDate <= '" & _
' Format(Worksheets("Dashboard").Range("I4").Value) & "'", _
' ActiveConnection:=cnMTPS, Options:=adCmdText
' Enter field names in first row
For i = 1 To rst.Fields.Count
Worksheets("InvoiceData").Cells(, i).Value = rst.Fields(i - 1).Name
Next i
' Copy records to worksheet, starting at cell A2
Worksheets("InvoiceData").Range("A2").CopyFromRecordset rst
' Close the recordset and the connection
rst.Close
cnMTPS.Close
Set cnMTPS = Nothing
End Sub