ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how to format a date to be used in sqlstring in excel 2007- contd (https://www.excelbanter.com/excel-programming/403349-how-format-date-used-sqlstring-excel-2007-contd.html)

ragup001

how to format a date to be used in sqlstring in excel 2007- contd
 
Thanks, but how would you format the field and use it in sqlstring ??
Dim a as String
Dim b as String

a = Range("A1").Select
b = Range("A2").Select

(A1 contains 12/1/2006 and A contains 12/31/2006)

Is it this?? sqlstring = sqlstring #a#,#b#

Thanks in advance.


In any case I am getting an error. Any help is much appreciated.

Seems to me that whenever I have to deal with dates I have to use # before
and after the date. This may not be the case for you at all, but I'd try:
sqlstring = sqlstring & #12/1/2006# ,#12/31/2006#

"ragup001" wrote:

I have a startdate in cell a1 as 12/1/2006 and enddate in cell a2 as
12/31/2006. These are used as parameters for a sqlstring in excel 2007.
sqlstring = sqlstring & '12/1/2006' ,'12/31/2006'

Excel gives me a run-time error '1004' : SQL syntax error.

Can someone please tell me how to format the dates??

Thanks,

Ragup



sebastienm

how to format a date to be used in sqlstring in excel 2007- contd
 
Hi,
it depends on the database. MS Access uses the syntax
# <date #
as date literal. For most database you can enclose the date in single quote:
SELECT ... FROM ... WHERE invoiceDate = '12/1/2006'

eg:
dim sql as string
Dim strDate as String

strDate = Range("A1").Text
sql = "SELECT ... FROM .... WHERE InvoiceDate = '" & strDate & "'"

--
Regards,
Sébastien
<http://www.ondemandanalysis.com
<http://www.ready-reports.com


"ragup001" wrote:

Thanks, but how would you format the field and use it in sqlstring ??
Dim a as String
Dim b as String

a = Range("A1").Select
b = Range("A2").Select

(A1 contains 12/1/2006 and A contains 12/31/2006)

Is it this?? sqlstring = sqlstring #a#,#b#

Thanks in advance.


In any case I am getting an error. Any help is much appreciated.

Seems to me that whenever I have to deal with dates I have to use # before
and after the date. This may not be the case for you at all, but I'd try:
sqlstring = sqlstring & #12/1/2006# ,#12/31/2006#

"ragup001" wrote:

I have a startdate in cell a1 as 12/1/2006 and enddate in cell a2 as
12/31/2006. These are used as parameters for a sqlstring in excel 2007.
sqlstring = sqlstring & '12/1/2006' ,'12/31/2006'

Excel gives me a run-time error '1004' : SQL syntax error.

Can someone please tell me how to format the dates??

Thanks,

Ragup




All times are GMT +1. The time now is 10:22 AM.

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