Marshall,
Here is the entire routine as I have it now. It is a little sloppy as I am
changing the routine and have been trying various things to make the query
property be added and updated. The new code starts at the lines of pluses.
Private Function fBuildSQL()
'*******************************************
'Name: sBuildSQL (Sub)
'Purpose: Build an SQL Statement
'Author: John Spencer UMBC-CHPDM
'Date: January 17, 2002, 03:36:51 PM
'*******************************************
On Error GoTo ErrHandler
Dim strSQL As String
Dim strFieldList As String
Dim strJoinType As String
Dim i As Integer, i2 As Integer
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsQdf As DAO.Recordset
Dim fld As Field
Dim strSearchCriteria As String, strSearchOperator As String
Dim strSQLTemp As String
Dim strFldName As String, iFldType As Integer
Dim strNEWSQL As String, strNEWWhere As String
Dim qdfAny As QueryDef
Dim strX As String
'-----------------------------------------------------------------------
' BUILD SELECT CLAUSE
'-----------------------------------------------------------------------
'Get SELECT statement including Distinct, DistinctRow, and TOP n
i = InStr(1, strSelect, " Distinct ", vbTextCompare)
If i = 7 Then strSQLTemp = " Distinct "
i = InStr(1, strSelect, " Distinctrow ", vbTextCompare)
If i = 7 Then strSQLTemp = " DistinctRow "
i = InStr(1, strSelect, " Top ", vbTextCompare)
If i >= 7 And i <= 20 Then
strSQLTemp = strSQLTemp & " TOP "
i2 = InStr(i + 5, strSelect, " ", vbTextCompare)
strSQLTemp = strSQLTemp & Mid(strSelect, i + 5, i2 - (i + 5))
End If
If Me.chkShowDistinct = True Then
strX = fCheckDistinctDenied()
If Len(strX) > 0 Then
MsgBox "Distinct Not Allowed with " & strX & "!", , "No Distinct
with Memo fields"
Me.chkShowDistinct = False
Else
If InStr(1, strSQLTemp, " Distinct ", vbTextCompare) = 0 Or _
InStr(1, strSQLTemp, " Distinct ", vbTextCompare) > 15 Then
strSQLTemp = " DISTINCT " & strSQLTemp
End If
End If 'fCheckDistinctDenied
End If 'Me.chkShowDistinct = True
strSQLTemp = "SELECT " & strSQLTemp
'-----------------------------------------------------------------------
' SPECIFIED FIELDS
'-----------------------------------------------------------------------
strFieldList = fBuildSQLFieldList()
strSQLTemp = strSQLTemp & strFieldList & vbCrLf
'---------------------------------------------------------------
'Build the NEW WHERE clause
'---------------------------------------------------------------
'Right now we have n combo/textbox sets so set up the
'master loop to go through these controls
' For i = 0 To conMAXCONTROLS - 1
For i = 0 To cCountCriteria - 1
If Len(Me("cbxFld" & i) & "") > 0 And Len(Me("Txtval" & i) & "") > 0 _
Or Me("cmbCondition" & i) = "<>Null" Or Me("cmbCondition" & i) =
"Null" Then
If i > 0 And Len(strNEWWhere) > 0 Then
Select Case Me("opgClauseType" & i)
Case 1
strJoinType = " OR "
Case 2
strJoinType = " AND "
Case Else
strJoinType = "" 'ERROR CONDITION
Beep
End Select
End If 'Set StrJoinType
'get field name from vArFields array
strFldName = fGetOtherFieldName(Me("cbxfld" & i))
iFldType = fGetFieldType(Me("cbxFld" & i))
strSearchOperator = Me("cmbCondition" & i) & ""
If Len(Trim(strSearchOperator & "")) = 0 Then strSearchOperator =
"="
strSearchCriteria = Me("txtVal" & i) & ""
Select Case strSearchOperator
Case "=", ""
strSearchCriteria = strSearchCriteria
strSearchOperator = "="
Case ">", "<", ">=", "<=", "<>"
strSearchCriteria = strSearchCriteria
Case "Null"
strSearchCriteria = ""
strSearchOperator = "Is Null"
Case "<>Null"
strSearchCriteria = ""
strSearchOperator = "Is not Null"
Case "x*"
strSearchCriteria = strSearchCriteria & "*"
strSearchOperator = "Like"
Case "<>x*"
strSearchCriteria = strSearchCriteria & "*"
strSearchOperator = "Not Like"
Case "*x*"
strSearchCriteria = "*" & strSearchCriteria & "*"
strSearchOperator = "Like"
Case "<>*x*"
strSearchCriteria = "*" & strSearchCriteria & "*"
strSearchOperator = "Not Like"
Case "In", "<>In"
'Check field type and set item list up with proper
separators for dates
Select Case iFldType
Case dbDate
strSearchCriteria = "#" & Trim(strSearchCriteria) &
"#"
strSearchCriteria = ReplaceString(strSearchCriteria,
",", "#, #")
Case dbText, dbMemo
strSearchCriteria = Chr$(34) &
Trim(strSearchCriteria) & Chr$(34)
strSearchCriteria = ReplaceString(strSearchCriteria,
",", _
Chr$(34) & ", " & Chr$(34))
End Select
strSearchCriteria = " (" & strSearchCriteria & ")"
If strSearchOperator = "<>In" Then
strSearchOperator = "Not In"
End If
Case "Between"
strSearchOperator = "Between"
If Len(Trim(strSearchCriteria)) = 0 Then
ElseIf Trim(strSearchCriteria) = "And" Then
strSearchCriteria = vbNullString
ElseIf InStr(1, strSearchCriteria, " and ", vbTextCompare)
= 0 Then
strSearchCriteria = strSearchCriteria & " AND " &
strSearchCriteria
End If
End Select
If (iFldType = 10 Or iFldType = 12) And Right(strSearchOperator,
2) <> "In" Then
If Len(strSearchCriteria) > 0 Then
If Left(strSearchCriteria, 1) <> Chr(34) Then
strSearchCriteria = Chr(34) & strSearchCriteria
End If
If Right(strSearchCriteria, 1) <> Chr(34) Then
strSearchCriteria = strSearchCriteria & Chr(34)
End If
End If
End If
strNEWWhere = strNEWWhere & strJoinType _
& Application.BuildCriteria("" & strFldName & "", _
iFldType, strSearchOperator & " " & strSearchCriteria &
"")
End If ' Fieldname and Text Value are there
Next i
If Len(strWhere) > 0 And Len(strNEWWhere) > 0 Then
strNEWWhere = strWhere & " AND " & strNEWWhere
ElseIf Len(strWhere) > 0 Then
strNEWWhere = strWhere
ElseIf Len(strNEWWhere) > 0 Then
strNEWWhere = " WHERE " & strNEWWhere
End If
'---------------------------SPECIFIED SORT
ORDER ------------------------------
Dim strNewOrderBy As String
'Parse the order by list and build the Order by
strNewOrderBy = fBuildSQLSort(Me.lstSortOrder.RowSource)
fBuildSQL = strParameters & _
strSQLTemp & _
strFrom & vbCrLf & _
strNEWWhere & vbCrLf & _
strGroupBy & _
strHaving & _
strNewOrderBy
Me.txtSQL = strParameters & _
strSQLTemp & _
strFrom & vbCrLf & _
strNEWWhere & vbCrLf & _
strGroupBy & _
strHaving & _
strNewOrderBy
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
If DoesQueryExist("Display_AdHoc_Results") = False Then
CurrentDb.CreateQueryDef "Display_AdHoc_Results"
CurrentDb().QueryDefs("Display_AdHoc_Results").SQL = Me.txtSQL
DoEvents
CurrentDb().QueryDefs.Refresh
CurrentDb().QueryDefs("Display_AdHoc_Results").Properties("RecordsetType")
= 2
CurrentDb().QueryDefs.Refresh
DoEvents
End If
CurrentDb().QueryDefs("Display_AdHoc_Results").SQL = Me.txtSQL
ExitHere:
Set rsQdf = Nothing
Set rs = Nothing
Set db = Nothing
Exit Function
ErrHandler:
Select Case Err.Number
Case 3270
Dim dbany As DAO.Database
Dim qdef As DAO.QueryDef
Dim prp As DAO.Property
Set dbany = CurrentDb()
Set qdef = dbany.QueryDefs("Display_AdHoc_Results")
Set prp = qdef.CreateProperty("RecordSetType", dbInteger, 2)
qdef.Properties.Append prp
qdef.Properties.Refresh
dbany.QueryDefs.Refresh
Resume
'we're trying to open a parameter query
Case 3061
MsgBox "The " & mconQ & Me.lstTables & mconQ & " query you've
selected " _
& " is a Parameter Query." & vbCrLf & Err.Description,
vbExclamation + vbOKOnly, _
"Missing parameters"
Case Else
MsgBox Err.Number & ": " & Err.Description, , "fBuildSQL"
Me.txtSQL = ""
Resume Next
'Either invalid SQL or some other error
End Select
Me.SfrmDisplayData.SourceObject = vbNullString
Resume ExitHere
End Function
--
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
..