![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I am still having trouble with the complicated SQL statement that I posted a few days ago. I need to add a further criterion which is the field P.Cancelled (a Yes/No field) to select those with 'yes', those with 'no' or 'all policies'. The relevant criterion is defined in the textbox 'txtWhichPolicies' and will be '0', '-1', or '*'. I have tried lots of different variations of code adding it in where I have placed ???????????????? in the example below all seem either bring back all the records or just freeze the whole thing. Is it because it is a UNION query that I am having the problem? |
#3
| |||
| |||
|
|
I am still having trouble with the complicated SQL statement that I posted a few days ago. I need to add a further criterion which is the field P.Cancelled (a Yes/No field) to select those with 'yes', those with 'no' or 'all policies'. The relevant criterion is defined in the textbox 'txtWhichPolicies' and will be '0', '-1', or '*'. I have tried lots of different variations of code adding it in where I have placed ???????????????? in the example below all seem either bring back all the records or just freeze the whole thing. Is it because it is a UNION query that I am having the problem? Private Sub cmdOK_Click() Dim dbs As Database Dim loForm As Form Dim lrs As DAO.Recordset Dim lsCriteria As String Dim lsSQL As String Dim stDocName As String Dim stPolicyStatus As String stPolicyStatus = txtWhichPolicies On Error GoTo Err_cmdOK_Click '* Attempt to find the Policy Set dbs = CurrentDb lsSQL = "SELECT DISTINCT P.policy_no " _ & "FROM dbo_Policy P, " _ & "dbo_Organisation O " _ & "WHERE P.insured_id = O.organisation_id " _ & "AND P.version_no IN (SELECT MAX(version_no) " _ & "FROM dbo_Policy P " _ & "WHERE P.policy_no = policy_no) " _ & "AND (O.organisation_alias LIKE '*" & txtName & "*'" _ & "OR " _ & "O.organisation_name LIKE '*" & txtName & "*') " lsSQL = lsSQL & "UNION " _ & "SELECT DISTINCT P.policy_no " _ & "FROM dbo_Policy P, " _ & "dbo_CoverSchedule CS, " _ & "dbo_CoverScheduleSiteGroup CSSG, " _ & "dbo_Site S " _ & "WHERE P.policy_no = CS.policy_no " _ & "AND P.version_no = CS.version_no " _ & "AND P.version_no IN (SELECT MAX(version_no) " _ & "FROM dbo_Policy P " _ & "WHERE P.policy_no = policy_no) " _ & "AND CS.policy_no = CSSG.policy_no " _ & "AND CS.version_no = CSSG.version_no " _ & "AND CS.cover_schedule_sequence = CSSG.cover_schedule_sequence " _ & "AND CSSG.site_id = S.site_id " _ & "AND S.site_name LIKE '*" & txtName & "*' " ???????????????? [snip] |
#4
| |||
| |||
|
|
FireyColin wrote: I am still having trouble with the complicated SQL statement that I posted a few days ago. *I need to add a further criterion which is the field P.Cancelled (a Yes/No field) to select those with 'yes', those with 'no' or 'all policies'. *The relevant criterion is defined in the textbox 'txtWhichPolicies' and will be '0', '-1', or '*'. I have tried lots of different variations of code adding it in where I have placed ???????????????? in the example below all seem either bring back all the records or just freeze the whole thing. *Is it because it is a UNION query that I am having the problem? Private Sub cmdOK_Click() Dim dbs As Database Dim loForm As Form Dim lrs As DAO.Recordset Dim lsCriteria As String Dim lsSQL As String Dim stDocName As String Dim stPolicyStatus As String stPolicyStatus = txtWhichPolicies On Error GoTo Err_cmdOK_Click * *'* Attempt to find the Policy * *Set dbs = CurrentDb * *lsSQL = "SELECT DISTINCT P.policy_no " _ * * * * *& "FROM dbo_Policy P, " _ * * * * * * * & "dbo_Organisation O " _ * * * * *& "WHERE P.insured_id = O.organisation_id " _ * * * * * *& "AND P.version_no IN (SELECT MAX(version_no) "_ * * * * * * * * * * * * * * * * & "FROMdbo_Policy P " _ * * * * * * * * * * * * * * * * & "WHERE P.policy_no = policy_no) " _ * * * * * *& "AND (O.organisation_alias LIKE '*" & txtName & "*'" _ * * * * * * * * & "OR " _ * * * * * * * * & "O.organisation_name LIKE '*" & txtName & "*') " * *lsSQL = lsSQL & "UNION " _ * * * * *& "SELECT DISTINCT P.policy_no " _ * * * * *& "FROM dbo_Policy P, " _ * * * * * * * & "dbo_CoverSchedule CS, " _ * * * * * * * & "dbo_CoverScheduleSiteGroup CSSG, " _ * * * * * * * & "dbo_Site S " _ * * * * *& "WHERE P.policy_no = CS.policy_no " _ * * * * * *& "AND P.version_no = CS.version_no " _ * * * * * *& "AND P.version_no IN (SELECT MAX(version_no) "_ * * * * * * * * * * * * * * * * & "FROMdbo_Policy P " _ * * * * * * * * * * * * * * * * & "WHERE P.policy_no = policy_no) " _ * * * * * *& "AND CS.policy_no = CSSG.policy_no " _ * * * * * *& "AND CS.version_no = CSSG.version_no " _ * * * * * *& "AND CS.cover_schedule_sequence = CSSG.cover_schedule_sequence " _ * * * * * *& "AND CSSG.site_id = S.site_id " _ * * * * * *& "AND S.site_name LIKE '*" & txtName & "*' " * * * * * *???????????????? [snip] Try using: * *If Me.txtWhichPolicies = True Or Me.txtWhichPolicies = False Then * * * lsSQL = lsSQL & " AND P.Cancelled=" & Me.txtWhichPolicies * *End If You should also add a space before UNION |
#5
| |||
| |||
|
|
On Dec 6, 5:18*pm, Marshall Barton <marshbar... (AT) wowway (DOT) com> wrote: FireyColin wrote: I am still having trouble with the complicated SQL statement that I posted a few days ago. *I need to add a further criterion which is the field P.Cancelled (a Yes/No field) to select those with 'yes', those with 'no' or 'all policies'. *The relevant criterion is defined in the textbox 'txtWhichPolicies' and will be '0', '-1', or '*'. I have tried lots of different variations of code adding it in where I have placed ???????????????? in the example below all seem either bring back all the records or just freeze the whole thing. *Is it because it is a UNION query that I am having the problem? Private Sub cmdOK_Click() Dim dbs As Database Dim loForm As Form Dim lrs As DAO.Recordset Dim lsCriteria As String Dim lsSQL As String Dim stDocName As String Dim stPolicyStatus As String stPolicyStatus = txtWhichPolicies On Error GoTo Err_cmdOK_Click * *'* Attempt to find the Policy * *Set dbs = CurrentDb * *lsSQL = "SELECT DISTINCT P.policy_no " _ * * * * *& "FROM dbo_Policy P, " _ * * * * * * * & "dbo_Organisation O " _ * * * * *& "WHERE P.insured_id = O.organisation_id " _ * * * * * *& "AND P.version_no IN (SELECT MAX(version_no) " _ * * * * * * * * * * * * * * * * & "FROM dbo_Policy P " _ * * * * * * * * * * * * * * * * & "WHERE P.policy_no = policy_no) " _ * * * * * *& "AND (O.organisation_alias LIKE '*" & txtName & "*'" _ * * * * * * * * & "OR " _ * * * * * * * * & "O.organisation_name LIKE '*" & txtName & "*') " * *lsSQL = lsSQL & "UNION " _ * * * * *& "SELECT DISTINCT P.policy_no " _ * * * * *& "FROM dbo_Policy P, " _ * * * * * * * & "dbo_CoverSchedule CS, " _ * * * * * * * & "dbo_CoverScheduleSiteGroup CSSG, " _ * * * * * * * & "dbo_Site S " _ * * * * *& "WHERE P.policy_no = CS.policy_no " _ * * * * * *& "AND P.version_no = CS.version_no " _ * * * * * *& "AND P.version_no IN (SELECT MAX(version_no) " _ * * * * * * * * * * * * * * * * & "FROM dbo_Policy P " _ * * * * * * * * * * * * * * * * & "WHERE P.policy_no = policy_no) " _ * * * * * *& "AND CS.policy_no = CSSG.policy_no " _ * * * * * *& "AND CS.version_no = CSSG.version_no " _ * * * * * *& "AND CS.cover_schedule_sequence = CSSG.cover_schedule_sequence " _ * * * * * *& "AND CSSG.site_id = S.site_id " _ * * * * * *& "AND S.site_name LIKE '*" & txtName & "*' " * * * * * *???????????????? [snip] Try using: * *If Me.txtWhichPolicies = True Or Me.txtWhichPolicies = False Then * * * lsSQL = lsSQL & " AND P.Cancelled=" & Me.txtWhichPolicies * *End If You should also add a space before UNION That makes sense to me but where in the code should I put it? Sorry if I seem a bit thick but, as you've probably worked out, I'm a bit of an amateur at this., |
#6
| |||
| |||
|
|
FireyColin wrote: On Dec 6, 5:18�pm, Marshall Barton <marshbar... (AT) wowway (DOT) com> wrote: FireyColin wrote: I am still having trouble with the complicated SQL statement that I posted a few days ago. �I need to add a further criterion which is the field P.Cancelled (a Yes/No field) to select those with 'yes', those with 'no' or 'all policies'. �The relevant criterion is defined in the textbox 'txtWhichPolicies' and will be '0', '-1', or '*'. I have tried lots of different variations of code adding it in where I have placed ???????????????? in the example below all seem either bring back all the records or just freeze the whole thing. �Is it because it is a UNION query that I am having the problem? Private Sub cmdOK_Click() Dim dbs As Database Dim loForm As Form Dim lrs As DAO.Recordset Dim lsCriteria As String Dim lsSQL As String Dim stDocName As String Dim stPolicyStatus As String stPolicyStatus = txtWhichPolicies On Error GoTo Err_cmdOK_Click � �'* Attempt to find the Policy � �Set dbs = CurrentDb � �lsSQL = "SELECT DISTINCT P.policy_no " _ � � � � �& "FROM dbo_Policy P, " _ � � � � � � � & "dbo_Organisation O " _ � � � � �& "WHERE P.insured_id = O.organisation_id " _ � � � � � �& "AND P.version_no IN (SELECT MAX(version_no) " _ � � � � � � � � � � � � � � � � & "FROM dbo_Policy P " _ � � � � � � � � � � � � � � � � & "WHERE P.policy_no = policy_no) " _ � � � � � �& "AND (O..organisation_alias LIKE '*" & txtName & "*'" _ � � � � � � � � & "OR " _ � � � � � � � � & "O.organisation_name LIKE '*" & txtName & "*') " � �lsSQL = lsSQL & "UNION " _ � � � � �& "SELECT DISTINCT P.policy_no " _ � � � � �& "FROM dbo_Policy P, " _ � � � � � � � & "dbo_CoverSchedule CS, " _ � � � � � � � & "dbo_CoverScheduleSiteGroup CSSG, " _ � � � � � � � & "dbo_Site S " _ � � � � �& "WHERE P.policy_no = CS.policy_no " _ � � � � � �& "AND P.version_no = CS.version_no " _ � � � � � �& "AND P.version_no IN (SELECT MAX(version_no) " _ � � � � � � � � � � � � � � � � & "FROM dbo_Policy P " _ � � � � � � � � � � � � � � � � & "WHERE P.policy_no = policy_no) " _ � � � � � �& "AND CS..policy_no = CSSG.policy_no " _ � � � � � �& "AND CS..version_no = CSSG.version_no " _ � � � � � �& "AND CS..cover_schedule_sequence = CSSG.cover_schedule_sequence " _ � � � � � �& "AND CSSG.site_id = S.site_id " _ � � � � � �& "AND S.site_name LIKE '*" & txtName & "*' " � � � � � �???????????????? [snip] Try using: � �If Me.txtWhichPolicies = True Or Me.txtWhichPolicies = False Then � � � lsSQL = lsSQL & " AND P.Cancelled=" & Me.txtWhichPolicies � �End If You should also add a space before UNION That makes sense to me but where in the code should I put it? Sorry if I seem a bit thick but, as you've probably worked out, I'm a bit of an amateur at this., Put in place of the line with ???????????????? -- Marsh- Hide quoted text - - Show quoted text - |
#7
| |||
| |||
|
|
On Dec 7, 4:18*pm, Marshall Barton wrote: FireyColin wrote: On Dec 6, 5:18�pm, Marshall Barton wrote: FireyColin wrote: I am still having trouble with the complicated SQL statement that I posted a few days ago. �I need to add a further criterion which is the field P.Cancelled (a Yes/No field) to select those with 'yes', those with 'no' or 'all policies'. The relevant criterion is defined in the textbox 'txtWhichPolicies' and will be '0', '-1', or '*'. I have tried lots of different variations of code adding it in where I have placed ???????????????? in the example below all seem either bring back all the records or just freeze the whole thing. [snip] Try using: If Me.txtWhichPolicies = True Or Me.txtWhichPolicies = False Then lsSQL = lsSQL & " AND P.Cancelled=" & Me.txtWhichPolicies End If I tried that but whichever option I choose it always retrieve all the records. |
#8
| |||
| |||
|
|
FireyColin wrote: On Dec 7, 4:18 pm, Marshall Barton wrote: FireyColin wrote: On Dec 6, 5:18 pm, Marshall Barton *wrote: FireyColin wrote: I am still having trouble with the complicated SQL statement that I posted a few days ago. I need to add a further criterion which is the field P.Cancelled (a Yes/No field) to select those with 'yes', those with 'no' or 'all policies'. * The relevant criterion is definedin the textbox 'txtWhichPolicies' and will be '0', '-1', or '*'. I have tried lots of different variations of code adding it in where I have placed ???????????????? in the example below all seem either bring back all the records or just freeze the whole thing. [snip] Try using: * *If Me.txtWhichPolicies = True Or Me.txtWhichPolicies = False Then * * * lsSQL = lsSQL & " AND P.Cancelled=" & Me.txtWhichPolicies * *End If I tried that but whichever option I choose it always retrieve all the records. That implies txtWhichPolicies is neither True nor False. *I suspect that it's a Text value that might look like 0 or -1, but is just text characters. *I guess you could try changing it to: * *If Me.txtWhichPolicies = "-1" Then * * * lsSQL = lsSQL & " AND P.Cancelled = True" * *ElseIf *Me.txtWhichPolicies = "0" Then * * * lsSQL = lsSQL & " AND P.Cancelled = False" * *End If -- Marsh- Hide quoted text - - Show quoted text - |
#9
| |||
| |||
|
|
On Dec 7, 6:12*pm, Marshall Barton <marshbar... (AT) wowway (DOT) com> wrote: FireyColin wrote: On Dec 7, 4:18 pm, Marshall Barton wrote: FireyColin wrote: On Dec 6, 5:18 pm, Marshall Barton *wrote: FireyColin wrote: I am still having trouble with the complicated SQL statement that I posted a few days ago. I need to add a further criterion which is the field P.Cancelled (a Yes/No field) to select those with 'yes', those with 'no' or 'all policies'. * The relevant criterion is defined in the textbox 'txtWhichPolicies' and will be '0', '-1', or '*'. I have tried lots of different variations of code adding it in where I have placed ???????????????? in the example below all seem either bring back all the records or just freeze the whole thing. [snip] Try using: * *If Me.txtWhichPolicies = True Or Me.txtWhichPolicies = False Then * * * lsSQL = lsSQL & " AND P.Cancelled=" & Me.txtWhichPolicies * *End If I tried that but whichever option I choose it always retrieve all the records. That implies txtWhichPolicies is neither True nor False. *I suspect that it's a Text value that might look like 0 or -1, but is just text characters. *I guess you could try changing it to: * *If Me.txtWhichPolicies = "-1" Then * * * lsSQL = lsSQL & " AND P.Cancelled = True" * *ElseIf *Me.txtWhichPolicies = "0" Then * * * lsSQL = lsSQL & " AND P.Cancelled = False" * *End If Still no joy i'm afraid, it just comes up with 'Too few parameters. Expected 1' |
#10
| |||
| |||
|
|
FireyColin wrote: On Dec 7, 6:12 pm, Marshall Barton <marshbar... (AT) wowway (DOT) com> wrote: FireyColin wrote: On Dec 7, 4:18 pm, Marshall Barton wrote: FireyColin wrote: On Dec 6, 5:18 pm, Marshall Barton wrote: FireyColin wrote: I am still having trouble with the complicated SQL statement that I posted a few days ago. I need to add a further criterion which is the field P.Cancelled (a Yes/No field) to select those with 'yes', those with 'no' or 'all policies'. The relevant criterion is defined in the textbox 'txtWhichPolicies' and will be '0', '-1', or '*'. I have tried lots of different variations of code adding it in where I have placed ???????????????? in the example below all seem either bring back all the records or just freeze the whole thing. [snip] Try using: If Me.txtWhichPolicies = True Or Me.txtWhichPolicies = False Then lsSQL = lsSQL & " AND P.Cancelled=" & Me.txtWhichPolicies End If I tried that but whichever option I choose it always retrieve all the records. That implies txtWhichPolicies is neither True nor False. I suspect that it's a Text value that might look like 0 or -1, but is just text characters. I guess you could try changing it to: If Me.txtWhichPolicies = "-1" Then lsSQL = lsSQL & " AND P.Cancelled = True" ElseIf Me.txtWhichPolicies = "0" Then lsSQL = lsSQL & " AND P.Cancelled = False" End If Still no joy i'm afraid, it just comes up with 'Too few parameters. Expected 1' That's what you get when there is a misspelled field name or the SQL is garbled. *Since the query runs when the text box is neither "0" or "-1" the problem is in the part that's added when it is "0" or "-1". *Inspect that carefully. * If you can't find anything, add the line: * *Debug.Print *lsSQL right before the OpenRecordset line and look at the Immediate window to see the completed SQL statement. *That should allow you to spot what's wrong there and maybe you can back track to what's wrong. -- Marsh- Hide quoted text - - Show quoted text - |
![]() |
| Thread Tools | |
| Display Modes | |
| |