Stuck in Loop

msjeb007

Registered User.
Local time
Today, 12:52
Joined
Mar 9, 2018
Messages
13
My loop code is stuck. It only returns the first record. What am I missing?

:banghead:

Code:
Dim rs As DAO.Recordset
Dim asReturn As String
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Parts A")
'rs.MoveFirst
If Not (rs.EOF) = True Then
    rs.MoveFirst
    Do Until rs.EOF
        asReturn = Mid(DLookup("doc_num_cd", "Parts A"), 2, 5)
        MsgBox "Substring is " & asReturn
        rs.MoveNext
    Loop
Else
    MsgBox "There are no records in the recordset."
End If
MsgBox "Finished looping through records."
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
 
Last edited by a moderator:
Not 100%, but I think your SQL is bad. When you use non-alphanumeric characters in names you must add brackets around them. If you have a table named "Parts A", to reference it in SQL, you need brackets like so:

[Parts A]

It would be best to omit that space. Further, do you have a Parts B and Parts C tables? That smells like a structure issue if so.
 
No there are no "Parts B" or "Parts C" tables. "Parts A" is a modification of the "Parts" table.
I tried the brackets and locked the database. Are you suggesting changing the table name to "Parts_A"?
 
That would be a better name, otherwise you must use brackets.
 
No change in the result. Still only returns on the first record.
 
These two lines of code do not use data from the recordset...
Code:
        asReturn = Mid(DLookup("doc_num_cd", "Parts A"), 2, 5)
        MsgBox "Substring is " & asReturn
That will return the same result for every iteration of the loop. You're just running the same DLookup() for each row in the recordset.
hth
Mark
 
Should I reference the recordset in my asReturn line?
 
Should I reference the recordset in my asReturn line?
I don't know what you are trying to do. Are you trying to get data out of the recordset? If so, then yes, you should reference the recordset.
Mark
 
Referencing a recordset in dlookup

How do you reference a recordset within dlookup?

Here is my line

asReturn = Mid(DLookup("doc_num_cd", "Parts_A"), 2, 5)

I've already set the recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Parts_A")
 
@OP,

You should look at how you use DLookup because you are not telling it WHICH record to look up. You are simply saying "Get me the first record you find". You are entirely omitting a criteria for DLookup.
 
Re: Referencing a recordset in dlookup

How many records do you have in the recordset? If it is only one then

asReturn = Mid(DLookup("doc_num_cd", rst!Parts_A), 2, 5)

If there are many records what are you expecting in asReturn? Or is asReturn an array where you would loop through the recordset, storing the string for each record in each array element.

Not enough information provided as to what you want
 
We have 2 threads on the same code from msjeb007.

Moderators informed.
 
You need to move to last record, then back to the first record to get it to work, below is how I normally do a loop on mine. As you're getting the data in the record set, then so long as the data you want is in there, you don't need to use a DLookup and can user rs("FIELDNAME")

Code:
Dim rs As DAO.Recordset
Dim asReturn As String
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Parts A")
'rs.MoveFirst
If Not rs.RecordCount = 0 Then
    rs.MoveLast
    rs.MoveFirst
    Do Until rs.EOF
        asReturn = Mid(rs("doc_num_cd"), 2, 5)
        MsgBox "Substring is " & asReturn
        rs.MoveNext
    Loop
Else
    MsgBox "There are no records in the recordset."
End If
MsgBox "Finished looping through records."
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
 
Cronk,
I have attempted to merge the threads. Thanks.
 

Users who are viewing this thread

Back
Top Bottom