I built a search form to experiment with the conversation on the following post mentioned at the end of my post, ( if you want to view the link just navigate through the instructions. As of now I can not post links). I too am trying to create a search form with dynamic values but seem to run into a problem. Apologies to Eljefegeneo for hijacking his post.
I can not post the database. I did export the table data into a tabbed delimited format and will include that at the end. If you want to reproduce my bug copy that data in a txt file and import that into the table tblMain. Make a search form and a sub form. the sub form is linked to the table and the main form is unbound with two search buttons.
Problem Statement:
The code works fine. I did find a bug that seems to arise with the wild cards when the entire field values are entered. You can replicate the bug by testing the search criteria listed below. What are your thoughts? Thanks
This is a brief example of the bug. A detailed description is near the code below.
If my name is "Devtycoon" and I search "Dev" the SQL statement will build "*Dev*" and it will pull up my name, "Devtycoon". On the contrary if I search "Tycoon" the SQL statement will build "*tycoon*" and it will pull up my name, "Devtycoon". If i search "DevTycoon" the sql statement will build "*Devtycoon*" and no results will be returned. That is buggy because the name is in the database but no wildcards would be needed.
Table structure:
tblMain
There is a function for the following components of the SQL statement
I tested two additional surnames organizations and program titles as follows:
Try example (1). you will get both the 4's and the 14's records returned.
If you type in letter for letter of the second record (the one with the 14's) no records populate. It is like the wild card does not like it if you type in the entire field value. Uncle Gizmo's and Allen Brown's method do the same thing were no records populate if the 14's entire surname organization and program title are entered into the text controls. Can you reproduce this error? Other than that I think either method is bulletproof.
Example criteria
1) put the following criteria in each control then hit the search button
4 surname
4 organization
4 program title
[two records returned]
2) put the following criteria in each control then hit the search button
[no records returned]
14 surname 14
14 organization 14
14 program title 14
Code behind search form:
Summary of the solution on the original post:
Two valuable solutions were offered to achieve a method of creating a dynamic search form.
To find the original post go to the forums under microsoft access discussion > queries> and search for the thread "Search Criteria" I can not post links at this time
2. Allen Brown
-build SQL string then set filter to the string; uses a procedure
tblMain (This is the data for table main in a tab delimited data format, data is in the field order shown in the table structure)
4 "4 Surname" "4 organization" "4 program title" "4 city" "4 state" "4 zip" "4 telephone"
5 "5 Surname" "5 organization" "5 program title" "5 city" "5 state" "5 zip" "5 telephone"
6 "6 surname" "6 organization" "6 program title" "6 city" "6 state" "6 zip" "6 telephone"
7 "7 surname" "7 organization" "7 program title" "7 city" "7 state" "7 zip" "7 telephone"
8 "8 surname" "8 organization" "8 program title" "8 city" "8 state" "8 zip" "8 telephone"
9 "Smith1" "234 organization" "234 program title" "234 city" "234 state" "234 zip" "234 telephone"
10 "smith2" "456 orgnaization" "456 program title" "456 city" "456 state" "456 zip" "456 telephone"
11 "1smith1" "11 organization" "11 program title" "111 city" "111 state" "111 zip" "111 telephone"
12 "2smith2" "22 organization" "22 program title" "22 city" "22 state" "22 zip" "22 telephone"
13 "1313 surname" "1313 organization" "1313 program title" "1313 city" "1313 state" "1313 zip" "1313 telephone"
14 "14 surname 14" "14 organization 14" "14 program title" "14 city 14" "14 state 14" "14 zip 14" "14 telephone 14"
I can not post the database. I did export the table data into a tabbed delimited format and will include that at the end. If you want to reproduce my bug copy that data in a txt file and import that into the table tblMain. Make a search form and a sub form. the sub form is linked to the table and the main form is unbound with two search buttons.
Problem Statement:
The code works fine. I did find a bug that seems to arise with the wild cards when the entire field values are entered. You can replicate the bug by testing the search criteria listed below. What are your thoughts? Thanks
This is a brief example of the bug. A detailed description is near the code below.
If my name is "Devtycoon" and I search "Dev" the SQL statement will build "*Dev*" and it will pull up my name, "Devtycoon". On the contrary if I search "Tycoon" the SQL statement will build "*tycoon*" and it will pull up my name, "Devtycoon". If i search "DevTycoon" the sql statement will build "*Devtycoon*" and no results will be returned. That is buggy because the name is in the database but no wildcards would be needed.
Table structure:
tblMain
- ClientID
- Surname [text]
- Organization [text]
- ProgramTitle [text]
- City [text]
- State[text]
- Zip4 [text]
- Telephone [text]
There is a function for the following components of the SQL statement
- SELECT / FROM
- WHERE
- controlA = me.txtSurname
- controlB = me.txtOrganization
- controlC = me.txtProgramTitle
I tested two additional surnames organizations and program titles as follows:
Try example (1). you will get both the 4's and the 14's records returned.
If you type in letter for letter of the second record (the one with the 14's) no records populate. It is like the wild card does not like it if you type in the entire field value. Uncle Gizmo's and Allen Brown's method do the same thing were no records populate if the 14's entire surname organization and program title are entered into the text controls. Can you reproduce this error? Other than that I think either method is bulletproof.
Example criteria
1) put the following criteria in each control then hit the search button
4 surname
4 organization
4 program title
[two records returned]
2) put the following criteria in each control then hit the search button
[no records returned]
14 surname 14
14 organization 14
14 program title 14
Code behind search form:
Code:
Option Compare Database
Option Explicit
Private Sub cmdShowallRecords_Click()
Dim strSQL0 As String
Me.txtOrganization = ""
Me.txtProgramTitle = ""
Me.txtSurName = ""
strSQL0 = fSQL_SelectFrom & fWhere & fSurName & fOrganization & fProgramTitle
Me.DS.Form.RecordSource = Left(strSQL0, Len(strSQL0) - 5) & ")"
End Sub
'--------------------------------------------------
Private Sub cmdSearch_Click()
Dim strSQL0 As String
strSQL0 = fSQL_SelectFrom & fWhere & fSurName & fOrganization & fProgramTitle
'MsgBox " >>> " & Left(strSQL0, Len(strSQL0) - 5) & ")"
Me.DS.Form.RecordSource = Left(strSQL0, Len(strSQL0) - 5) & ")"
End Sub
'--------------------------------------------------
Private Function fProgramTitle() As String
Dim strsubName As String
Dim strModuleName As String
strsubName = "fProgramTitle"
strModuleName = "Form - " & Me.Name
On Error GoTo Error_Handler:
Dim strSQL0 As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSTARL As String
Dim strSTARR As String
strSTARL = Chr(34) & "*" ' character 34 = "
strSTARR = "*" & Chr(34) ' character 34 = "
'((tblMain.ProgramTitle) Like
'"*"
'me.txtProgramTitle]
'"*"
'") AND"
strSQL1 = "((tblMain.ProgramTitle) Like "
'"*"
'Me.txtProgramTitle
strSQL2 = ") AND "
strSQL0 = strSQL1 & strSTARL & Me.txtProgramTitle & strSTARR & strSQL2
fProgramTitle = strSQL0
Exit_ErrorHandler:
Exit Function
Error_Handler:
Select Case Err.Number
Case 1 'When required, replace place holder (1) with an error number
MsgBox "Error produced by Place Holder please check your code ! Error Number >>> " _
& Err.Number & " error Desc >> " & Err.Description, conappName
Case Else
MsgBox "Error From --- " & strModuleName & ", " & strsubName & " --- error Number >>> " & Err.Number _
& " <<< Error Description >> " & Err.Description, , conappName
End Select
Resume Exit_ErrorHandler
End Function 'fProgramTitle
'--------------------------------------------------
Private Function fOrganization() As String
Dim strsubName As String
Dim strModuleName As String
strsubName = "fOrganization"
strModuleName = "Form - " & Me.Name
On Error GoTo Error_Handler:
Dim strSQL0 As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSTARL As String
Dim strSTARR As String
strSTARL = Chr(34) & "*" ' character 34 = "
strSTARR = "*" & Chr(34) ' character 34 = "
'((tblMain.Surname) Like
'" * "
'me.txtOrganization)
'" * "
') AND
strSQL1 = "((tblMain.Organization) Like "
'" * "
'me.txtOrganization)
'" * "
strSQL2 = ") AND "
strSQL0 = strSQL1 & strSTARL & Me.txtOrganization & strSTARR & strSQL2 '& strSQL3 & strSQL4 & strSQL5 & strSQL6 & strSQL7 & strSQL8 & strSQL9
fOrganization = strSQL0 '" ((tblMain.Surname) Like " * " & me.txtSurname) AND "
Exit_ErrorHandler:
Exit Function
Error_Handler:
Select Case Err.Number
Case 1 'When required, replace place holder (1) with an error number
MsgBox "Error produced by Place Holder please check your code ! Error Number >>> " _
& Err.Number & " error Desc >> " & Err.Description, conappName
Case Else
MsgBox "Error From --- " & strModuleName & ", " & strsubName & " --- error Number >>> " & Err.Number _
& " <<< Error Description >> " & Err.Description, , conappName
End Select
Resume Exit_ErrorHandler
End Function 'fOrganization
'--------------------------------------------------
Private Function fWhere() As String
Dim strsubName As String
Dim strModuleName As String
strsubName = "fWhere"
strModuleName = "Form - " & Me.Name
On Error GoTo Error_Handler:
Dim strSQL0 As String
Dim strSQL1 As String
strSQL1 = " WHERE ("
strSQL0 = strSQL1
fWhere = strSQL0
Exit_ErrorHandler:
Exit Function
Error_Handler:
Select Case Err.Number
Case 1 'When required, replace place holder (1) with an error number
MsgBox "Error produced by Place Holder please check your code ! Error Number >>> " _
& Err.Number & " error Desc >> " & Err.Description, conappName
Case Else
MsgBox "Error From --- " & strModuleName & ", " & strsubName & " --- error Number >>> " & Err.Number _
& " <<< Error Description >> " & Err.Description, , conappName
End Select
Resume Exit_ErrorHandler
End Function 'fWhere
'--------------------------------------------------
Private Function fSurName() As String
Dim strsubName As String
Dim strModuleName As String
strsubName = "fSurName"
strModuleName = "Form - " & Me.Name
On Error GoTo Error_Handler:
Dim strSQL0 As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSTARL As String
Dim strSTARR As String
strSTARL = Chr(34) & "*" ' character 34 = "
strSTARR = "*" & Chr(34) ' character 34 = "
strSQL1 = "((tblMain.Surname) Like "
'*
'Me.txtSurname
strSQL2 = ") AND "
strSQL0 = strSQL1 & strSTARL & Me.txtSurName & strSTARR & strSQL2
fSurName = strSQL0 '" ((tblMain.Surname) Like " * " & me.txtSurname) AND "
Exit_ErrorHandler:
Exit Function
Error_Handler:
Select Case Err.Number
Case 1 'When required, replace place holder (1) with an error number
MsgBox "Error produced by Place Holder please check your code ! Error Number >>> " _
& Err.Number & " error Desc >> " & Err.Description, conappName
Case Else
MsgBox "Error From --- " & strModuleName & ", " & strsubName & " --- error Number >>> " & Err.Number _
& " <<< Error Description >> " & Err.Description, , conappName
End Select
Resume Exit_ErrorHandler
End Function 'fSurName
Private Function fSQL_SelectFrom() As String
Dim strsubName As String
Dim strModuleName As String
strsubName = "fSQL_SelectFrom" 'fSQL_SelectFrom is the stcok name, replace with fSQL_SelectFrom or something
strModuleName = "Form - " & Me.Name
On Error GoTo Error_Handler
Dim strSQL0 As String
Dim strSQL1 As String
Dim strSQL2 As String
strSQL1 = "SELECT tblMain.ClientID, tblMain.Surname, tblMain.Organization, tblMain.ProgramTitle, tblMain.City, tblMain.State, tblMain.Zip4, tblMain.Telephone"
strSQL2 = " FROM tblMain"
strSQL0 = strSQL1 & strSQL2
fSQL_SelectFrom = strSQL0
Exit_ErrorHandler:
Exit Function
Error_Handler:
Select Case Err.Number
Case 1 'When required, replace place holder (1) with an error number
MsgBox "Error produced by Place Holder please check your code ! Error Number >>> " _
& Err.Number & " error Desc >> " & Err.Description, conappName
Case Else
MsgBox "Error From --- " & strModuleName & ", " & strsubName & " --- error Number >>> " & Err.Number _
& " <<< Error Description >> " & Err.Description, , conappName
End Select
Resume Exit_ErrorHandler
End Function 'sSQLX
Summary of the solution on the original post:
Two valuable solutions were offered to achieve a method of creating a dynamic search form.
To find the original post go to the forums under microsoft access discussion > queries> and search for the thread "Search Criteria" I can not post links at this time
- Uncle gizmo's video tutorials:
-build SQL string then set record source of subform to that string; uses a function to build SQL for each form control
2. Allen Brown
-build SQL string then set filter to the string; uses a procedure
tblMain (This is the data for table main in a tab delimited data format, data is in the field order shown in the table structure)
4 "4 Surname" "4 organization" "4 program title" "4 city" "4 state" "4 zip" "4 telephone"
5 "5 Surname" "5 organization" "5 program title" "5 city" "5 state" "5 zip" "5 telephone"
6 "6 surname" "6 organization" "6 program title" "6 city" "6 state" "6 zip" "6 telephone"
7 "7 surname" "7 organization" "7 program title" "7 city" "7 state" "7 zip" "7 telephone"
8 "8 surname" "8 organization" "8 program title" "8 city" "8 state" "8 zip" "8 telephone"
9 "Smith1" "234 organization" "234 program title" "234 city" "234 state" "234 zip" "234 telephone"
10 "smith2" "456 orgnaization" "456 program title" "456 city" "456 state" "456 zip" "456 telephone"
11 "1smith1" "11 organization" "11 program title" "111 city" "111 state" "111 zip" "111 telephone"
12 "2smith2" "22 organization" "22 program title" "22 city" "22 state" "22 zip" "22 telephone"
13 "1313 surname" "1313 organization" "1313 program title" "1313 city" "1313 state" "1313 zip" "1313 telephone"
14 "14 surname 14" "14 organization 14" "14 program title" "14 city 14" "14 state 14" "14 zip 14" "14 telephone 14"
Last edited: