DAO: Access to Excel

Lekster07

New Member
Joined
Feb 2, 2011
Messages
15
Hi Everyone,

I'm trying to import data from Access to Excel with multiple criterias using DAO. I got this code (at least the lines I'd like to modify) from the internet:

Set rs = DB.OpenRecordset("SELECT * FROM " & _
TableName & " WHERE " & FieldName & _
" = 'MyCriteria'", dbReadOnly) ' filter records

I can't seem to modify this line where in I can refer to at least 2 criterias instead of 1 (MyCriteria).

The criterias I'm using is a range of date.

Thanks in advance!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Assuming your criteria are dates, something like this:-
Code:
[FONT=Courier New][COLOR=#0000ff]Set rs = DB.OpenRecordset("SELECT * FROM " & TableName & " " _[/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]    & "WHERE " & FieldName & "=#" & MyCriteria & "# " _[/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]    & "AND " & FieldName2  & "=#" & MyCriteria2 & "#;", _[/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]    dbReadOnly)[/COLOR][/FONT]

I suggest that you build the SQL string first and then pass that to the .OpenRecordset command - that way you can check the contents of the SQL string fairly easily;-
Code:
[FONT=Courier New][COLOR=#0000ff]Dim strSQL As String[/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][/COLOR][/FONT] 
[FONT=Courier New][COLOR=#0000ff]strSQL = "SELECT * FROM " & TableName & " " _[/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]    & "WHERE " & FieldName & "=#" & MyCriteria & "# " _[/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff]    & "AND " & FieldName2  & "=#" & MyCriteria2 & "#;"[/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][/COLOR][/FONT] 
[FONT=Courier New][COLOR=#0000ff]Debug.Print strSQL[/COLOR][/FONT]
[FONT=Courier New][COLOR=#0000ff][/COLOR][/FONT] 
[FONT=Courier New][COLOR=#0000ff]Set rs = DB.OpenRecordset(strSQL[/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff], [/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff]dbReadOnly)[/COLOR][/FONT]
 
Upvote 0
If you try this and it doesn't work, hit Ctrl-G to open the Immediate window and see what's in strSQL by typing ?strsql and pressing Enter.

Then get back to us and tell us:-
  • what's in MyCriteria and MyCriteria2 (whatever you call them)
  • what's in strSQL
  • what the SQL string is that you want to execute
 
Upvote 0
If you try this and it doesn't work, hit Ctrl-G to open the Immediate window and see what's in strSQL by typing ?strsql and pressing Enter.


Then get back to us and tell us:-
  • what's in MyCriteria and MyCriteria2 (whatever you call them)
  • what's in strSQL
  • what the SQL string is that you want to execute


Thanks for the response Ruddles! I did the same to what you have posted here but nothing happened when I run the code. I tried ?strsql but when I press enter, it's not giving me anything.

By the way, when I run the code, this is what I get in the immediate window:
Select * FROM testdb WHERE date_ans=#5/1/2011# AND date_ans=#5/2/2011#;
 
Upvote 0
Hi

I suspect that no records will satisfy the condition where date_ans equals two different values........it appears there is an issue with the criteria. Does one of the date_ans need to be another field?

Andrew
 
Upvote 0
Hi

I suspect that no records will satisfy the condition where date_ans equals two different values........it appears there is an issue with the criteria. Does one of the date_ans need to be another field?

Andrew


Hi Andrew! Thanks for the observation. I noticed that as well. I think I haven't really clarified what I wanted. Basically, my database table has one column for dates and I would want to filter records that satisfies a range of date (May 1 to May 2 for example) from that column and will give me data for the entire row (that satisfies the date criteria).

I can do this easily in Excel if my database is an Excel by just using Autofilter and then selecting the range of date I wanted, but I'm using an Access database.
 
Upvote 0
Ok - I haven't tested this but based on your last post, substitute the = signs for >= and <= respectively.

Andrew
 
Upvote 0
Change your date criteria to the following format

date_ans Between #1/1/2001# And #3/1/2003#

note the word between
 
Upvote 0
Sorry, I just got back!

Hopefully you understand why:-
Code:
Select * FROM testdb WHERE date_ans=#5/1/2011# AND date_ans=#5/2/2011#;
will never select any records?

[Pause for thought]

It's because no record can have date_ans=#5/1/2011# AND date_ans=#5/2/2011#. You actually want all records where date_ans=#5/1/2011# OR date_as=#5/2/2011#.

(The English language is very sloppy in that respect - SQL is not so forgiving, as you have discovered. VBA is similarly very demanding when it evaluates ANDs and ORs.)

Using:-
Code:
date_ans > = #5/1/2011# AND date_ans < = #5/2/2011#
will work, or:-
Code:
date_ans between #5/1/2011# AND =#5/2/2011#
or:-
Code:
date_ans in [#5/1/2011#, #5/2/2011#]

This is good if you plan to be using SQL at all: http://w3schools.com/sql/.
 
Upvote 0

Forum statistics

Threads
1,224,538
Messages
6,179,412
Members
452,912
Latest member
alicemil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top