Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / New Users / May 2007

Tip: Looking for answers? Try searching our database.

Changing the criterion in a Query

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ray C - 18 May 2007 11:16 GMT
Am I able to change the criterion in a query under program control?
I would like to use the same query in different circumstances and search by
different criterion based on different fields. Is this possible and, if so,
how would the code structure look?
RayC
Dennis - 18 May 2007 12:42 GMT
You do this sort of thing using a form. Enter your criteria into fields on
the form.
You can then enter into the criteria rows of your query.
[Forms]![FormName]![Fieldname]

> Am I able to change the criterion in a query under program control?
> I would like to use the same query in different circumstances and search by
> different criterion based on different fields. Is this possible and, if so,
> how would the code structure look?
> RayC
Klatuu - 18 May 2007 14:48 GMT
Yes, there are several ways to do this.  Which you use will depend on the
circumstances.  To address a specific, based on your post, one way is to
create a stored query that has no criteria at all (no WHERE).  Then,
programmatically create the WHERE portion of the query.  Now, retrieve the
SQL of a "template" version of the query, add the WHERE part to it, and save
it back to the stored query.

Here is an example:

Dim strWhere As String
Dim strSQL As String
   
   strSQL = CurrentDb.QueryDefs("qselLaborWeeklyDetailXLTemplate").SQL
   strWhere = BuildWhere()
   If Len(strWhere) > 0 Then
       strSQL = Replace(strSQL, ";", "Having " & strWhere)
   End If
   CurrentDb.QueryDefs("qselLaborWeeklyDetailXL").SQL = strSQL

Here is an example of how I build a Where condition string when there are
multiple controls that may or may not be included in the criteria:

Private Function BuildWhere() As String
Dim strWhere As String
   
   On Error GoTo BuildWhere_Error

   If Len(Me.cboActivity & "") > 0 Then
       strWhere = "[CISAttributeTable_ME]![Activity] = '" & Me.cboActivity
& "'"
   End If
   
   If Len(Me.cboAcctgUnit & "") > 0 Then
       If Len(strWhere) > 0 Then
           strWhere = strWhere & " And "
       End If
       strWhere = strWhere & "[PerformAcctUnit] = '" & Me.cboAcctgUnit & "'"
   End If
   
   If Len(Me.cboEmployeeID & "") > 0 Then
       If Len(strWhere) > 0 Then
           strWhere = strWhere & " And "
       End If
       strWhere = strWhere & "[EmployeeNum] = '" & Me.cboEmployeeID & "'"
   End If
   
   BuildWhere = strWhere
   
BuildWhere_Exit:

   On Error Resume Next
   Exit Function

BuildWhere_Error:

   MsgBox "Error " & Err.Number & " (" & Err.Description & _
       ") in procedure BuildWhere of VBA Document
Form_frmLaborWeeklySelection"
   GoTo BuildWhere_Exit
   
End Function

Signature

Dave Hargis, Microsoft Access MVP

> Am I able to change the criterion in a query under program control?
> I would like to use the same query in different circumstances and search by
> different criterion based on different fields. Is this possible and, if so,
> how would the code structure look?
> RayC
KARL DEWEY - 19 May 2007 00:17 GMT
One way I did was to have a form with option group and multiple text boxes
for entring criteria.
In the query add a field to the grid for the option group.  Have a row for
each selection of option group and criteria to go with it.

OPT 1 - field X > textbox1
OPT 2 - field X < textbox1
OPT 3 - field X between textbox1 and textbox2
OPT 4 - field Y = textbox1

Signature

KARL DEWEY
Build a little - Test a little

> Am I able to change the criterion in a query under program control?
> I would like to use the same query in different circumstances and search by
> different criterion based on different fields. Is this possible and, if so,
> how would the code structure look?
> RayC
Ray C - 19 May 2007 16:49 GMT
What a Great response, Thank you all

RayC

> Am I able to change the criterion in a query under program control?
> I would like to use the same query in different circumstances and search by
> different criterion based on different fields. Is this possible and, if so,
> how would the code structure look?
> RayC
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.