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 ... - Private Sub SearchFor_AfterUpdate()
-
On Error GoTo Err_SearchFor_AfterUpdate
-
Dim MySQL As String
-
-
MySQL = "SELECT KeyNumber, KeyCode, KeyName "
-
MySQL = MySQL & "FROM fKeyList "
-
-
If InStr(1, Me.SearchFor, Chr(34)) Then
-
MsgBox "Do not use quotes"
-
Else
-
-
Select Case Me.SearchField
-
-
Case 1 'keynumber
-
MySQL = MySQL & "WHERE [KeyNumber] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
-
Case 2 'keycode
-
MySQL = MySQL & "WHERE [KeyCode] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
-
Case 3 'keyname
-
If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
-
MsgBox "Must use letters or single apostrophe. No spaces or special characters"
-
Else
-
MySQL = MySQL & "WHERE [KeyName] like " & Chr(34) & Me![SearchFor] & "" & Chr(34)
-
-
End If
-
-
Case 4 'doornumber
-
MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM sfDoorList WHERE DoorNumber LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
-
-
Case 5 'lastname
-
If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
-
MsgBox "Must use letters or single apostrophe. No spaces or special characters"
-
Else
-
MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM sfStaffList WHERE StaffLastName LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
-
-
End If
-
End Select
-
-
'MsgBox MySQL
-
Me.sfKeyList.Form.RecordSource = MySQL
-
End If
-
-
Exit_SearchFor_AfterUpdate:
-
Exit Sub
-
-
Err_SearchFor_AfterUpdate:
-
MsgBox Err.Description
-
Resume Exit_SearchFor_AfterUpdate
-
-
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
8 2121 - ...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: - 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.
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 - me.[subformname].form.recordsource = ...
and it worked fine for me.
-Stewart
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.
- ...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: - 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.
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
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.
Alright I finally got it.
This is the end result of my code ... - Private Sub SearchFor_AfterUpdate()
-
On Error GoTo Err_SearchFor_AfterUpdate
-
Dim MySQL As String
-
-
MySQL = "SELECT KeyNumber, KeyCode, KeyName "
-
MySQL = MySQL & "FROM tKey "
-
-
If InStr(1, Me.SearchFor) Then
-
MsgBox "Do not use quotes"
-
Else
-
-
Select Case Me.SearchField
-
-
Case 1 'keynumber
-
MySQL = MySQL & "WHERE [KeyNumber] like '" & Me![SearchFor] & "'"
-
Case 2 'keycode
-
MySQL = MySQL & "WHERE [KeyCode] like '" & Me![SearchFor] & "'"
-
Case 3 'keyname
-
If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
-
MsgBox "Must use letters or single apostrophe. No spaces or special characters"
-
Else
-
MySQL = MySQL & "WHERE [KeyName] like '" & Me![SearchFor] & "'"
-
-
End If
-
-
Case 4 'doornumber
-
MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM tDoor WHERE DoorNumber LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
-
-
Case 5 'lastname
-
If Me.SearchFor.Value Like "*[!',!A-Z,!a-z]*" Then
-
MsgBox "Must use letters or single apostrophe. No spaces or special characters"
-
Else
-
MySQL = MySQL & "WHERE (KeyNumber In (SELECT DISTINCT KeyNumber FROM tStaff WHERE StaffLastName LIKE " & Chr(34) & "" & Me![SearchFor] & "" & Chr(34) & "))"
-
End If
-
End Select
-
-
'MsgBox MySQL
-
Me.sfKeyList.Form.RecordSource = MySQL
-
End If
-
-
Exit_SearchFor_AfterUpdate:
-
Exit Sub
-
-
Err_SearchFor_AfterUpdate:
-
MsgBox Err.Description
-
Resume Exit_SearchFor_AfterUpdate
-
-
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
|
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...
|
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,...
|
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...
| |