Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / New Users / August 2006

Tip: Looking for answers? Try searching our database.

Record Find, First & Last

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ray C - 09 Aug 2006 11:11 GMT
Help please
I am looking to find all the occurences in a table where one of the Fields
holds a "Serial Number" and another field holds the "Contract Number". The
Serial Numbers are unique and the Contract Numbers are the same. So, I want
to search through my Table, searching every record for a field that matches
my Contract Number Criterion and pick out the corresponding unique serial
number.
Easy, I thought. Just FindFirst Criteria, followed by a loop to FindNext
Criterion. the Loop to be terminated by an EOF.
When I did this the loop continued to run well after the last racord in the
Table had been found. (I thought thay the loop would exit after it had found
the last matching Critera)
So I came up with a solution whereby I did a Find First, store that record
in the variable "First", followed by a FindLast and store that record in the
variable "Last". I then would compare each record in the Find Next Loop with
the Variable "Last" and use that to exit wen FindNext = "Last". However, My
initial attempts gives me the results where "First" contains the first Serial
Number that matches the Contract Number, Great, Unfortunately the vatriable
Last = the same Serial Number. The loop works great and finds all of the
other serial numbers that have the same Contract Number but I have no method
of jumping out of the loop when I get to the last Matching Record.

I must be missing something,, can anyone help? I enclose my "Play" code
below but it contains bits of other attempts to get the routine working. Any
better suggestions as to how I should be approaching this would be most
welcome.

Please don'y foget that the line "if first = last" sould be in the loop to
provide an exit from the loop.
   rs1.MoveFirst
   rs1.FindFirst strCriteria
   first = rs1("SerialNo")
   rs1.MoveLast
   rs1.FindLast strCriteria
   last = rs1("SerialNo")
   If first = last Then Debug.Print , "Last"
Debug.Print rs1("ContractNo")
       [ContractNo] = rs1("ContractNo")
       strCriteria = BuildCriteria("ContractNo", dbInteger, [ContractNo])
   rs1.FindFirst strCriteria
Debug.Print rs1("SerialNo")
   ' If Not (rs1.NoMatch) Then
   Do Until rs1.EOF
   rs1.FindNext strCriteria
Debug.Print rs1("SerialNo")
    ' Else:
' Debug.Print "no Records"
   ' End If
   Loop
Douglas J. Steele - 09 Aug 2006 12:36 GMT
If I'm following correctly, the problem you're running into is because in
essence you're using:

  Do Until rs1.EOF
     rs1.FindNext strCriteria
     Debug.Print rs1("SerialNo")
  Loop

You're in a particular place in the recordset, so rs1.EOF isn't true and you
enter the loop. You do a FindNext, and that does take you to EOF. However,
you try to refer to rs1("SerialNo"), which causes an error, since you're at
EOF.

Try:

  rs1.FindFirst strCriteria
  Do Until rs1.EOF
     Debug.Print rs1("SerialNo")
     rs1.FindNext strCriteria
  Loop

or, if you prefer,

  rs1.FindFirst strCriteria
  If rs1.EOF Then
     Debug.Print "No Records"
  Else
     Do Until rs1.EOF
        Debug.Print rs1("SerialNo")
        rs1.FindNext strCriteria
     Loop
  End If

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> Help please
> I am looking to find all the occurences in a table where one of the Fields
[quoted text clipped - 45 lines]
>     ' End If
>     Loop
Ray C - 09 Aug 2006 19:52 GMT
Thanks Douglas, that is a great help.

Ray c

> If I'm following correctly, the problem you're running into is because in
> essence you're using:
[quoted text clipped - 89 lines]
> >     ' End If
> >     Loop
Ken Sheridan - 09 Aug 2006 13:21 GMT
Why not just use a query restricted on the ContractNo column, e.g.

Sub GetSerialNumbers(lngContractNo As Long)

   On Error GoTo Err_Handler

   Dim rst As ADODB.Recordset
   Dim strSQL As String
   
   strSQL = "SELECT * FROM Contracts" & _
       " WHERE ContractNo = " & lngContractNo
       " ORDER BY SerialNo"
   
   Set rst = New ADODB.Recordset
   
   With rst
       .ActiveConnection = CurrentProject.Connection
       .Open _
           Source:=strSQL, _
           CursorType:=adOpenKeyset, _
           Options:=adCmdText
       If Not .EOF Then
           Do While Not .EOF
               Debug.Print .Fields("ContractNo") & "; " & .Fields("SerialNo")
               .MoveNext
           Loop
       Else
           MsgBox "No matching records.", vbInformation, "Sorry"
       End If
   End With
   
Exit_here:
   rst.Close
   Set rst = Nothing
   Exit Sub
   
Err_Handler:
   MsgBox Err.Description, vbExclamation, "Error"
   Resume Exit_here

End Sub

Ken Sheridan
Stafford, England

> Help please
> I am looking to find all the occurences in a table where one of the Fields
[quoted text clipped - 45 lines]
>     ' End If
>     Loop
Ray C - 09 Aug 2006 19:56 GMT
Hi Ken
I managed to gry the issue resolved (with the help of another MVP) using EOF
but I am intrigued by your solution and will try it out.
Thank you for you for your help it is much appreciated.

Ray C

> Why not just use a query restricted on the ContractNo column, e.g.
>
[quoted text clipped - 90 lines]
> >     ' End If
> >     Loop
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.