473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Loop doesn't find all occurences of search, need help with recordset

Hello and TIA for guidance.

I am building a reusable search procedure (thanks go to Graham Thorpe
for his example that set me on my way). Everything works up until the
2nd match is found, the command doesn't go to the third match, it
exits out of the loop. I've read everything I could find in NG's, but
I can't figure out where I'm going wrong. Here is my code.

Function cmdSearch(ctlSearchText As Control, ctlFoundText As Control,
qrySearch As String)
'author Graham Thorpe 25-01-02
'modification Liz Malcolm 6/1/2004
'call from "On Click" event of a search button
On Error GoTo Err_cmdSearch
Dim dbSearch As DAO.Database
Set dbSearch = CurrentDb()
Dim rstSearch As DAO.Recordset
Set rstSearch = dbSearch.OpenRecordset(qrySearch)
Dim strTextRef As String
'strTextRef is the passed match to ctlFoundText
'ctlFoundText is the passed reference to the control on the form
'with the value you want to match
Dim strSearch As String
'strSearch is the passed value from the unbound search box
'ctlSearchText is the passed reference to the unbound search box
'txtSearch is the name of the unbound search box
'check search box for Null Value or Entry
If IsNull(ctlSearchText) Or (ctlSearchText) = "" Then
Call DataMissing_msg
ctlSearchText.SetFocus
Exit Function
End If
'Perform the search using value entered into txtSearch
rstSearch.MoveFirst
ctlFoundText.SetFocus
DoCmd.FindRecord ctlSearchText
ctlFoundText.SetFocus
strTextRef = ctlFoundText.Text
ctlSearchText.SetFocus
strSearch = ctlSearchText.Text
'If matching record is found, focus is set in matched control,
'and search control is cleared
If strTextRef = strSearch Then
Do While strTextRef = strSearch And Not rstSearch.EOF
DoEvents
If MsgBox("Match Found - Search Again?", vbYesNo, "Search
Results") = vbYes Then
ctlFoundText.SetFocus
rstSearch.MoveNext
If Not rstSearch.NoMatch = True Then
MsgBox ("There are no more matches")
Exit Do
Else
DoCmd.GoToRecord , , acNext 'display next record
End If
Else
Exit Do
End If
Loop
ctlFoundText.SetFocus
ctlSearchText = ""
'if value not found focus is set to a new record and search control is
cleared
Else
Call search_msg
Call NewRec
ctlSearchText.SetFocus
ctlSearchText = ""
End If
rstSearch.Close
Set rstSearch = Nothing
dbSearch.Close
Set dbSearch = Nothing
Exit_cmdsearch:
Exit Function
Err_cmdSearch:
MsgBox Err.Description
Resume Exit_cmdsearch
End Function

Again TIA for any help.

Liz M.
Nov 13 '05 #1
3 2193
rkc

"Liz Malcolm" <em******@bellsouth.net> wrote in message
news:ae**************************@posting.google.c om...
Hello and TIA for guidance.

I am building a reusable search procedure (thanks go to Graham Thorpe
for his example that set me on my way). Everything works up until the
2nd match is found, the command doesn't go to the third match, it
exits out of the loop. I've read everything I could find in NG's, but
I can't figure out where I'm going wrong. Here is my code.


<snip all posted code>

I didn't know where to begin correcting all that so I started with the
basic concept and re-wrote it. The match is on any part of the
target field. That's easily changed if it's not acceptable.

<code>
Function cmdSearch(ctlSearchText As Control, _
ctlFoundText As Control, _
qrySearch As String)
Dim strTextRef As String
'strTextRef is the passed match to ctlFoundText
'ctlFoundText is the passed reference to the control on the form
'with the value you want to match

Dim strSearch As String
'strSearch is the passed value from the unbound search box
'ctlSearchText is the passed reference to the unbound search box
'txtSearch is the name of the unbound search box
'check search box for Null Value or Entry

Dim lngLastRecordFound
'lngLastRecordfound holds the record position of the
'last matching record and is used to determine whether
'a match was found or not by comparison to the
'Me.CurrentRecord value

On Error GoTo Err_cmdSearch

lngLastRecordFound = 0

If IsNull(ctlSearchText) Or (ctlSearchText) = "" Then
'------------------------------------------
'replacement for routine not posted
MsgBox "Missing data message"
'------------------------------------------
GoTo Exit_cmdsearch
End If

'Perform the search using value entered into txtSearch
ctlFoundText.SetFocus
'Match any part of the field
DoCmd.FindRecord ctlSearchText.Value, acAnywhere
'set search and target string values for comparison
'note: could just use the control values directly
strTextRef = ctlFoundText.Value
strSearch = ctlSearchText.Value

'test for initial match
If InStr(strTextRef, strSearch) = 0 Then
MsgBox "No Records Found"
GoTo Exit_cmdsearch
End If

'keep searching until Me.CurrentRecord matches
'lngLastRecordFound or the user exits by choice.
Do While Me.CurrentRecord <> lngLastRecordFound
'update last found postion
lngLastRecordFound = Me.CurrentRecord

If MsgBox("Match Found - Search Again?", _
vbYesNo, "Search Results") = vbYes Then
ctlFoundText.SetFocus
DoCmd.FindNext
Else
Exit Do
End If
Loop

MsgBox "No more records found"

'-----------------
'unknown methods
'Call search_msg
'Call NewRec
'-----------------
Exit_cmdsearch:
'reset state of controls on exit
ctlSearchText.SetFocus
ctlSearchText = ""
Exit Function

Err_cmdSearch:
MsgBox Err.Description
Resume Exit_cmdsearch

End Function
</code>


Nov 13 '05 #2
"rkc" <rk*@yabba.dabba.do.rochester.rr.bomb> wrote in message news:<dR********************@twister.nyroc.rr.com> ...
"Liz Malcolm" <em******@bellsouth.net> wrote in message
news:ae**************************@posting.google.c om...
Hello and TIA for guidance.

I am building a reusable search procedure (thanks go to Graham Thorpe
for his example that set me on my way). Everything works up until the
2nd match is found, the command doesn't go to the third match, it
exits out of the loop. I've read everything I could find in NG's, but
I can't figure out where I'm going wrong. Here is my code.


<snip all posted code>

I didn't know where to begin correcting all that so I started with the
basic concept and re-wrote it. The match is on any part of the
target field. That's easily changed if it's not acceptable.

<code>
Function cmdSearch(ctlSearchText As Control, _
ctlFoundText As Control, _
qrySearch As String)
Dim strTextRef As String
'strTextRef is the passed match to ctlFoundText
'ctlFoundText is the passed reference to the control on the form
'with the value you want to match

Dim strSearch As String
'strSearch is the passed value from the unbound search box
'ctlSearchText is the passed reference to the unbound search box
'txtSearch is the name of the unbound search box
'check search box for Null Value or Entry

Dim lngLastRecordFound
'lngLastRecordfound holds the record position of the
'last matching record and is used to determine whether
'a match was found or not by comparison to the
'Me.CurrentRecord value

On Error GoTo Err_cmdSearch

lngLastRecordFound = 0

If IsNull(ctlSearchText) Or (ctlSearchText) = "" Then
'------------------------------------------
'replacement for routine not posted
MsgBox "Missing data message"
'------------------------------------------
GoTo Exit_cmdsearch
End If

'Perform the search using value entered into txtSearch
ctlFoundText.SetFocus
'Match any part of the field
DoCmd.FindRecord ctlSearchText.Value, acAnywhere
'set search and target string values for comparison
'note: could just use the control values directly
strTextRef = ctlFoundText.Value
strSearch = ctlSearchText.Value

'test for initial match
If InStr(strTextRef, strSearch) = 0 Then
MsgBox "No Records Found"
GoTo Exit_cmdsearch
End If

'keep searching until Me.CurrentRecord matches
'lngLastRecordFound or the user exits by choice.
Do While Me.CurrentRecord <> lngLastRecordFound
'update last found postion
lngLastRecordFound = Me.CurrentRecord

If MsgBox("Match Found - Search Again?", _
vbYesNo, "Search Results") = vbYes Then
ctlFoundText.SetFocus
DoCmd.FindNext
Else
Exit Do
End If
Loop

MsgBox "No more records found"

'-----------------
'unknown methods
'Call search_msg
'Call NewRec
'-----------------
Exit_cmdsearch:
'reset state of controls on exit
ctlSearchText.SetFocus
ctlSearchText = ""
Exit Function

Err_cmdSearch:
MsgBox Err.Description
Resume Exit_cmdsearch

End Function
</code>


Hello again. Since I am attempting to make this a reusable procedure
for all forms in my database shouldn't I be using the recordset method
instead of the ME method? I've placed the function in my
AllFormsUtilities module.

TIA for any help and guidance.
Nov 13 '05 #3
rkc

"Liz Malcolm" <em******@bellsouth.net> wrote in message
news:ae**************************@posting.google.c om...
Hello again. Since I am attempting to make this a reusable procedure
for all forms in my database shouldn't I be using the recordset method
instead of the ME method? I've placed the function in my
AllFormsUtilities module.


In that case, if I were to do things the way you're attempting to do
things, I guess I would pass the form object to the procedure
as well. You would then substitute the variable holding a reference
to the Form for Me.

objFormToSearch.CurrentRecord

As far as I could tell, the procedure you posted wasn't searching
for anything after the initial DoCmd.FindRecord. There was no
search being done in your loop. All it could ever do is exit.



Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sue Adams | last post by:
I actually have two issues/questions: I have an autonumber field in an access db table that I grab and later use to update a record in another table withing the same db. The code I use to get...
5
by: !TG | last post by:
I currently use Do while loop, but I'd rather use a For Loop though I have never gotten the hang of them. Would some one please be so kind as to show me how to loop through a recordset.
8
by: Jack | last post by:
Hi, I am maintaining quite a few asp applications. In one of those, I have to change the response.redirect to a particular page. This application has more than fifty asp pages. Instead of going...
4
by: Tom | last post by:
Wta is the code structure to put Rst.FindNext in a loop? I'm using what I show below. It works fine but my instinct says it should be in some standard loop rather than using the GoTo structure. ...
1
by: zpq | last post by:
.....I need to loop through a record set and get the value in one record and compare the value to the value in another record. tia stan
7
by: David Mitchell | last post by:
I use a function to read all of the files from a couple of directories (and subfolders) and update a table(tblfiles) with the fullpath and file name, the filesize and the date the file was created....
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
4
by: Dameon | last post by:
Hi All, I have a process where I'd like to search the contents of a file(in a dir) for all occurences (or the count of) of a given string. My goal is to focus more on performance, as some of the...
8
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.