T
Tony Williams
I am trying to build a form that the user has a series of Check Boxes and
unbound text boxes to choose which fields from 2 tables to search on and the
criteria for the search. I am following the idea from "Beginning Access 2000
VBA" by Smith and Sussman and trying to adapt their code to fit my form.
Basically the checkbox form builds a query that collects the results.
There is a command button that should execute the query. Here is the OnClick
procedure of the button
Private Sub cmdFind_Click()
Dim strSQL As String
If Not BuildSQLString(strSQL) Then
MsgBox "There was a problem building the SQL string"
Exit Sub
End If
MsgBox strSQL
CurrentDb.QueryDefs("qryExample").SQL = strSQL
End Sub
Here is my code for building the SQL
Function BuildSQLString(strSQL As String) As Boolean
Dim strSELECT As String
Dim strFROM As String
Dim strWHERE As String
strSELECT = "s.* "
strFROM = "tbldocument s "
If chkCompanyID Then
strFROM = strFROM & " INNER JOIN tblInstitution1 i " & _
"ON s.txtrefnbr = i.txtrefnbr "
strWHERE = " AND i.txtinstitution = " & txtcompany
End If
If chklastname Then
strFROM = strFROM & " INNER JOIN tblInstitution1 i " & _
"ON s.txtrefnbr = i.txtrefnbr "
strWHERE = " AND i.txtlastname = " & txtname
End If
If chkDaterange Then
If Not IsNull(txtDateFrom) Then
strWHERE = strWHERE & " AND s.txtExpiryDate >= " & _
"#" & Format$(txtDateFrom, "mm/dd/yyyy") & "#"
End If
If Not IsNull(txtDateTo) Then
strWHERE = strWHERE & " AND s.DateOrdered <= " & _
"#" & Format$(txtDateTo, "mm/dd/yyyy") & "#"
End If
End If
strSQL = "SELECT " & strSELECT
strSQL = strSQL & "FROM " & strFROM
If strWHERE <> "" Then strSQL = strSQL & "WHERE " & Mid$(strWHERE, 6)
BuildSQLString = True
End Function
This seems to work except that the query it creates uses the text which is
input in the textboxes on the form and loads them into the query with square
brackets as if it's the name of a field. For example if I input "Williams"
in the txtname text box it should find me all records in tblInstitution1
where the txtlastname field is Williams. However when I open the query the
criteria is shown as [williams] and not as "williams" and when I run the
query I get a message that asks me for the value of a field williams.
I'm a novice at VBA and am using the book to try and understand it but when
things go wrong I'm not yet clever enough to work out why!!!
Can anyone help me here?
Thanks
Tony
unbound text boxes to choose which fields from 2 tables to search on and the
criteria for the search. I am following the idea from "Beginning Access 2000
VBA" by Smith and Sussman and trying to adapt their code to fit my form.
Basically the checkbox form builds a query that collects the results.
There is a command button that should execute the query. Here is the OnClick
procedure of the button
Private Sub cmdFind_Click()
Dim strSQL As String
If Not BuildSQLString(strSQL) Then
MsgBox "There was a problem building the SQL string"
Exit Sub
End If
MsgBox strSQL
CurrentDb.QueryDefs("qryExample").SQL = strSQL
End Sub
Here is my code for building the SQL
Function BuildSQLString(strSQL As String) As Boolean
Dim strSELECT As String
Dim strFROM As String
Dim strWHERE As String
strSELECT = "s.* "
strFROM = "tbldocument s "
If chkCompanyID Then
strFROM = strFROM & " INNER JOIN tblInstitution1 i " & _
"ON s.txtrefnbr = i.txtrefnbr "
strWHERE = " AND i.txtinstitution = " & txtcompany
End If
If chklastname Then
strFROM = strFROM & " INNER JOIN tblInstitution1 i " & _
"ON s.txtrefnbr = i.txtrefnbr "
strWHERE = " AND i.txtlastname = " & txtname
End If
If chkDaterange Then
If Not IsNull(txtDateFrom) Then
strWHERE = strWHERE & " AND s.txtExpiryDate >= " & _
"#" & Format$(txtDateFrom, "mm/dd/yyyy") & "#"
End If
If Not IsNull(txtDateTo) Then
strWHERE = strWHERE & " AND s.DateOrdered <= " & _
"#" & Format$(txtDateTo, "mm/dd/yyyy") & "#"
End If
End If
strSQL = "SELECT " & strSELECT
strSQL = strSQL & "FROM " & strFROM
If strWHERE <> "" Then strSQL = strSQL & "WHERE " & Mid$(strWHERE, 6)
BuildSQLString = True
End Function
This seems to work except that the query it creates uses the text which is
input in the textboxes on the form and loads them into the query with square
brackets as if it's the name of a field. For example if I input "Williams"
in the txtname text box it should find me all records in tblInstitution1
where the txtlastname field is Williams. However when I open the query the
criteria is shown as [williams] and not as "williams" and when I run the
query I get a message that asks me for the value of a field williams.
I'm a novice at VBA and am using the book to try and understand it but when
things go wrong I'm not yet clever enough to work out why!!!
Can anyone help me here?
Thanks
Tony