VBA / SQL: problem with dates

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO & SQL.
I'd like to update some fields with a defined criteria using the WHERE clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the whole
SQL string into access and did not work. But if I remove the date criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
Try wrapping the date in the FORMAT function so, instead of #15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.
 
Have you tried an unambiguous date string like "15-Jan-2005"?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Andy Wiggins said:
Try wrapping the date in the FORMAT function so, instead of #15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Santiago said:
Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO & SQL.
I'd like to update some fields with a defined criteria using the WHERE clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the whole
SQL string into access and did not work. But if I remove the date criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
My code usually collects a date from somewhere, assigns it to a variable and
then passes it on to something like the OP's query. I was interpretting the
example in those terms and not assuming the OP was actually using a
hard-coded date :-)

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Bob Phillips said:
Have you tried an unambiguous date string like "15-Jan-2005"?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Andy Wiggins said:
Try wrapping the date in the FORMAT function so, instead of #15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Santiago said:
Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO & SQL.
I'd like to update some fields with a defined criteria using the WHERE clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the whole
SQL string into access and did not work. But if I remove the date criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
I've tried both: with the format() function and also with unambiguous
dates... doesn't work...

It's quite annoying I must say... you work out your code and doesn't work
because of these format matters... :-(

Bob Phillips said:
Have you tried an unambiguous date string like "15-Jan-2005"?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Andy Wiggins said:
Try wrapping the date in the FORMAT function so, instead of #15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Santiago said:
Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO & SQL.
I'd like to update some fields with a defined criteria using the WHERE clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the whole
SQL string into access and did not work. But if I remove the date criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
:-(

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Santiago said:
I've tried both: with the format() function and also with unambiguous
dates... doesn't work...

It's quite annoying I must say... you work out your code and doesn't work
because of these format matters... :-(

Bob Phillips said:
Have you tried an unambiguous date string like "15-Jan-2005"?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Andy Wiggins said:
Try wrapping the date in the FORMAT function so, instead of #15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO
&
SQL.
I'd like to update some fields with a defined criteria using the WHERE
clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the whole
SQL string into access and did not work. But if I remove the date criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
I'll try assigning the contents of the cell previously to a Date variable,
but I'm not positive towards this... It's really pissing me off!

Andy Wiggins said:
:-(

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Santiago said:
I've tried both: with the format() function and also with unambiguous
dates... doesn't work...

It's quite annoying I must say... you work out your code and doesn't work
because of these format matters... :-(

Bob Phillips said:
Have you tried an unambiguous date string like "15-Jan-2005"?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Try wrapping the date in the FORMAT function so, instead of #15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO &
SQL.
I'd like to update some fields with a defined criteria using the WHERE
clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the
whole
SQL string into access and did not work. But if I remove the date
criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
Finally made it work!!!! a combination of lots of functions:

dim vdate as date

vdate = dateserial(year(cell), month(cell), day(cell))

sSQL = sSQL & "#" & format(vdate, "dd-mmm-yyyy") & "#"


Thanks all for the assistance
Bregards
Santiago


Santiago said:
I'll try assigning the contents of the cell previously to a Date variable,
but I'm not positive towards this... It's really pissing me off!

Andy Wiggins said:
:-(

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Santiago said:
I've tried both: with the format() function and also with unambiguous
dates... doesn't work...

It's quite annoying I must say... you work out your code and doesn't work
because of these format matters... :-(

:

Have you tried an unambiguous date string like "15-Jan-2005"?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Try wrapping the date in the FORMAT function so, instead of #15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO &
SQL.
I'd like to update some fields with a defined criteria using the WHERE
clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the
whole
SQL string into access and did not work. But if I remove the date
criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
:-)

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Santiago said:
Finally made it work!!!! a combination of lots of functions:

dim vdate as date

vdate = dateserial(year(cell), month(cell), day(cell))

sSQL = sSQL & "#" & format(vdate, "dd-mmm-yyyy") & "#"


Thanks all for the assistance
Bregards
Santiago


Santiago said:
I'll try assigning the contents of the cell previously to a Date variable,
but I'm not positive towards this... It's really pissing me off!

Andy Wiggins said:
:-(

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

I've tried both: with the format() function and also with unambiguous
dates... doesn't work...

It's quite annoying I must say... you work out your code and doesn't work
because of these format matters... :-(

:

Have you tried an unambiguous date string like "15-Jan-2005"?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Try wrapping the date in the FORMAT function so, instead of
#15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO
&
SQL.
I'd like to update some fields with a defined criteria using the
WHERE
clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the
whole
SQL string into access and did not work. But if I remove the date
criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
Even collected it can be formatted thus.

--

HTH

RP
(remove nothere from the email address if mailing direct)


Andy Wiggins said:
My code usually collects a date from somewhere, assigns it to a variable and
then passes it on to something like the OP's query. I was interpretting the
example in those terms and not assuming the OP was actually using a
hard-coded date :-)

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Bob Phillips said:
Have you tried an unambiguous date string like "15-Jan-2005"?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Andy Wiggins said:
Try wrapping the date in the FORMAT function so, instead of #15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO
&
SQL.
I'd like to update some fields with a defined criteria using the WHERE
clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the whole
SQL string into access and did not work. But if I remove the date criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
Santiago,

Humour me. Try

sSQL = sSQL & """" & format(vdate, "dd-mmm-yyyy") & """"

--

HTH

RP
(remove nothere from the email address if mailing direct)


Santiago said:
Finally made it work!!!! a combination of lots of functions:

dim vdate as date

vdate = dateserial(year(cell), month(cell), day(cell))

sSQL = sSQL & "#" & format(vdate, "dd-mmm-yyyy") & "#"


Thanks all for the assistance
Bregards
Santiago


Santiago said:
I'll try assigning the contents of the cell previously to a Date variable,
but I'm not positive towards this... It's really pissing me off!

Andy Wiggins said:
:-(

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

I've tried both: with the format() function and also with unambiguous
dates... doesn't work...

It's quite annoying I must say... you work out your code and doesn't work
because of these format matters... :-(

:

Have you tried an unambiguous date string like "15-Jan-2005"?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Try wrapping the date in the FORMAT function so, instead of
#15/01/2005#,
you have FORMAT(#15/01/2005#,"dd-mmm-yyyy").

It just might be that VBA or SQL (or both) are trying to recognise an
American date format rather than the English format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Hey guys,

I'm trying to update some data in an Access DB from Excel using ADO
&
SQL.
I'd like to update some fields with a defined criteria using the
WHERE
clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the
whole
SQL string into access and did not work. But if I remove the date
criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
I'll take your word for it, but my experience tells me to use format.

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-

Bob Phillips said:
Even collected it can be formatted thus.

--

HTH

RP
(remove nothere from the email address if mailing direct)


Andy Wiggins said:
My code usually collects a date from somewhere, assigns it to a variable and
then passes it on to something like the OP's query. I was interpretting the
example in those terms and not assuming the OP was actually using a
hard-coded date :-)

--
Andy Wiggins FCCA
www.BygSoftware.com
Excel, Access and VBA Consultancy
-
ADO
&
SQL.
I'd like to update some fields with a defined criteria using the WHERE
clause
in SQL, but dates seem not to work...

I use the SQL string like:

UPDATE
SET [field1] = 'aa', [field2] = #15/01/2005# etc... _
WHERE [field3] = 'bla' AND [field4] = #01/01/2005#

I believe that the problem is the date I'm filtering... I copied the
whole
SQL string into access and did not work. But if I remove the date
criteria
seems to work.

I appreciate your help.
Thanks & Bregards

Santiago
 
Andy said:
my experience tells me to use format.

My experience tells me to collect the data as a date value rather than
text (e.g. assign to a Date variable in VBA), use a dedicated OLE DB
provider of the correct version (e.g. OLE DB for Jet 4.0), use a
Command object with a dependent Parameter object typed appropriately
(e.g. adDate for a Jet 4.0 DATETIME column), if using Jet then possibly
use the ADOX.Procedures(<n>).Command method to create the Command and
Parameter objects for me, then assign the strongly-typed date value to
the stronly-typed Parameter object. In other words, I assume the OLE DB
provider is better at remembering rules on formats, type conversion,
SQL parsing, regional settings, etc than I will.
 
Bob said:
Santiago,

Humour me. Try

sSQL = sSQL & """" & format(vdate, "dd-mmm-yyyy") & """"

I'd go for the *international* standard format, which even Access/Jet
understands i.e.

sSQL = sSQL & "#" & Format$(vdate, "yyyy-mm-dd") & "#"
 
Jamie said:
which even Access/Jet understands

I meant to imply Jet and Access applications are US-centric. Apologies
to Access fans who I though I was implying that the non- entry level
ANSI-92 relational style view of what is little more than a file based
ISAM under the covers excuse for a DBMS that is Access/Jet is in some
way inferior <g>.
 
Another problem that can occur with dates results from the fact that
DATE field is actually a DATE&TIME field. Depending on how you forma
your output you might not see that a time is part of the value
Depending on how the date field gets populated you may get a time valu
as well. This will affect the selection as 1/1/5 10:15 is not the sam
as 1/1/5

Regard
 
tony said:
Another problem that can occur with dates results from the fact that a
DATE field is actually a DATE&TIME field.

Good point but you are preaching to the converted <g> e.g. here's a
typical bit of my DDL, note how I have constrained my DATETIME columns
only the minimum/maximum value (as appropriate) which the data type
allows:

CREATE TABLE Earnings (
pilot_ID CHAR(10) NOT NULL
REFERENCES Pilots (pilot_ID)
ON DELETE CASCADE
ON UPDATE CASCADE,
earnings_amt DECIMAL(19,4)
DEFAULT 0 NOT NULL,
start_date DATETIME NOT NULL,
CONSTRAINT start_date_value CHECK (
HOUR(start_date) = 0
AND MINUTE(start_date) = 0
AND SECOND(start_date) = 0),
end_date DATETIME NULL,
CONSTRAINT end_date_value CHECK (
HOUR(end_date) = 23
AND MINUTE(end_date) = 59
AND SECOND(end_date) = 59),
CONSTRAINT chrono_order
CHECK (start_date < end_date),
PRIMARY KEY (pilot_ID, start_date));

You see I am a fan *really* of Jet and I use it properly <g>.

Jamie.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top