ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   SQL CASE statement on EXCEL VBA (https://www.excelbanter.com/excel-programming/334277-sql-case-statement-excel-vba.html)

Gilson

SQL CASE statement on EXCEL VBA
 
Hello folks ! How can I use CASE WHEN statement on excel vba ? I'm using
excel sheet like database I did :

dim db as dao.database
dim rs as dao.recordset
dim cSQL as string

set db=opendatabase(thisworkbook.path & "\" &
thisworkbook.name,false,false,"Excel 8.0")

cSQL="select cd_codigo, (case when cd_hist=1 then valor else 0 end), (case
when cd_hist=2 then valor else 0 end) from [Plan1$]"

set rs=db.openrecordset(cSQL)

but this code make a error. Why ?

regards.

David Lloyd[_3_]

SQL CASE statement on EXCEL VBA
 
My understanding is that the CASE statement is supported for SQL Server
T-SQL. DAO, of which the OpenDatabase method is a part, is developed for
JET workspaces as well as ODBCDirect workspaces. The default for DAO is a
connection to a JET datasource. JET SQL syntax does not support the CASE
SQL statement. One alternative would be to use an IIF statement instead.
For example:

IIF(cd_hist=1, valor, 0)

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


"Gilson" wrote in message
...
Hello folks ! How can I use CASE WHEN statement on excel vba ? I'm using
excel sheet like database I did :

dim db as dao.database
dim rs as dao.recordset
dim cSQL as string

set db=opendatabase(thisworkbook.path & "\" &
thisworkbook.name,false,false,"Excel 8.0")

cSQL="select cd_codigo, (case when cd_hist=1 then valor else 0 end), (case
when cd_hist=2 then valor else 0 end) from [Plan1$]"

set rs=db.openrecordset(cSQL)

but this code make a error. Why ?

regards.




All times are GMT +1. The time now is 08:29 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com