tags:

views:

51

answers:

1

Hi,

I hv created a From Date & To Date in excel (Sheet1). Under cell A3, I hv From Date :: 01-01-2010 and cell A4 To Date :: 31-08-2010.

How do I link the Date Range to the below macro:-

Sub Sales()

Dim StrSQl As String

Con = "Provider=IBMDA400;Data Source=XXX.XXX.XXX.XXX;User Id=yyyy;Password=zzzz"

Set Db = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.recordset")
Db.connectionSTring = Con
Db.Open
StrSQl = "select myuc, sum (myac) as Amount from myabc.myqwerty where mydt >= 20100101 and mydt <= 20100831 group by (mycl)"
rs.Open StrSQl, Db, 3, 3
Sheet1.Cells(10, 1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
Set cn = Nothing
End Sub

Pls take note of the date format between excel & SQL statement. Both are different in formats.

Thanks.

+1  A: 

Perhaps:

FromDate=Format(Sheet1.Range("A3"),"yyyymmdd")
ToDate=Format(Sheet1.Range("A4"),"yyyymmdd")
StrSQl = "select myuc, sum (myac) as Amount from myabc.myqwerty where mydt >= "
    & FromDate & " and mydt <= " & ToDate & " group by (mycl)"
Remou
After inserted the above, the system prompted with an error. Run-time error '6': Overflow under FromDate=Format(Sheet1.Range("A3"),"yyyymmdd").
Bob
What, if anything, did you declare FromDate As? BTW, I tested before I posted.
Remou
It works as I hv changed the input format to yyymmdd from 01-01-2010. Just need to educate the users of the new format.
Bob
You should not have to change the format, the line `Format(Sheet1.Range("A3"),"yyyymmdd")` takes a date of any format, including 01-01-2010, and changes it to 20100101, which is what you show in your example SQL.
Remou
Bob
Clear the result from the sheet? You would need to select the range and delete it.
Remou