473,624 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

searching recordsets using FindFirst and FindNext

Corster
36 New Member
I went through a great deal of hassle to figure this out for myself, but now it is complete, I would like to share it with the world!
I know afew other people have had trouble with FindFirst and FindNext, so I hope this will be helpful!

Expand|Select|Wrap|Line Numbers
  1. 'This is the counter to help with FindNext
  2. Dim vblCnt As Integer
  3.  
  4. Private Sub btnSearch_Click()
  5. On Error GoTo Err_btnSearch_Click
  6.     Dim vblSearch As String
  7.     Dim vblRSC As Recordset
  8.     cbxField.SetFocus
  9.     vblSearch = cbxField.Text
  10.     'Set variable to attain appropriate column referenced to text selected from combobox.
  11.     'This allows you to set the field the user wishes to search without using your field names.
  12.     If cbxField.Text = "My Search Column Option 1" Then
  13.         vblSearch = "[MyColumn1]"
  14.     Else
  15.         If cbxField.Text = "My Search Column Option 2" Then
  16.             vblSearch = "[MyColumn2]"
  17.         Else
  18.             If cbxField.Text = "My Search Column Option 3" Then
  19.                 vblSearch = "[MyColumn3]"
  20.             Else
  21.                 If cbxField.Text = "My Search Column Option 4" Then
  22.                     vblSearch = "[MyColumn4]"
  23.                 Else
  24.                     MsgBox ("Please select the field you wish to search!")
  25.                     Resume Exit_btnSearch_Click
  26.                 End If
  27.             End If
  28.         End If
  29.     End If
  30.     'Set focus to referenced control
  31.     txtSearch.SetFocus
  32.     'Use variable to store clone of current recordset to allow dynaset searching
  33.     Set vblRSC = Me.RecordsetClone()
  34.         'If not first search of specified text, find the succeeding record (Stored in FindNext global variable)
  35.         If vblCnt > 1 Then
  36.             vblRSC.FindNext (vblSearch & " = '" & txtSearch.Text & "'")
  37.             If vblRSC.NoMatch Then
  38.                 MsgBox (" There are no more instances of " & txtSearch.Text & "in" & cbxField.Text & ".")
  39.                 vblCnt = 1
  40.                 vblRSC.Close
  41.                 Exit Sub
  42.             Else
  43.                 'Freeze current record for Me.Refresh
  44.                 Me.Bookmark = vblRSC.Bookmark
  45.             End If
  46.         Else
  47.             'Find the first instance
  48.             vblRSC.FindFirst (vblSearch & " = '" & txtSearch.Text & "'")
  49.             If vblRSC.NoMatch Then
  50.                 MsgBox ("Cannot find " & txtSearch.Text & "in" & cbxField.Text & "; please redefine your search.")
  51.                 vblRSC.Close
  52.                 Exit Sub
  53.             Else
  54.                 Me.Bookmark = vblRSC.Bookmark
  55.             End If
  56.         End If
  57.         Me.Refresh
  58.     'Close the recordset clone
  59.     vblRSC.Close
  60.     If MsgBox("Is this the record you were looking for?" & Chr(13) & "Select 'Yes' to close this message or 'No' if you wish to continue searching.", vbYesNo, "Searching...") = vbNo Then
  61.         'Search for the next instance
  62.         vblCnt = vblCnt + 1
  63.         'Search again by performing another click on the search button
  64.         Call btnSearch_Click
  65.     Else
  66.         vblRSC.Close
  67.         Exit Sub
  68.     End If
  69.  
  70. Exit_btnSearch_Click:
  71.     vblRSC.Close
  72.     Exit Sub
  73.  
  74. Err_btnSearch_Click:
  75.     MsgBox Err.Description
  76.     Resume Exit_btnSearch_Click
  77. End Sub
May 22 '07 #1
1 6711
NeoPa
32,566 Recognized Expert Moderator MVP
Corster,
Thank you for posting this. This code may well help some of our members if they come across similar situations :)
May 25 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
7574
by: Paolini | last post by:
compiling this using gcc -o xxx xxx.c I obtain this error message in function Main 6 storage size of 'ff' isn't known Can any help me ? #include <stdio.h>
10
11832
by: Frusterated | last post by:
Hi, I'm using Access-2k under Win-XP... When I have a form with imbedded code it works perfect. Such as the following code example. Dim db as DAO.Database Dim rs as DAO.Recordset Set db = CurrentDB set rs = db.OpenRecordset("MyTable")
0
2085
by: imassadpk | last post by:
Hi all, Apprecite your help in resolving this tricky issue... I am trying implementing Recurrsive TreeView in MS. Access 2003 ADP project. So far, no luck :( The Problem: This sample snippet is all written for DAO and doesn't work in ADO
10
37832
by: Hughes | last post by:
Hi, I would like to get the filename from a folder by using C language. For example, in path /Users/abc/Desktop/xyz/ there is a file named "test.s" (file "test.s" is inside folder "xyz"). How should I write a routine in C to get the file name "test.s" ? Thanks a lot.
5
1623
by: Steffen Loringer | last post by:
Hi group, may be a simple question: How can I find out in C, which files of a specified extension exist in a specified folder? My app should constantly look for a file with an known extension but unknown name. A link with an example would be nice Thanks a lot for help Steffen
1
2924
by: bmshirey | last post by:
I have a program which manages various log files of multiple extensions. The program needs to delete files based on an age paramter and a file number limit paramter. So if files are older than say a week they get deleted, and if my max file number limit is 1000 and I have 1200, I delete 200 files, starting with the oldest. Findfirst and findnext work great for putting the files into a list when there is only one file type. They will load...
0
1338
by: David W. Fenton | last post by:
I was just working on an A97 app in which has lots of filtering and dynamic changing of the recordsets, but also uses bookmark navigation of subsets of records. I was trying to implement some fixes for some very rare conditions where the FIND dropdown doesn't list the same dataset as the form. I always code bookmark navigation with a With Me.RecordsetClone, and not thinking too carefully, I added a requery after the first NoMatch and...
0
1790
by: JC | last post by:
I have a list box loaded with Student Name, Social Security. When the user click on a specific record, the following code is assigned to the lstStudent1_AfterUpdate procedure. Private Sub lstStudent1_AfterUpdate() ' Find the record that matches the control. Dim rs As Object Set rs = Me.Recordset.Clone rs.FindFirst " = '" & Me! & "'"
9
1908
by: drhowarddrfine | last post by:
I don't want to use a db manager, like mysql, for such a small database but I'm finding this trickier than I thought and hope someone can provide some guidance. I have a restaurant menu with prices and other info in a small file. It's set up in a YAML-ish style, if you're familiar with that format. I'm just looking for some ideas on the best way to retrieve data based on a "keyword". What complicates things for me is that some of these...
0
8233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8619
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8334
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6108
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.