I think you may write such function by yourself. It isn't so hard.
Oh! Just now I wrote function that you need. It is ready for using - you may copy following code and paste into any module and use this function always when you need. Of course you can develop it and make it better and better, but there you are idea and little more:
Function UserSetSQL(strTableName As String, _
Optional strExcludingFieldList As String = "", _
Optional strDelimiter As String = ";", _
Optional strWhere As String = "", _
Optional strGroupBy As String = "", _
Optional strOrderBy As String = ""
Dim strSQL As String
Dim dbs As Database
Dim tdf As TableDef
Dim fld As Field
Dim arrExclude
Dim i As Integer
arrExclude = Split(strExcludingFieldList, strDelimiter)
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(strTableName)
For Each fld In tdf.Fields
If UBound(arrExclude) <> -1 Then
For i = LBound(arrExclude) To UBound(arrExclude)
If Trim(arrExclude(i)) = fld.Name Then
GoTo NextField
End If
Next i
End If
If strSQL <> "" Then
strSQL = strSQL & ","
End If
strSQL = strSQL & "[" & fld.Name & "]"
NextField:
Next fld
dbs.Close
Set dbs = Nothing
If Trim(strWhere) <> "" Then
strWhere = "Where " & strWhere
End If
If Trim(strGroupBy) <> "" Then
strGroupBy = "Group By " & strGroupBy
End If
If Trim(strOrderBy) <> "" Then
strOrderBy = "Order By " & strOrderBy
End If
strSQL = "Select " & strSQL & " From [" & strTableName & "] "
strSQL = Trim(strSQL & strWhere & " " & strGroupBy & " " & strOrderBy) & ";"
UserSetSQL = strSQL
End Function
Examples:
1. All fields are included in SQL clause without Where and Order statements:
?UserSetSQL("SKL" Select [PersonalCode],[SkillCode],[SkillDate],[RevOrgNo],[USERID],[RevisionDate] From [SKL];
2. Several fields are excluded and fields for Where and Order statements are defined:
?UserSetSQL("SKL", "USERID;RevisionDate;RevOrgNo",";","RevisionDate<Date()-15",,"PersonalCode,RevisionDate" Select [PersonalCode],[SkillCode],[SkillDate] From [SKL] Where RevisionDate<Date()-15 Order By PersonalCode,RevisionDate;
In addition there is sub program that create new query:
Function NewQuery(strSQL As String, strQueryName As String) 'This function create new query
'It check for existing query with same name and remove if find it
Dim dbs As Database, myQueryDefine As QueryDef
' Return reference to current database.
Set dbs = CurrentDb
' Delete old query with same name
On Error GoTo Err_NewQuery
dbs.QueryDefs.Delete strQueryName
Err_NewQuery: ' Create new query.
Set myQueryDefine = dbs.CreateQueryDef(strQueryName, strSQL)
Set dbs = Nothing
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.