Access 2013 Loop Through Recordset

xljunkie

Board Regular
Joined
May 20, 2011
Messages
92
I keep getting error messages when using

Code:
Dim rs as DAO.Recordset
Set rs = Currentdb.OpenRecordset("tblClients")
rs.MoveFirst
Do Until rs.EOF = True
     'Code
Loop
Set rs = Nothing


I assume this is because I'm on Access 2013 and in my references Microsoft DAO 3.6 is unchecked, and when I check it, I get an error in loading the DLL. What is the solution to this issue? Do I need to save the db as an .mdb rather than my current .accdb format? I wanted to keep the accdb format in order to have nicer looking forms/graphics, but it's not needed.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Well I will need to run multiple processes. I have a table called tblClients which lists out a client number, name, etc. This list will grow over time so I am trying to find a way to automate some processes.

For one, I want to update other query criteria based on the number of clientid's found in tblClients. I've already found a solution to my original problem of creating a string of clientid's (1 or 2 or 3 or 4 ...) using a for loop. I added an autonumber to the table which I can use to dcount and use a for loop the specified number of times.

Anyway, my next problem is now updating query criteria using this string (1 or 2 or 3 or 4 ...). I'm seeing I can create a function which can be referenced within the query criteria, but I anticipate later on when I need to do some other processes, that having the ability to just loop through the records might be helpful.
 
Upvote 0
Are you going to use a concatenated, delimited string of all client IDs as criteria in a query?
 
Upvote 0
No, when you enter criteria in a query and you want to limit a dataset to a specific set of client ID's, you'd need to put a phrase such as this in the criteria field: 1 Or 2 Or 3 Or 4. So the criteria is not delimited. I've stored that particular string in a VBA variable, and I believe I can use a public function to place that criteria in a query.

But later on, I will need to export individual Excel files for each client, run a separate Excel macro from Access, save and close the Excel file, and move on to the next client. I've done this process before using an .mdb database. I don't remember if I needed the DAO object library for it. I will need to check tomorrow.

Anyway, if I do need the DAO object library to accomplish all the Excel work from Access, I will work on converting my db into the .mdb format tomorrow. I was just hoping I could achieve everything I need to do from the Access 2013 .accdb version. I'm not an expert or MVP-level programmer, but I've created a number of lengthy, automated processes in the past.
 
Upvote 0
I get an error in loading the DLL
You should let us know what the error number and message text is. To dimension a variable as a DAO recordset, you must have a reference to that library. If all else fails, you can try not specifying DAO and let Access figure it out (this is late binding). e.g. Dim rs as Recordset. The file extension is not your problem, so accdb should work fine.

creating a string of clientid's (1 or 2 or 3 or 4 ...) using a for loop
IN (1,2,3,4...) is how I would do it - if I were employing your method, but I would not rely on a count of id's to decide on how many times to loop. Autonumbers can get out of wack, especially in a shared db. What if the numbers ever become 1,2,3,6 (no 5 anymore, for example). Record deletion is not the only reason these numbers can lead you astray, so please don't focus on that. A better method is to open a recordset on a query object or on a vba sql statment and let that govern how many times to loop. Also, there is something to be said for using a function to return a sql statement if it makes sense.
Code:
For one thing, you are missing a MoveNext statement
It's probably in the 'Code section you omitted?
some minor code suggestions:
Code:
Dim rs as DAO.Recordset 
Set rs = Currentdb.OpenRecordset("tblClients") 
If rs.Recordcount > 0 Then 'error will occur if 0 recs and you try to move first
 rs.MoveFirst 
 Do Until rs.EOF = True '(or Do While Not rs.EOF
 'Code Loop 
End If
rs.Close 'best to close the recordset before setting it to nothing.
Set rs = Nothing
 
Upvote 0
I've tried many variations of leaving in DAO and not earlier today. I got various errors, which I'll investigate more tomorrow.

As far as using the IN statement, I use that all the time in SQL Server. I've found that most statements that are easy to accomplish and write in SQL Server are difficult to reproduce in Access. I didn't even know you could put IN in the criteria, but I'll try that out tomorrow. But again, looping through the recordset would help in not relying on the autonumber, and help me write a comma-delimited string.
 
Upvote 0
I got various errors, which I'll investigate more tomorrow.
We'll be happy to try to help with those if you need it. In fact, we might have clued in on those if we could have seen the rest of your code. I suspect you know that if your string is text, you will have to incorporate quotes in that build. It might look like this air code
Code:
strWherePart = " WHERE ClientName IN ('"
[I]code stuff
Do Until or Do While Not part[/I]
strWherePart = strWherePart & rs.fields(numberhere) & "', '"
Move Next
strWherePart = Mid(strWherePart,1, Len(strWherePart)-3) & ") "
Sometimes to keep code more concise, I will pass rs.Fields(number) to a function that has the sql building portion.
 
Last edited:
Upvote 0
Have you considered using sub queries?
 
Upvote 0
For one thing, you are missing a MoveNext statement
It's probably in the 'Code section you omitted?
It can be dangerous to make such assumptions! Especially when things aren't working as intended...
 
Upvote 0

Forum statistics

Threads
1,221,831
Messages
6,162,250
Members
451,757
Latest member
iours

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