Exporting a row in Access to Excel using the Range function in VBA

needhelpwithvba

New Member
Joined
Aug 5, 2011
Messages
4
I'm trying to export a specific row from Access into Excel. In this case, I have a code that looks to cell B2 and B3 in Excel to define the criteria of what to look for in Access. My code looks like this:

Sub NumberPets()

Const strDb As String = "C:\Pets.mdb"
'looks to cell B2 and B3 for criteria on #dogs and #cats
Const strQry As String = "SELECT * from [tblPets] WHERE ([tblPets].[Dogs]=Range("B2").Value) And ([tblPets].[Cats]=Range("B3").Value)"

Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection

Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Ace.OLEDB.12.0; Persist Security Info = False; " & "Data Source=C:\Pets.mdb;"
Set rs = New ADODB.Recordset

With rs
Set .ActiveConnection = cn
.Open strQry
End With
'pastes data to cell B4
Worksheets("Data").Range("B4").CopyFromRecordset rs

rs.Close: cn.Close
Set rs = Nothing: Set cn = Nothing
End Sub


I get an error message that says "Compile error: Syntax error." If I replace Range("B2").Value with 1 and Range("B3").Value with 0 the code works fine. Any help would be greatly appreciated!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Where and how are you replacing Range("B3") and Range("B2")?
 
Upvote 0
If I re-write this line of code:

Const strQry As String = "SELECT * from [tblPets] WHERE ([tblPets].[Dogs]=Range("B2").Value) And ([tblPets].[Cats]=Range("B3").Value)"

as

Const strQry As String = "SELECT * from [tblPets] WHERE ([tblPets].[Dogs]=1) And ([tblPets].[Cats]=0)"

the code works. The problem is that it no longer references cell B2 and B3, which was the whole purpose of the initial macro.
 
Upvote 0
You can't have a variable constant, so you'll have to change that to a variable and use:
Code:
Dim strQuery as string
strQry = "SELECT * from [tblPets] WHERE ([tblPets].[Dogs]=" & Range("B2").Value & ") And ([tblPets].[Cats]=" & Range("B3").Value & ")"
 
Upvote 0
Oops, I got it the wrong way round - it's including the ranges causing the problem isn't it?


To do that you would have to use a variable rather than a constant.
Rich (BB code):
Dim strQry As String 
 
 
strQry = " SELECT * "
strQry = strQry & " FROM [tblPets] "
strQry = strQry & " WHERE ([tblPets].[Dogs]=" & Range("B2").Value & ") AND([tblPets].[Cats]=" & Range("B3").Value & ")"
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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