![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a form that, without going into too much detail, deletes a query and then creates a new query “TempNewQuery” |
#3
| |||
| |||
|
|
On Mon, 9 Apr 2012 15:11:02 +0100, "Phil" <phil (AT) stantonfamily (DOT) co.uk wrote: I have a form that, without going into too much detail, deletes a query and then creates a new query “TempNewQuery” Why delete and recreate the query? Create the query without the Where clause. Then pass the where clause as part of a string to the Recordset statement. Air Code strSQL "SELECT * from [query name] WHERE WHERE (((TrophyYear.Year)>" & IIf(Forms.ReportSelector.FromYear |
#4
| |||
| |||
|
|
On 12/04/2012 21:32:32, Tony Toews wrote: On Mon, 9 Apr 2012 15:11:02 +0100, "Phil" <p... (AT) stantonfamily (DOT) co.uk wrote: I have a form that, without going into too much detail, deletes a queryand then creates a new query “TempNewQuery” Why delete and recreate the query? *Create the query without the Where clause. Then pass the where clause as part of a string to the Recordset statement. Air Code strSQL *"SELECT * from [query name] WHERE WHERE (((TrophyYear.Year)>" & IIf(Forms.ReportSelector.FromYear Ah... it appears that I have to go into some detail. I have a form that lists every form, query and report in the database. From any of these, I can create a new query and add additional parameters. I delete the old TempNewQuery whatever that might be from the last time I selected an item, then create a new TempNewQuery based on the selected form/ Report/Query and the additional parameters. This subsequently gets used for Emails or mail merge Phil- Hide quoted text - - Show quoted text - |
#5
| |||
| |||
|
|
On Apr 13, 9:45*am, "Phil" <p... (AT) stantonfamily (DOT) co.uk> wrote: On 12/04/2012 21:32:32, Tony Toews wrote: On Mon, 9 Apr 2012 15:11:02 +0100, "Phil" <p... (AT) stantonfamily (DOT) co.uk wrote: I have a form that, without going into too much detail, deletes a query and then creates a new query “TempNewQuery” Why delete and recreate the query? *Create the query without the Wher e clause. Then pass the where clause as part of a string to the Recordset statement. Air Code strSQL *"SELECT * from [query name] WHERE WHERE (((TrophyYear.Year)>" & IIf(Forms.ReportSelector.FromYear Ah... it appears that I have to go into some detail. I have a form that lists every form, query and report in the database. Fr om any of these, I can create a new query and add additional parameters. I delete the old TempNewQuery whatever that might be from the last time I selected an item, then create a new TempNewQuery based on the selected fo rm/ Report/Query and the additional parameters. This subsequently gets used f or Emails or mail merge Phil- Hide quoted text - - Show quoted text - Not much help to your question. I sometimes simply exchange/update the SQL statement. Ex: Sub SetQSql() Dim d As Database Dim q As QueryDef Set d = CurrentDb Set q = d.QueryDefs("Query1") q.SQL = "Select * from Query2" 'change query1's SQL d.Close End Sub Are you stepping thru your code? I sometimes will, in debug mode, get the SQL I just updated and do a debug.print strSQL and then grab that SQL and create a new query, go into SQL view, and paste the SQL from the debug into it and then see if it runs. |
#6
| |||
| |||
|
|
On 13/04/2012 20:01:14, Patrick Finucane wrote: On Apr 13, 9:45*am, "Phil" <p... (AT) stantonfamily (DOT) co.uk> wrote: On 12/04/2012 21:32:32, Tony Toews wrote: On Mon, 9 Apr 2012 15:11:02 +0100, "Phil" <p... (AT) stantonfamily (DOT) co.uk wrote: I have a form that, without going into too much detail, deletes a query and then creates a new query “TempNewQuery” Why delete and recreate the query? *Create the query without the Wher e clause. Then pass the where clause as part of a string to the Recordset statement. Air Code strSQL *"SELECT * from [query name] WHERE WHERE (((TrophyYear.Year)>" & IIf(Forms.ReportSelector.FromYear Ah... it appears that I have to go into some detail. I have a form that lists every form, query and report in the database.Fr om any of these, I can create a new query and add additional parameters. I delete the old TempNewQuery whatever that might be from the last time I selected an item, then create a new TempNewQuery based on the selectedfo rm/ Report/Query and the additional parameters. This subsequently gets used f or Emails or mail merge Phil- Hide quoted text - - Show quoted text - Not much help to your question. *I sometimes simply exchange/update the SQL statement. *Ex: Sub SetQSql() Dim d As Database Dim q As QueryDef Set d = CurrentDb Set q = d.QueryDefs("Query1") q.SQL = "Select * from Query2" *'change query1's SQL d.Close End Sub Are you stepping thru your code? I sometimes will, in debug mode, get the SQL I just updated and do a debug.print strSQL and then grab that SQL and create a new query, go into SQL view, and paste the SQL from the debug into it and then see if it runs. Wish life were that simple. Query1 in your example almost certainly will contain WHERE clauses and ORDER BY and possibly HAVING etc. |
|
I have a form which allows the user to basically add additional WHERES. There is a combo box that you pick for the different types of WHERE condition like =, >=, like, between etc and then the fields in the query are listed .. So for example if you had a query based on Members and there was already something like "WHERE Age < 40 ORDER BY Surname", useing this form you could add something like Surname like "S*" and EMail > "". Obviously pretty complex to regenerate the SQL statement to read WHERE Age < 40 AND Surname like "S*" AND EMail > "" ORDER BY Surname Anyway that all works. That is the new TempNewQuery that I create for subsequent use in Emails or Mail merge Going back to the original posting this is the bit that fails WHERE (((TrophyYear.Year)>=IIf(Forms.ReportSelector.From Year=0,[Year],forms..ReportSelector.FromYear) AND (TrophyYear.Year)<=IIf(Forms.ReportSelector.ToYear =0,[Year],forms.ReportSelector.ToYear)) AND ((TrophyTypes.TrophyTypeSelected)=True)) ; If I paste the SQL from the Debug Window into a new query it works fine, It's just using VBA that I get error 3061 Application-defined or object-defined error. As I said, I have got over the problem by replacing the above with WHERE (((TrophyTypes.TrophyTypeSelected)=True) AND ((TrophyYear.Year)>=GetFromYear() And (TrophyYear.Year)<=GetToYear())) and used a function and similar Function GetToYear() As Long Function GetFromYear() As Long * * If Not IsLoaded("ReportSelector") Then * * * * GetFromYear = Year(Date) * * Else * * * * If Forms!ReportSelector!FromYear = 0 Then * * * * * * GetFromYear = Year(Date) * * * * Else * * * * * * GetFromYear = Forms!ReportSelector!FromYear * * * * End If * * End If End Function Phil |
#7
| |||
| |||
|
|
Wish life were that simple. Query1 in your example almost certainly will contain WHERE clauses and ORDER BY and possibly HAVING etc. |
|
As I said, I have got over the problem by replacing the above with WHERE (((TrophyTypes.TrophyTypeSelected)=True) AND ((TrophyYear.Year)>=GetFromYear() And (TrophyYear.Year)<=GetToYear())) |
#8
| |||
| |||
|
|
On Sat, 14 Apr 2012 09:51:56 +0100, "Phil" <phil (AT) stantonfamily (DOT) co.uk wrote: Wish life were that simple. Query1 in your example almost certainly will contain WHERE clauses and ORDER BY and possibly HAVING etc. Whereas very few of my queries contain Where clauses other than Inactive = false or similar. I will add the WHERE clauses in VBA code or filters. As I said, I have got over the problem by replacing the above with WHERE (((TrophyTypes.TrophyTypeSelected)=True) AND ((TrophyYear.Year)>=GetFromYear() And (TrophyYear.Year)<=GetToYear())) Right. You got bit by the fact that executing a query via VBA code doesn't parse out the Forms!<formname>!<FieldName> separately whereas running those querys in the query, form or reports works just fine. Tony |
#9
| |||
| |||
|
|
Tony, I'll eat my hat. |
|
Live & learn Looks as if I am going to have to review a hell of a lot of queries if I am going to use them as a source of VBA code. Many thanks |
![]() |
| Thread Tools | |
| Display Modes | |
| |