473,386 Members | 1,705 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,386 software developers and data experts.

Vba code for a search button in access form

I have created a form with combo box and a search button to search for a book title entered in the combo box from the books table and display the other details of the book in a book subform.Pls I need the vba code for the search button. This is the code i tried
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command19_Click()
  2.  Dim LSQL  As String
  3.     Dim LSearchString As String
  4.  
  5.     If Len(Combo15) = 0 Or IsNull(Combo15) = True Then
  6.         MsgBox "You must enter a search string."
  7.  
  8.     Else
  9.  
  10.         LSearchString = Combo15
  11.  
  12.         'Filter results based on search string
  13.         LSQL = "select * from book"
  14.         LSQL = LSQL & " where bookID = " & LSearchString & ""
  15.  
  16.         Form_book.RecordSource = LSQL
  17.  
  18.         lblTitle.Caption = "Customer Details:  Filtered by '" & LSearchString & "'"
  19.         'Clear search string
  20.         Combo15 = ""
  21.  
  22.         MsgBox "Results have been filtered.  All Book Title containing " & LSearchString & "."
  23.  
  24.     End If
  25. End Sub.
  26.  
It gave an error on this line
'Form_book.RecordSource = LSQL'
Dec 14 '14 #1
2 4805
jimatqsi
1,271 Expert 1GB
Danny,
Welcome to Bytes.com. You'll find lots of good help here. But you have to do your part by posting all the relevant information. To tell us there was an error without telling what error occurred is not very helpful. We can guess but it would go a lot smoother if we didn't have to guess.

You say a book title is being entered in the search string. But the search string is being tested against BookID. Is BookID actually a title? Things ending in ID are usually numeric values. So it may be that your error came about because you compared a numeric to a string value.

Your solution might be as simple as changing BookID to BookTitle (if there is a field by the name of BookTitle).

Further, you really don't need a Search button. You could put this same code in the After_Update event of the combo box and then the search would be done as soon as a value was entered, no need to click a button afterward.

I hope that helps.

Jim
Dec 15 '14 #2
twinnyfo
3,653 Expert Mod 2GB
Danny,

First, my guess at to the nature of your error is that you are evaluating a String (LSearchString) for a probably numerical value (BookID), unless, of course, LSearchString has a Bound Column which is numerical--you have not indicated so.

It is also not typical to use a Combo Box as a Search text box, unless, of course, you also have set the Row Source of your combo box to the list of titles (and your bound column could be the BookID). This would allow the user to "Select" the Book Title, rather than "Enter" it. Then, in the AFterUpdate Event of your Combo Box (great advice, Jim!), all you need is this:

Expand|Select|Wrap|Line Numbers
  1. Me.SubFormName.Filter = "BookID = " & LSearchString
  2. Me.SubFormName.FilterOn = True
Also, as a word of advice, you have named your Command Button "Command19", which, although it will work, may be confusing later on as your troubleshoot--or if someone else has to troubleshoot. It is always wise to name your controls something that will make sense, so you can find them on your forms and give others some insight as to what they might do.
Dec 15 '14 #3

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

Similar topics

3
by: DC | last post by:
I need help. I've confused myself with all of the things I've tried. I'm trying to modify the exiting Contacts template in Access 2000 to include a search option. To the Contact Entry form I've...
8
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...
1
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...
4
by: Shailja | last post by:
Hi, Can anyone tell me how to open an access form from VB when access database is closed? Form gets open when database is open but I want code to open access form when database is closed?
7
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...
2
by: buddyr | last post by:
Hello, I have a cmd button on form to search for serial number. I notice when I search for record-that if I don't start at the beginning of records I might not find the record. example: if I am...
1
by: cbones | last post by:
Hello, I am trying to create a search button in a windows forms application. I used a Microsoft Access 2007 file as the database and would like to be able to search the file by specific fields. ...
3
by: Ganimex | last post by:
Hello, I am very new with access 2007 and have little Access or VBA experiance. In fact I have only been using Access for around 3 weeks. I am creating a massive DB with information for over 350...
13
spideynok
by: spideynok | last post by:
Hi! I had a code that can search, just enter any keyword like. If I want to search "Access" I can put Acc only and then it will show me all record that has "Acc" on the database like Access,...
3
by: hessah | last post by:
I am a beginner with Microsoft Access databases. Is is possible someone could help me with how I would place VB code behind a search button? Im developing database, i want to add Search button to...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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.