473,406 Members | 2,467 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,406 software developers and data experts.

Search Form using Option Buttons

7
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 2121
Stewart Ross
2,545 Expert Mod 2GB
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 Expert Mod 2GB
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
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
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 Expert Mod 2GB
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
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
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 Expert Mod 2GB
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
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...
5
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...
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: 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...
2
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...
20
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...
2
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...
3
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...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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...
0
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
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,...
0
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...

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.