S
Steve
Could you "guys" run your eyes of the following two
snippets of code and explain where I've gone awry, I
attempting to adapt an example from a book. I want to
create a QBF form, build an SQL string and display the
product of the function in a MSG box. The string is built
as far as the WHERE clause and then error message 3145
(syntax error in the WHERE clause) is displayed. If
possible could anybody give me a basic example and I
could take it from there. Thank you for your time.
Function BuildSQLString(ByRef strSQL As String) As Boolean
Dim strSELECT As String
Dim strFROM As String
Dim strWHERE As String
strSELECT = "SELECT
s.*.*" 'set the columns to
return results from
'(all columns
in this case)
'set an alias "h" for HRBaseTbl
strFROM = "FROM HRBaseTbl As h "
If chkFirstNameID Then
'check for 2nd or more WHERE term
If strWHERE <> " " Then strWHERE = strWHERE & " AND "
strWHERE = strWHERE & "h.HRID = " & cboFirstNameID
End If
If chkSurnameID Then
'check for 2nd or more WHERE term
If strWHERE <> " " Then strWHERE = strWHERE & " AND "
strWHERE = strWHERE & "h.HRID = " & cboSurnameID
End If
strSQL = strSELECT & strFROM
If strWHERE <> " " Then strSQL = strSQL & "WHERE " &
strWHERE
BuildSQLString = True
End Function
Private Sub cmdFind_Click()
Dim strSQL As String
Dim strTableName 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
snippets of code and explain where I've gone awry, I
attempting to adapt an example from a book. I want to
create a QBF form, build an SQL string and display the
product of the function in a MSG box. The string is built
as far as the WHERE clause and then error message 3145
(syntax error in the WHERE clause) is displayed. If
possible could anybody give me a basic example and I
could take it from there. Thank you for your time.
Function BuildSQLString(ByRef strSQL As String) As Boolean
Dim strSELECT As String
Dim strFROM As String
Dim strWHERE As String
strSELECT = "SELECT
s.*.*" 'set the columns to
return results from
'(all columns
in this case)
'set an alias "h" for HRBaseTbl
strFROM = "FROM HRBaseTbl As h "
If chkFirstNameID Then
'check for 2nd or more WHERE term
If strWHERE <> " " Then strWHERE = strWHERE & " AND "
strWHERE = strWHERE & "h.HRID = " & cboFirstNameID
End If
If chkSurnameID Then
'check for 2nd or more WHERE term
If strWHERE <> " " Then strWHERE = strWHERE & " AND "
strWHERE = strWHERE & "h.HRID = " & cboSurnameID
End If
strSQL = strSELECT & strFROM
If strWHERE <> " " Then strSQL = strSQL & "WHERE " &
strWHERE
BuildSQLString = True
End Function
Private Sub cmdFind_Click()
Dim strSQL As String
Dim strTableName 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