473,503 Members | 3,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA Code for Search Button on Access 2016 form

2 New Member
I have created a unbound text box (txtSearch) and a Button to use with the text box for searching all records on a form.

The field on the form that I am using for the search is labeled Full_Name.

When I try to search for a first name of any user in the Full_Name field nothing happens and I get no error.

Here is my code.


Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2.    If IsNull(TxtSearch) = False Then
  3.     Me.Recordset.FindFirst '[Full_Name]=' & TxtSearch
  4.     Me!TxtSearch = Null
  5.     If Me.Recordset.NoMatch Then
  6.         MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
  7.             Me!TxtSearch = Null
  8.     End If
  9. End If
  10.  
  11. End Sub

What am I missing?
Jun 27 '19 #1
5 6810
zmbd
5,501 Recognized Expert Moderator Expert
Good Morning duffyt60 and Welcome!

1) You have not set " TxtSearch " to any value; thus, it is null
2) I'll send you a template in your Bytes.com>Inbox with some suggestions on how to properly setup the VBA-IDE
The minimum is that you should have both
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Option Compare Database
Set properly
The first one will force you to properly declare your variables

3) It's a guess; however, I suspect that [Full_Name] is a text field; thus, you will need to quote the value you are searching for

with the bit of tweaking you now have:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2. '
  3.   Dim myTxtSearch as String
  4.   myTxtSearch = "[Full_Name]= %1" & me.TxtSearch & "%1"
  5.   myTxtSearch = Replace(myTxtSearch , "%1", Chr(34))
  6. '
  7.    If IsNull(TxtSearch) = False Then
  8.     Me.Recordset.FindFirst myTxtSearch 
  9.     Me.TxtSearch = Null
  10.     If Me.Recordset.NoMatch Then
  11.         MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
  12.             Me.TxtSearch = Null
  13.     End If
  14. End If
  15.  
  16. End Sub
The replace() makes inserting the double quote much cleaner otherwise you have silly things such as """" - it's a trick I picked up from Neopa
>> BTW: Building the string outside of the function (as I have done in lines 4 and 5) allows you to troubleshoot the string by just inserting a Debug.print yourStringHere following the formation and then [<ctrl><G> to open the immediate pane to see the string. A malformed string is one of the more common causes of unexpected results and errors.
Jun 27 '19 #2
NeoPa
32,557 Recognized Expert Moderator MVP
It seems like you're missing any wild-card in your search criteria. In simple terms you're searching for the record where the [Full_Name] field is exactly equal to the first name entered. I'm guessing, as most values in the table will consist of both first and surnames, that it never finds a matching record.

Your search criteria needs to end up something like :
Expand|Select|Wrap|Line Numbers
  1. "([Full_Name] Like 'Robert *')"
What you have is more like :
Expand|Select|Wrap|Line Numbers
  1. "([Full_Name]='Robert')"
That's guessing the format of your data but hopefully you can see the logic.

Another point that may be worth mentioning is that fields in tables should hold elemental data rather than grouping similar data together. Have a [Full_Name] field if you must, but create it in your query/SQL as a formula using other fields that are used to make it up. A common usage is :
Expand|Select|Wrap|Line Numbers
  1. ([FirstName] + ' ') & [Surname] AS [Full_Name]
That assumes there will always be a value for [Surname] but that [FirstName] may hold Nulls.
Jun 27 '19 #3
NeoPa
32,557 Recognized Expert Moderator MVP
There may be some misunderstanding about what I've advised so I'll add here a link to an article that discusses such stuff (Quotes (') and Double-Quotes (") - Where and When to use them).

I only noticed after reading Zmbd's post, after posting my last one, that your original use of quotes was further off-base than I'd realised when first I read your post. That's fine. The linked article will help clarify a lot of the potential problems when working with strings in code.

Replace() is very important and very helpful for putting strings together in a clear and obvious way. Personally I don't recommend using Char() for dealing with quotes of any sort but I know many do. As you'll see in the linked article it's rarely necessary to double up on quotes when dealing with VBA (") and SQL (') together, but when it is necessary I believe the code should show as clearly and as readably as possible what is going on. That said it is an option that many use so choose your poison and be ready to interpret that of other developers when you come across them.

PS. Some questions are clearer than others and this one is pretty decent. Especially for a first-timer.
Jun 27 '19 #4
duffyt60
2 New Member
I have updated my Access 2016 database form with the information provided by zmbd; however, I continue to get no error and nothing happens when I enter a first name in the search box. I have added a copy of my database for review.
{Admin edit}
Please don't attach databases unless requested by someone trying to help you.
We require (and expect) you to explain your situation clearly in words.
Jul 2 '19 #5
NeoPa
32,557 Recognized Expert Moderator MVP
Please explain clearly what you've changed and confirm that your project compiles without error. I take what you posted earlier to mean that but we need to be 100% clear.

Zmbd posted a buch of instructions and we need to know exactly which of those you've followed. It may well be all, and that would be great, but it's very rarely so. n explanation of exactly what you've done while following those instructions would make your situation a lot clearer.

Until you can manage that let's see what we do have enough information to progress with.

If your code all compiles ok but nothing happens when you enter a filtering value there are two things to check first :
  1. Is the property set so that the code you've written in the event procedure is run when the event triggers?
    One way to force that is to cut & paste all the code from the associated module back into itself. That way the associations are made for you automatically.
  2. Which event are you using? If it's the .AfterUpdate() event then that should work once you've made a change and moved off to another control on the form. If it's a Click() event then in needs to be clicked after making the changes (obviously).
Check that whatever event you're using actually triggers the correct code.
Jul 2 '19 #6

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

Similar topics

4
1476
by: Grant | last post by:
Can I add/code a button on my form that will print only the selected record? Thanks Grant Dayton
8
3195
by: Steph | last post by:
Hi. I'm very new to MS Access and have been presented with an Access database of contacts by my employer. I am trying to redesign the main form of the database so that a button entitled...
2
2573
by: rcmail14872 | last post by:
I have a form that lists several different categories for labels. I made a form that will let the user add a new category. On the Form the user can type in the name for the new category, then I...
1
4614
by: fong.yang | last post by:
I'm trying to set up a search button on a form. I went thru the FindRecord macro wizards and it's not working correctly or maybe it is and I just don't understand all the arguements I have to...
5
2026
by: neelesh kumar | last post by:
sir, i have a form exam created by using a table tblexam.In the tblexam fields are questionno,question,option1,option2,option3,option4,answer fields. In my form i have a...
7
2439
by: Jason1983 | last post by:
hi, iam using access for the first time and need help setting up search button on the form.i have employer and employee tables.so the employer table will be have many different groups of...
1
3206
by: sultan030281 | last post by:
hi! i'm beginner in access, please help me to create search button. i make form & in that form i give a text box. my wish, when I type a some name in the textbox, then I press that searchbutton,...
1
1308
by: mdshakilahmmed | last post by:
i have a simple access from where i am create simple search button to find record. search button works good, here is may question is,i want to create search button where i am just click the button &...
8
2642
by: MS Access Probz | last post by:
Hi all, I'm new to this forum. My Access skills are above basic, but I seem to be having trouble getting to the next level regardless of how much time I invest in it. I'm using Access 2016. ...
1
1423
by: HalloweenWeed | last post by:
Good day I am new to the forum and still learning MS Access. I have developed a code to allow 'live' 'searh-on-the-fly' queries 'as you type.' However, the problem lies in that when doing a query you...
0
7064
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
7261
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
7315
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...
0
7445
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...
0
5559
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,...
1
4991
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...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1492
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 ...
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.