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 ...
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
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 ...
Code:
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 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
Comment