View Full Version : Query from Form


totomalas
08-23-2009, 01:55 AM
Is there a way to run a Query from a Form and allow the user to edit the Criteria and the fields shown in the Query directly from the Form..??

Uncle Gizmo
08-23-2009, 05:45 AM
You can run a query from a form; use the command button wizard to do this.

You can use controls on your form to dictate the criteria for your query.

The simplest way to vary the number of fields returned by the query would be to have several different queries called from several different command buttons.

The fact that you require different numbers of fields returned by the query is a good indication that there is room for improvement in your database design. It would be a good idea to check this before getting too deeply in to your project.

totomalas
08-23-2009, 08:33 AM
I want to give the user of the database the ability to build his own query from a form.. my database keeps track of retirement requests of those employees wanting to retire.

so there are two main criterion, one is request type and this has a drop down menu of five, and request status which has a drop down menu of four items..and there are ten fields in each record.....so i want the user to generate a query, that will show him all requests, with his selection of request type, his selection of request status and his selection of fields out of the ten. (through a checkbox or something)

it will be very hard to have a query built for every combination...so what do u suggest?

Uncle Gizmo
08-23-2009, 11:36 AM
>>>what do u suggest?<<<

There is a thread here that may provide you with inspiration... (http://www.access-programmers.co.uk/forums/showthread.php?p=225359)

Uncle Gizmo
08-23-2009, 11:42 AM
If you examine the sample db then this line:
strSQL = "SELECT tblInfo.ID, tblInfo.FName, tblInfo.LName, tblInfo.City, tblInfo.State " & _
"FROM tblInfo"

could be modified to allow selection of fields displayed.

Uncle Gizmo
08-23-2009, 11:57 AM
Download and have a look at this db the "Normalisation Form" (http://www.access-programmers.co.uk/forums/showthread.php?t=143970), you will see the functionality you require used in a different scenario, with some work you should be able to adapt it to your needs.



Private Function fExecute(intCodeSelect As Integer)
Dim adoCon As ADODB.Connection
Dim adoCmd As ADODB.Command

Set adoCon = CurrentProject.Connection
Set adoCmd = New ADODB.Command

Dim strSQL As String
Dim strSQL1 As String
Dim strSQL2 As String
Dim strSQL3 As String
Dim strField As String
Dim strFldName As String
Dim varRow As Variant

strSQL1 = fIntoTableSQL(intCodeSelect)
strSQL2 = "SELECT "
strSQL3 = " FROM "

For Each varRow In lstFieldToMove.ItemsSelected()
strField = lstFieldToMove.Column(0, varRow)
strFldName = "'" & strField & "'"
Select Case intCodeSelect
Case 1
strSQL = strSQL1 & strSQL2 & cboFromThisTable & "." & cboMatchingID1 & _
", " & cboFromThisTable & "." & strField & strSQL3 & cboFromThisTable
Case 2
strSQL = strSQL1 & strSQL2 & cboFromThisTable & "." & cboMatchingID1 & _
", " & strFldName & ", " & cboFromThisTable & "." & strField & strSQL3 & cboFromThisTable
End Select

With adoCmd
.ActiveConnection = adoCon
.CommandType = adCmdText
.CommandText = strSQL
.Execute
End With
Next varRow
End Function 'fExecute

totomalas
08-23-2009, 10:55 PM
Thank you very much.... this is very useful... :) :).....i will use it and if i have any questions i will come back to you..