Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching for match in recordset

Status
Not open for further replies.

tania20

Programmer
Oct 30, 2006
148
AU
Hi, I am trying to search the records in a table "air to see if there is a match with general.HN (a field in the current form). If there is I want to do nothing, else add a new record with general.HN. I just cant seem to get it right though! any help appreciated!Cheers Tania

Set db = CurrentDb()
Set rs = db.OpenRecordset("air", dbOpenDynaset)

If Not rs.EOF then
If rs![HN] = Me![general.HN] Then

Else
rs.AddNew
rs![HN] = Me![general_info.HN]
rs.Update
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End If
End If
 
what you're doing is testing if the first record in the recordset matches hn, if not then add a new record...

if you want to test the whole recordset then you will have to loop through the entire recordset and test each record individually...

--------------------
Procrastinate Now!
 
How are ya tania20 . . .

Have a look at the [blue]FindFirst[/blue] method and the [blue]NoMatch[/blue] property for a recordset in VBE help . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
i tried using a loop with

do while not rs.eof
rs.movenext
loop

but that didnt work.is findfirst and nomatch avaiable in A2k?
 
tania20 . . .

In A2k:
Code:
[blue]   Dim db As DAO.Recordset, rs As DAO.Recordset
   
   Set db = CurrentDb()
   Set rs = db.OpenRecordset("air", dbOpenDynaset)
   
   rs.[purple][b]FindFirst[/b][/purple] "[HN] = [red][b]'[/b][/red]" & Me![general.HN] & "[red][b]'[/b][/red]"
      
   If rs.[purple][b]NoMatch[/b][/purple] Then
      rs.AddNew
      rs![HN] = Me![general_info.HN]
      rs.Update
   End If
   
   Set rs = Nothing
   Set db = Nothing[/blue]
Note: if [blue]HN[/blue] is numeric, remove the two single quotes in [red]red[/red] . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
thanks for everyones input, ive got it working how i need now..thanks!
 
tania20 . . .

. . . and your final results for those who were following this thread?

Calvin.gif
See Ya! . . . . . .

Be sure to see FAQ219-2884:
 
i used aceman's method with findfirst and nomatch.for some silly reason i had thught it wasnt available in A2k so hadnt tried it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top