Link to home
Start Free TrialLog in
Avatar of jlcannon
jlcannon

asked on

Add another JOIN to a sql statement

I have a button on a form that in the click event is points to a sub that builds a sql statement for the data i need. currently the click event is as below but I thought i might be able to add some additional data by editing the first section where it initially builds the sql by adding on more line,
 sql = sql & " AND JOIN tblImpactedTeams ON T_EVC_UE.[Event#] = tblImpactedTeams.[Event#] "
 to what is already there which is
sql = "SELECT T_EVC_UE.*, [SumOfImpact] AS [Customer Impact] "
    sql = sql & "FROM T_EVC_UE LEFT JOIN qryImpactTotal ON T_EVC_UE.[Event#] = qryImpactTotal.[Event#]"

but in adding that line i put a watch on the sql statement and a stop just after it is built and copy it to a blank query and it pops a message saying missing operator. the result is:

SELECT T_EVC_UE.*, [SumOfImpact] AS [Customer Impact] FROM T_EVC_UE LEFT JOIN qryImpactTotal ON T_EVC_UE.[Event#] = qryImpactTotal.[Event#] AND JOIN tblImpactedTeams ON T_EVC_UE.[Event#] = tblImpactedTeams.[Event#] ;

Private Sub Command173_Click()
On Error GoTo Err_Command173_Click

    Dim oApp As Object
    Dim mywhere As String
    Dim mylen As Integer
    Dim sql As String

  '  Set oApp = CreateObject("Excel.Application")
  '  oApp.Visible = True
    'Only XL 97 supports UserControl Property
  '  On Error Resume Next
  '  oApp.UserControl = True
    
    sql = "SELECT T_EVC_UE.*, [SumOfImpact] AS [Customer Impact] "
    sql = sql & "FROM T_EVC_UE LEFT JOIN qryImpactTotal ON T_EVC_UE.[Event#] = qryImpactTotal.[Event#]"
    sql = sql & " AND JOIN tblImpactedTeams ON T_EVC_UE.[Event#] = tblImpactedTeams.[Event#]"
  
    If InStr(1, Me.RecordSource, "WHERE") <> 0 Then
  
        mylen = 0
        mylen = InStr(1, Me.RecordSource, "ORDER") - InStr(1, Me.RecordSource, "WHERE")
        mywhere = Mid(Me.RecordSource, InStr(1, Me.RecordSource, "WHERE"), mylen)
        sql = sql & " " & mywhere & ";"
    Else
        sql = sql & ";"
    End If
    
    Me.sql.Value = sql

    Dim stDocName As String
    Dim stLinkCriteria As String
    
    If InStr(1, sql, "Team") > 0 Then
        sql = "SELECT T_EVC_UE.*, qryImpactTotal.SumOfImpact AS [Customer Impact] "
        sql = sql & "FROM (T_EVC_UE LEFT JOIN qryImpactTotal ON T_EVC_UE.[Event#] = qryImpactTotal.[Event#]) LEFT JOIN tblImpactedTeams ON T_EVC_UE.[Event#] = tblImpactedTeams.[Event#] "
        mylen = 0
        mylen = InStr(1, Me.RecordSource, "ORDER") - InStr(1, Me.RecordSource, "WHERE")
        mywhere = Mid(Me.RecordSource, InStr(1, Me.RecordSource, "WHERE"), mylen)
        sql = sql & " " & mywhere & ";"
        Me.sql.Value = sql
    End If
    
        stDocName = "frmAllInfo"
        DoCmd.OpenForm stDocName, acFormDS, , stLinkCriteria
    
Exit_Command173_Click:
    Exit Sub

Err_Command173_Click:
    MsgBox Err.Description
    Resume Exit_Command173_Click
    
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GRayL
GRayL
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jlcannon
jlcannon

ASKER

thank you
Thanks glad to help.