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

Search from command button not working

Hi, Ive been working on an Access database for over a month now and I have had some trouble with creating a search button, and making it work. I can create the button but for some reason it stil does not work. Both me and my boss have been through the coding numerous times and cannot find what is wrong.

Does anybody know the coding to create a successful search button in Access 2003???

Any information will be greatly appreciated thank you
Jenny
Sep 15 '08 #1
6 2892
Stewart Ross
2,545 Expert Mod 2GB
Hi, and Welcome to Bytes!

It would help us if you could post the code you have at present, and advise us of the names of the fields and controls concerned - we know nothing about your database other than what you tell us, and since you have not told us anything about what it is you are searching upon it is very difficult to advise you.

I have retitled your post to a more meaningful title - 'Help!!!' does not direct readers to what you want help with...

-Stewart
Sep 15 '08 #2
Ok thanks for replying, below is the code I have for the search button. There are feilds named, BMS ID, Name, Lot#, Storage Location, Date Recieved, amount Recieved, Measure, Expiry date all in the Compound List table. I want a search button on the Compound Information form searching the first four feilds of all compounds in Compound List table.

I hope this helps you understand more


The code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdsearch_Click()
  2. On Error GoTo Err_cmdsearch_Click
  3.  
  4. 'Define variables for SQL search
  5.     Dim used As Byte
  6.     Dim MySQL As String
  7.  
  8.     'Define database variables
  9.     Dim dbs As AllForms, rst As Recordset
  10.  
  11.     'Initialise the "used" variable which tests whether the SQL statement has been previously written
  12.     'or not
  13.     used = 0
  14.  
  15.     'First half of the SQL statement which selects every record from the "PRB Index" table
  16.     MySQL = "Select * FROM [Compound List] Where "
  17.  
  18.     'IF fields are blank then display an error message saying so and set the focus to the first field
  19.     If ([Name] = "" And [BMS ID] = "" And [Lot#] = "" And [Storage Location] = "") Then
  20.         MsgBox "Please enter search data into at least one of the search fields", vbCritical, "Error!"
  21.         [Compound Information].SetFocus
  22.         'Exit this code
  23.         Exit Sub
  24.     End If
  25.  
  26.     'IF the Name field is not blank THEN
  27.     If Forms![Compound Information]![Name] <> "" Then
  28.         'Do a wildcard search on the Name
  29.         MySQL = MySQL & " ([Name] like '*" & [Name] & "*') "
  30.         'Set the used byte to 1 to indicate that the MySQL statement has been used
  31.         used = 1
  32.     End If
  33.  
  34.     'IF the BMSID field is not blank THEN
  35.     If Forms![Compound Information]![BMS ID] <> "" Then
  36.         'IF the SQL statement has already been written
  37.         If used = 1 Then
  38.             'Append more search criteria to the SQL statement
  39.             MySQL = MySQL & " AND ([BMS ID] like '*" & [BMS ID] & "*') "
  40.         'ELSE the SQL statement has not been written
  41.         Else
  42.             MySQL = MySQL & " ([BMS ID] like '*" & [BMS ID] & "*') "
  43.             'Set the used byte to 1 to indicate that the MySQL statement has been used
  44.             used = 1
  45.         End If
  46.     End If
  47.  
  48.     'IF the Lot# field is not blank THEN
  49.     If Forms![Compound Information]![Lot#] <> "" Then
  50.         'IF the SQL statement has already been written
  51.         If used = 1 Then
  52.             'Append more search criteria to the SQL statement
  53.             MySQL = MySQL & " AND ([Lot#] like '*" & [Lot#] & "*') "
  54.         'ELSE the SQL statement has not been written
  55.         Else
  56.             MySQL = MySQL & " ([Lot#] like '*" & [Lot#] & "*') "
  57.             'Set the used byte to 1 to indicate that the MySQL statement has been used
  58.             used = 1
  59.         End If
  60.     End If
  61.  
  62.     'IF the Storage Location field is not blank THEN
  63.     If Forms![Compound Information]![Storage Location] <> "" Then
  64.         'IF the SQL statement has already been written
  65.         If used = 1 Then
  66.             'Append more search criteria to the SQL statement
  67.             MySQL = MySQL & " AND ([Storage Location] like '*" & [Storage Location] & "*') "
  68.         'ELSE the SQL statement has not been written
  69.         Else
  70.             MySQL = MySQL & " ([Storage Location] like '*" & [Storage Location] & "*') "
  71.             'Set the used byte to 1 to indicate that the MySQL statement has been used
  72.             used = 1
  73.         End If
  74.     End If
  75.  
  76.  
  77.     'IF the SQL statement has been written (i.e. the SQL criteria is not blank)
  78.     If used = 1 Then
  79.  
  80.         'Set the recordsource of the search results form to the SQl statement generated above
  81.         Me![Search Sub Form].Form.RecordSource = MySQL
  82.         'Refresh the search results form to pick up the new recordsource
  83.         Me![Search Sub Form].Form.Refresh
  84.  
  85.         'Once the recordsource has been set, make the table visible
  86.         Me![Search Sub Form].Visible = True
  87.  
  88.         'Set up the database and recordset variables
  89.         'dbs is set to the current database
  90.         'rst is set to the MySQL statement criteria
  91.         Debug.Print MySQL
  92.         Set dbs = CurrentDb
  93.         Set rst = dbs.OpenRecordset(MySQL)
  94.  
  95.         'IF no records have been found
  96.         If rst.RecordCount < 1 Then
  97.             'Make the search results form invisible again
  98.             Me![Search Sub Form].Visible = False
  99.  
  100.             'Display an error message
  101.             MsgBox "No Records Found. Please Re-Enter Your Search Criteria", vbCritical, "Error!"
  102.         End If
  103.  
  104.         'Close the recordset and disassociate the dbs variable from the current database
  105.         rst.Close
  106.         Set dbs = Nothing
  107.     End If
  108.  
  109. Err_cmdsearch_Click:
  110.     MsgBox Err.Description
  111.     Resume Exit_cmdsearch_Click
  112.  
  113. Exit_cmdsearch_Click:
  114.     Exit Sub
  115.  
  116. End Sub
  117.  
Thanks very much
Jenny x x
Sep 15 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Hard to tell in 116 lines of code. Anyways, the first comment I have is that I think it's a complicated way to do something relatively simple - and the simplest way is to apply a filter to the form concerned. A filter comprises the WHERE clause of an SQL statement (just as you have at present) but without the WHERE. You could then just apply the filter instead of changing the recordsource for your subform - using
Expand|Select|Wrap|Line Numbers
  1. Forms![subformname].Form.Filter = <yourSQLWhereClauseStringWithoutWhere>
  2. Forms![subformname].Form.Filteron = True
I am not sure where you are currently going wrong. One error is that the Refresh method is not the correct one to apply to update the recordsource of a form; use the Requery method instead.

In debugging what you currently have, if you continue to have problems you should set a breakpoint at the start of the code at step through it a line at a time, viewing the values of all local variables as you go. In particular, check that the SQL statement is correctly formed - you should be able to copy it into an Access query to check that the SQL delivers the results you expect.

-Stewart
Sep 16 '08 #4
NeoPa
32,556 Expert Mod 16PB
In case it helps, Debugging in VBA tells you had to do some basic debugging (setting breakpoints, stepping through code, etc).
Sep 16 '08 #5
thank you I've got it working now! all your help was very much appreciated! Thanks again
Jennyx x x
Sep 29 '08 #6
NeoPa
32,556 Expert Mod 16PB
Good for you Jenny.

I hope you managed to get into some debugging. That can be such a useful tool in your arsenal.

Welcome to Bytes!
Sep 29 '08 #7

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

Similar topics

83
by: D. Dante Lorenso | last post by:
Trying to use the 'search' in the docs section of PostgreSQL.org is extremely SLOW. Considering this is a website for a database and databases are supposed to be good for indexing content, I'd...
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...
11
by: Pete | last post by:
Is there any way to change the default search to "Any Part Of Field" instead of whole field? The first thing I ever do when searching for something in a field is change the default setting from...
12
by: ljungers | last post by:
I'm on the home streach of my project and found that my "Reset for New Search" command button not working as desired. What should happen is that when the button is clicked a Event Procedure is run....
1
by: Chris | last post by:
I need a search form to perform. I have a Form and a subform. The subform is based off a query and the main form is unbound. The query runs perfect, however, I have a command button in the main...
2
by: Mark | last post by:
Hi All, I am creating a music database for a friend and have run into a problem. Within the ALBUM table, I wanted to store the ARTIST_ID rather than the ARTIST_NAME. To do this, I intended to have...
2
by: MNNovice | last post by:
I am working on a database on my CD collection using Access 2003. Some of the table structures are given below. Table 1 tblMusicCategory Field 1: MusicCategoryID - Auto Number (PK) Field 2:...
0
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
0
by: Mark112 | last post by:
Hi I am attempting to restore the search feature that was created by an external company using the indexing service for our intranet. the intranet is located locally at each of our offices. The...
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: 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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.