473,770 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search Form using Option Buttons

7 New Member
Hello all,

I have created a database for my work that stores information for keys/locks, doors and employees.

Specifically the database contains all the information of our lock system, which doors certain locks are attached to and what employees have keys to certain locks.

The form has 3 subforms, 1 is the Key List which is basically the master subform as the other 2 subforms are a Door List and Staff List both of which are linked to the Key List using the [KeyNumber] field.

I am trying to make a search form to this that will allow me to search certain fields in all 3 of the subforms, the fields will be chosen by option buttons. With the search box being just a simple AfterUpdate command.

This is the code I currently have ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub SearchFor_AfterUpdate()
  2. On Error GoTo Err_SearchFor_AfterUpdate
  3.     Dim MySQL As String
  4.  
  5.     MySQL = "SELECT KeyNumber, KeyCode, KeyName "
  6.     MySQL = MySQL & "FROM fKeyList "
  7.  
  8.     If InStr(1, Me.SearchFor, Chr(34)) Then
  9.         MsgBox "Do not use quotes"
  10.     Else
  11.  
  12.         Select Case Me.SearchField
  13.  
  14.         Case 1  'keynumber
  15.             MySQL = MySQL & "WHERE [KeyNumber] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
  16.         Case 2  'keycode
  17.             MySQL = MySQL & "WHERE [KeyCode] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
  18.         Case 3  'keyname
  19.             If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
  20.                 MsgBox "Must use letters or single apostrophe. No spaces or special characters"
  21.             Else
  22.                 MySQL = MySQL & "WHERE [KeyName] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
  23.  
  24.             End If
  25.  
  26.         Case 4  'doornumber
  27.             MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM sfDoorList WHERE DoorNumber LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
  28.  
  29.         Case 5  'lastname
  30.             If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
  31.                 MsgBox "Must use letters or single apostrophe. No spaces or special characters"
  32.             Else
  33.                 MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM sfStaffList WHERE StaffLastName LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
  34.  
  35.             End If
  36.         End Select
  37.  
  38.     'MsgBox MySQL
  39.     Me.sfKeyList.Form.RecordSource = MySQL
  40.     End If
  41.  
  42. Exit_SearchFor_AfterUpdate:
  43.     Exit Sub
  44.  
  45. Err_SearchFor_AfterUpdate:
  46.     MsgBox Err.Description
  47.     Resume Exit_SearchFor_AfterUpdate
  48.  
  49. End Sub
The problem is it's not working. I keep getting this error when searching for anything using any of the options ...

The record source 'SELECT KeyNumber, KeyCode, KeyName FROM fKeyList WHERE [KeyNumber] like "****" specified on
this form or report does not exist.

The **** indicates the item i'm searching for.

Any help would be appreciated.

Thanks
Mar 2 '08 #1
8 2141
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
  1. ...MySQL = MySQL & "WHERE [KeyNumber] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
Hi Nmarks. At a glance the SQL looks fine - but then I notice the use of Chr(34) before and after all search phrases as above. ASCII 34 is the double-quote character ("). I guess it should instead be the single quote. Why not just refer directly to the character involved, like this:
Expand|Select|Wrap|Line Numbers
  1. MySQL = MySQL & "WHERE [KeyNumber] like '" & Me![SearchFor] & "'"
For clarity I would suggest not using Chr at all in your search strings unless you want to refer to a non-printable character.

-Stewart
ps Access string literals within a string built as above are referred to within single quotes, although strings in general are referred to within double quotes.
Mar 3 '08 #2
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi again. Notwithstanding my concerns about the quote marks, it isn't that. I have tried a skeleton version of your code in a new form using one of my own tables, but without the subforms. The SQL works without error on the recordsource of a main form.

After writing this I tried it successfully on a subform within a main form. Since everything seems to work, is sfKeyList a valid subform name? I too used
Expand|Select|Wrap|Line Numbers
  1. me.[subformname].form.recordsource = ... 
and it worked fine for me.

-Stewart
Mar 3 '08 #3
NMarks
7 New Member
In order to get all the forms to display in a tabular format like I wanted, and linked together, I had to create the form, then the "main" subform labeled sfKeyList.

The 2 additional subforms are sfDoorList and sfStaffList which are both subforms of sfKeyList.
Mar 4 '08 #4
NMarks
7 New Member
Expand|Select|Wrap|Line Numbers
  1. ...MySQL = MySQL & "WHERE [KeyNumber] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
Hi Nmarks. At a glance the SQL looks fine - but then I notice the use of Chr(34) before and after all search phrases as above. ASCII 34 is the double-quote character ("). I guess it should instead be the single quote. Why not just refer directly to the character involved, like this:
Expand|Select|Wrap|Line Numbers
  1. MySQL = MySQL & "WHERE [KeyNumber] like '" & Me![SearchFor] & "'"
For clarity I would suggest not using Chr at all in your search strings unless you want to refer to a non-printable character.

-Stewart
ps Access string literals within a string built as above are referred to within single quotes, although strings in general are referred to within double quotes.
I will have to edit this then.

The majority of my Access experience has been via Google and a guy I work with. He uses the Chr(34) a lot and I picked it up along the way.

Thank you for the advice.
Mar 4 '08 #5
Stewart Ross
2,545 Recognized Expert Moderator Specialist
I will have to edit this then.

The majority of my Access experience has been via Google and a guy I work with. He uses the Chr(34) a lot and I picked it up along the way.

Thank you for the advice.
Hi again. Just one more thought, since the SQL works fine for strings - it is a string you are comparing, isn't it? If the unique key number is a numeric value, not a string, the SQL 'where' condition will fail (there are no quote marks at all used when comparing numeric values, nor is the string comparison 'like' used). Long shot, though, given that your where clause seems well thought-out.

-Stewart
Mar 4 '08 #6
NMarks
7 New Member
Hi again. Just one more thought, since the SQL works fine for strings - it is a string you are comparing, isn't it? If the unique key number is a numeric value, not a string, the SQL 'where' condition will fail (there are no quote marks at all used when comparing numeric values, nor is the string comparison 'like' used). Long shot, though, given that your where clause seems well thought-out.

-Stewart
The unique Key Number field is a letter/numeric value. It contains 2 letters followed by 6 numbers manually entered by us, the user.

An example is "HB123456".

The KeyCode and DoorNumber fields are also setup the same way. Just less letters and numbers.
Mar 4 '08 #7
NMarks
7 New Member
Alright I finally got it.

This is the end result of my code ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub SearchFor_AfterUpdate()
  2. On Error GoTo Err_SearchFor_AfterUpdate
  3.     Dim MySQL As String
  4.  
  5.     MySQL = "SELECT KeyNumber, KeyCode, KeyName "
  6.     MySQL = MySQL & "FROM tKey "
  7.  
  8.     If InStr(1, Me.SearchFor) Then
  9.         MsgBox "Do not use quotes"
  10.     Else
  11.  
  12.         Select Case Me.SearchField
  13.  
  14.         Case 1  'keynumber
  15.             MySQL = MySQL & "WHERE [KeyNumber] like '" & Me![SearchFor] & "'"
  16.         Case 2  'keycode
  17.             MySQL = MySQL & "WHERE [KeyCode] like '" & Me![SearchFor] & "'"
  18.         Case 3  'keyname
  19.             If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
  20.                 MsgBox "Must use letters or single apostrophe. No spaces or special characters"
  21.             Else
  22.                 MySQL = MySQL & "WHERE [KeyName] like '" & Me![SearchFor] & "'"
  23.  
  24.             End If
  25.  
  26.         Case 4  'doornumber
  27.             MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM tDoor WHERE DoorNumber LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
  28.  
  29.         Case 5  'lastname
  30.             If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
  31.                 MsgBox "Must use letters or single apostrophe. No spaces or special characters"
  32.             Else
  33.                 MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM tStaff WHERE StaffLastName LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
  34.         End If
  35.         End Select
  36.  
  37.     'MsgBox MySQL
  38.     Me.sfKeyList.Form.RecordSource = MySQL
  39.     End If
  40.  
  41. Exit_SearchFor_AfterUpdate:
  42.     Exit Sub
  43.  
  44. Err_SearchFor_AfterUpdate:
  45.     MsgBox Err.Description
  46.     Resume Exit_SearchFor_AfterUpdate
  47.  
  48. End Sub
I replaced all the FROM fields to reference the tables directly as opposed the subforms on the form.

This worked like a charm. I did also clean up the code a bit as you advised, more so in the first section I still have to go through the bottom 2 sections that reference the other subforms, I tried once already but keep getting a syntax error so I just need to work it out a bit.

Thank you for all your help though it was greatly appreciated.
Mar 4 '08 #8
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi again. Well done for solving what was a very puzzling problem. You did all the work, and should take all the credit!

Cheers

Stewart
Mar 4 '08 #9

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

Similar topics

2
1719
by: James | last post by:
Hi there, I'm trying to create a simple DVD shopping cart. I have implemented a way to browse by genre and am trying to implement a way to search by selecting title, director and actor and typing some text in the text field for it to search the database. As trying to implement the title, director and actor searches seemed to complicated to try together, I am trying to make the title one work first and then move on to the other two from...
5
5056
by: Lyn | last post by:
Hi, I hope someone can help. I have a main form which mostly fills the Access window. In the bottom half of this form I have a tab control to display various types of data related to the main form record. In each page of the tab control, there are three buttons: Add, Update, Delete. Any of these buttons will open another, smaller form to add, update or delete the related data record. Originally, I had placed the smaller form such...
8
3222
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 'search' may be clicked on by the user and the user can then search all records by postcode. I want to do this to prevent duplicate data entry.
1
1754
by: Jackson M via AccessMonster.com | last post by:
I have buttons name A thur Z - When I select say the letter "H" button I need my ProductName field to jump to the record that starts with the letter "H" and soforth. Can anyone help -- This Message Is Coming To You From Sunny South Padre Island, Texas Message posted via http://www.accessmonster.com
2
3493
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button "No", he should not have the option of clicking on any of the six checkboxes. See Code attached. Thank you so much in advance for your help as I can't get to make this combo work. <p>Did you have any problems finding any of the information...
20
10828
by: Robert | last post by:
Need some help to stop me going around in circles on this one.... Have a nested subform (subform2) which simulates a continuous form for the record on the parent subform. Subform2 has rows of either an option button plus two text fields or a checkbox plus two text fields Am wanting to save the user entries into an underlying table. Tag property for each option button, check box or text field has the value of the key
2
7250
by: Homey! | last post by:
Hello all I am new to Access. I have imported data from an old FoxPro 2.x database. This is probably the most basic function but I cant get a search box to work. I need to search for company name and cant figure it out in access. Tony (homey)
3
2207
by: Elainie | last post by:
I would like to search a form with many fields on it, with out using the search facility through access. Througth a drop down list if possible.... How would I go about this? How could I also create this from a top bar menu option too...? Elaine
0
9617
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
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
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...
1
10036
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
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.