W.E.B
asked on
Excel 2007-VBA sql
Hello,
I'm trying to get a big select statement inside a VBA code,
Can you please help, I'm having some hard time.
Not sure IF it's ok to use this kind of Select inside !!
Option Explicit
Dim sh As Worksheet
Dim cnn As adodb.Connection
Public Sub GetSQLData()
Set sh = Sheets("Orders")
Dim rs As New adodb.Recordset
Dim sql As String
Set cnn = New adodb.Connection
cnn.Open "Provider=SQLOLEDB.1;User ID=sa; password=XXXXX!;Initial Catalog=CC;Data Source=sql-Laptop;"
sql = "SELECT Replace (Reference, ',', '') AS[Reference], Caller AS [Requester], CONVERT(varchar,SubTotalAm ount) as [Shipping Charges], "
case Tax1Name when 'GST' then Tax1Amount else 0 end as GST , case Tax1Name when 'HST_ON' then Tax1Amount else 0 end as HST_ON , case Tax1Name when 'HST_NS'then Tax1Amount else 0 end as HST_NS , case Tax1Name when 'HST_BC' then Tax1Amount else 0 end as HST_BC , case Tax2Name when 'TVQ' then Tax2Amount else 0 end as TVQ,
CONVERT(varchar,TotalAmoun t) AS [Total], CONVERT(varchar, FO.OrderDate, 1) AS [OrderDate], CONVERT(varchar,FO.OrderNo ) AS [OrderNumber], PickupCompanyName AS [Shipper Name], PickupContact AS [PU Contact], Replace (PickupStreet, ',', '') AS [Shipper Street],PickupCity AS [Shipper CITY],PickupProvince AS [Shipper Province],
PickupPostalCode AS [Shipper Postal Code],DeliveryCompanyName AS [Shipper Name],DeliveryContact AS [DEL Contact],Replace (PickupStreet, ',', '') AS [Delivery Street], DeliveryCity AS [Delivery City],DeliveryProvince AS [Delivery Province],DeliveryPostalCo de AS [Delivery Postal Code] From (select InvoiceNumber, orderno
From finalizedordersinvoices where invoicenumber = (select top 1 invoicenumber from finalizedordersinvoices i inner join finalizedorders o on o.orderno= i.orderno where Accountnumber in (45,7428,7429,7432,7433,78 65,9500,96 00,9601,96 02,9603,10 012,10013, 10015,3000 1) order by invoicenumber desc)) foi left join finalizedorders fo on fo.orderno = foi.orderno
WHERE AccountNumber in (45,7428,7429,7432,7433,78 65,9500,96 00,9601,96 02,9603,10 012,10013, 10015,3000 1) "
'Delete old data
sh.Range("A2:N65000").Clea rContents
'Get new data
rs.Open sql, cnn, adOpenDynamic, adLockOptimistic
sh.Range("A2").CopyFromRec ordset rs
rs.Close
cnn.Close
End Sub
I'm trying to get a big select statement inside a VBA code,
Can you please help, I'm having some hard time.
Not sure IF it's ok to use this kind of Select inside !!
Option Explicit
Dim sh As Worksheet
Dim cnn As adodb.Connection
Public Sub GetSQLData()
Set sh = Sheets("Orders")
Dim rs As New adodb.Recordset
Dim sql As String
Set cnn = New adodb.Connection
cnn.Open "Provider=SQLOLEDB.1;User ID=sa; password=XXXXX!;Initial Catalog=CC;Data Source=sql-Laptop;"
sql = "SELECT Replace (Reference, ',', '') AS[Reference], Caller AS [Requester], CONVERT(varchar,SubTotalAm
case Tax1Name when 'GST' then Tax1Amount else 0 end as GST , case Tax1Name when 'HST_ON' then Tax1Amount else 0 end as HST_ON , case Tax1Name when 'HST_NS'then Tax1Amount else 0 end as HST_NS , case Tax1Name when 'HST_BC' then Tax1Amount else 0 end as HST_BC , case Tax2Name when 'TVQ' then Tax2Amount else 0 end as TVQ,
CONVERT(varchar,TotalAmoun
PickupPostalCode AS [Shipper Postal Code],DeliveryCompanyName AS [Shipper Name],DeliveryContact AS [DEL Contact],Replace (PickupStreet, ',', '') AS [Delivery Street], DeliveryCity AS [Delivery City],DeliveryProvince AS [Delivery Province],DeliveryPostalCo
From finalizedordersinvoices where invoicenumber = (select top 1 invoicenumber from finalizedordersinvoices i inner join finalizedorders o on o.orderno= i.orderno where Accountnumber in (45,7428,7429,7432,7433,78
WHERE AccountNumber in (45,7428,7429,7432,7433,78
'Delete old data
sh.Range("A2:N65000").Clea
'Get new data
rs.Open sql, cnn, adOpenDynamic, adLockOptimistic
sh.Range("A2").CopyFromRec
rs.Close
cnn.Close
End Sub
ASKER
Hello,
I tried a simple Select top 10 * from FinalizedOrders where accountnumber in ..
I get the results, NO ERRORS.
If I run the String in SQL, i get the correct results, NO ERRORS.
thanks
I tried a simple Select top 10 * from FinalizedOrders where accountnumber in ..
I get the results, NO ERRORS.
If I run the String in SQL, i get the correct results, NO ERRORS.
thanks
Looks like you have an excess double quote at the end of the second line
sql = "SELECT Replace (Reference, ',', '') AS[Reference], Caller AS [Requester], CONVERT(varchar,SubTotalAm ount) as [Shipping Charges], "
Can you insert a debug line after the definition of your select, go to the immediate window in the VBA Editor (Ctrl+G), and copy the result into a new sql query in sql? That's usually how I spot issues in the string.
debug line code:
Thomas
sql = "SELECT Replace (Reference, ',', '') AS[Reference], Caller AS [Requester], CONVERT(varchar,SubTotalAm
Can you insert a debug line after the definition of your select, go to the immediate window in the VBA Editor (Ctrl+G), and copy the result into a new sql query in sql? That's usually how I spot issues in the string.
debug line code:
debug.print sql
Thomas
ASKER
Hi Thomas,
thanks for taking the time trying to help,
I tried to take the double quotes out, but , they keep popping up again.
when I try to run the debug, it says Compile error: syntax error.
case Tax1Name when 'GST' then Tax1Amount else 0 end as GST , case Tax1Name when 'HST_ON' then Tax1Amount else 0 end as HST_ON , case Tax1Name when 'HST_NS' then Tax1Amount else 0 end as HST_NS , case Tax1Name when 'HST_BC' then Tax1Amount else 0 end as HST_BC , case Tax2Name when 'TVQ' then Tax2Amount else 0 end as TVQ,
thanks for taking the time trying to help,
I tried to take the double quotes out, but , they keep popping up again.
when I try to run the debug, it says Compile error: syntax error.
case Tax1Name when 'GST' then Tax1Amount else 0 end as GST , case Tax1Name when 'HST_ON' then Tax1Amount else 0 end as HST_ON , case Tax1Name when 'HST_NS' then Tax1Amount else 0 end as HST_NS , case Tax1Name when 'HST_BC' then Tax1Amount else 0 end as HST_BC , case Tax2Name when 'TVQ' then Tax2Amount else 0 end as TVQ,
Try deleting the line feeds if you have any.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect,
thank you.for your time and help.
thank you.for your time and help.
ASKER
thank you very much
Glad to help,
Thomas
Thomas
Do you get the same error if you do a simpler select? Something like
sql="select orderno from finalizedorders"
if you run your sql string in a new query window in SQL Tools, do you get the right result?
Thomas