Retrieving data, looping over queries

chazewalker

New Member
Joined
Jun 1, 2011
Messages
1
hi! I'm new in here as well as in VBA programming...

I have an ADO connection on my excel to access, connection works fine but when i try to loop on the query, I got this error:

Run-time error '3705'
Operation is not allowed when the object is open.

I want the macro to retrieve data from access database by specific cells.
Help me plz.. :(

here's my code:

Code:
startRow = 7
here:

rs.Open "SELECT * FROM tblSample WHERE item_name = '" & ActiveSheet.Cells(startRow, 2) & "'"     '<<--here i got the error..
Do Until rs.EOF
   ActiveSheet.Cells(startRow, 2) = rs.Fields(2).Value
   ActiveSheet.Cells(startRow, 7) = rs.Fields(3).Value
   ActiveSheet.Cells(startRow, 6) = rs.Fields(5).Value
   rs.MoveNext
  startRow = startRow + 1
Loop

GoTo here

thanks in advance!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi,

Try ActiveSheet.Cells(startRow, 2).value

If still not good, add a debug.print & review the SQL

HTH

regards
 
Upvote 0
Another thought, does the code run OK without the WHERE clause?

SELECT * FROM tblSample
 
Upvote 0
You appear to be opening the recordset with rs.Open and then looping round and doing another rs.Open whilst the recordset is still open.

Close the recordset with an rs.Close before you loop round and try to open it again.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,600
Members
452,927
Latest member
whitfieldcraig

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