PDA

View Full Version : Solved: Problem with Dates



Cavaliere
05-26-2010, 04:25 AM
Hi everyone,

I'm trying to import data from Access table into Excel spreadsheet with the following code:


Private Sub CommandButton1_Click()
Dim dbDate As Date
dbDate = Worksheets("Sheet1").Range("Date").Value
Dim Connection As ADODB.Connection
Dim ResultSet As ADODB.Recordset

Set Connection = New ADODB.Connection
Connection.Open "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = G:\temp\Database1.mdb;"

Set ResultSet = New ADODB.Recordset

ResultSet.Open "SELECT * FROM USDQ WHERE Date='" & dbDate & "'", Connection, , , adCmdText

Worksheets("Sheet1").Range("e2").CopyFromRecordset ResultSet

End Sub


the problem is I cannot figure out date syntax and ...WHERE Date='" & dbDate & "'",... is not working. Date formats in excel and in access are the same. Could you please guide me with it.
Thanks in advance.

OBP
05-26-2010, 05:18 AM
You have to use the # symbol instead of the ' symbol.

Cavaliere
05-26-2010, 05:39 AM
It's not working, I've got the same Automation error.

OBP
05-26-2010, 07:29 AM
What error message do you get?

Cavaliere
05-26-2010, 07:34 AM
Run-time error '-2147217913 (80040e07)':
Automation error

OBP
05-26-2010, 07:58 AM
Perhaps it is not being caused by the Date, have you tried the automation without it?

Cavaliere
05-26-2010, 08:19 AM
Query without filter and when I filter by other field the script works fine without any errors

OBP
05-26-2010, 08:42 AM
Can you post the latest code that you used?

Cavaliere
05-26-2010, 08:54 AM
Here the code


Private Sub CommandButton1_Click()
Dim dbDate As Date
dbDate = Worksheets("Sheet1").Range("Date").Value
Dim Connection As ADODB.Connection
Dim ResultSet As ADODB.Recordset
Set Connection = New ADODB.Connection
Connection.Open "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = G:\temp\Database1.mdb;"
Set ResultSet = New ADODB.Recordset
ResultSet.Open "SELECT * FROM USDQ WHERE Date=#" & dbDate & "#", Connection, , , adCmdText
ActiveSheet.Range("e2").CopyFromRecordset ResultSet

End Sub

OBP
05-26-2010, 09:13 AM
Try putting a space between the = and the # and the # and the ".
Also ensure that your date is the correct format, you might need to use
Format(dbDate, "mm/dd/yyyy")
Which is US style.

Cavaliere
05-26-2010, 11:55 AM
Thanks a lot, problem solved. It turned out that whether excel or access or both are sensitive to date format and work ONLY with US style. I changed only my regional settings to US and the script worked which is odd for me.
OBP, thanks again)

geekgirlau
06-02-2010, 03:23 AM
Actually both Access and Excel are very US-centric when it comes to dates, regardless of what your regional settings are. I recommend always forcing a specific date format in your code.