Hi all,
I have created code to dynamically build a new SQL statement (I used faq705-2205 as a resource) and save it as a query in the current database. The intent is to be able to create a new query any time a user makes selections on a form and requests a 'report'. The report would just be showing the query results in a form for display. I used FAQ
I am attaching my code here. Currently I am just trying to use OpenQuery and can't even get that to happen.
I would like for the new query to be saved and then open the form that has that query name stored as it's RecordSource and display the new data each time.
I am sure I have been looking at it too long and can't see my own mistakes.
Thanks in advance for any help you can offer...
Donna
Private Sub cmd_ViewResults_Click()
On Error Resume Next
Dim ctl As Control
Dim sSQL As String
Dim sQryName As String
Dim bFirst As Boolean
Dim qryDef As QueryDef
Dim Db As DAO.Database
Set Db = CurrentDb()
sQryName = "qry_ReportResults"
sSQL = "SELECT field_names FROM tbl_Budget WHERE "
bFirst = True
'This section is where the code loops through each ComboBox
control on the form to collect parameters to build the WHERE clause.
With ctl
Select Case .ControlType
Case acComboBox
.SetFocus
If bFirst = True Then
sSQL = sSQL & ctl.Name & " = " & """" & ctl.Text & """"
bFirst = False
Else
sSQL = sSQL & " and " & BuildCriteria(.Name, dbText, .Text)
End If
End Select
End With
Next ctl
If ObjectExists("Query", sQryName) Then
Db.QueryDefs.Delete sQryName
End If
Set qryDef = Db.CreateQueryDef(sQryName, sSQL)
DoCmd.OpenQuery sQryName
'DoCmd.OpenForm "Budget Entries ALL", acNormal, , , acFormEdit
Set Db = Nothing
End Sub
I have created code to dynamically build a new SQL statement (I used faq705-2205 as a resource) and save it as a query in the current database. The intent is to be able to create a new query any time a user makes selections on a form and requests a 'report'. The report would just be showing the query results in a form for display. I used FAQ
I am attaching my code here. Currently I am just trying to use OpenQuery and can't even get that to happen.
I would like for the new query to be saved and then open the form that has that query name stored as it's RecordSource and display the new data each time.
I am sure I have been looking at it too long and can't see my own mistakes.
Thanks in advance for any help you can offer...
Donna
Private Sub cmd_ViewResults_Click()
On Error Resume Next
Dim ctl As Control
Dim sSQL As String
Dim sQryName As String
Dim bFirst As Boolean
Dim qryDef As QueryDef
Dim Db As DAO.Database
Set Db = CurrentDb()
sQryName = "qry_ReportResults"
sSQL = "SELECT field_names FROM tbl_Budget WHERE "
bFirst = True
'This section is where the code loops through each ComboBox
control on the form to collect parameters to build the WHERE clause.
With ctl
Select Case .ControlType
Case acComboBox
.SetFocus
If bFirst = True Then
sSQL = sSQL & ctl.Name & " = " & """" & ctl.Text & """"
bFirst = False
Else
sSQL = sSQL & " and " & BuildCriteria(.Name, dbText, .Text)
End If
End Select
End With
Next ctl
If ObjectExists("Query", sQryName) Then
Db.QueryDefs.Delete sQryName
End If
Set qryDef = Db.CreateQueryDef(sQryName, sSQL)
DoCmd.OpenQuery sQryName
'DoCmd.OpenForm "Budget Entries ALL", acNormal, , , acFormEdit
Set Db = Nothing
End Sub