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 2141 - ...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 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...
|
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...
|
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.
|
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
|
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...
| |
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
|
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)
|
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
|
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...
|
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,...
|
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...
| |
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,...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |