不支持连接表达式

时间:2014-02-24 16:16:21

标签: sql vba ms-access

我收到错误3296 JOIN表达式不支持Access中的错误但我似乎无法弄清楚为什么......有什么想法吗?

Public Sub BuildTempTable()
    If IsDebugMode = 0 Then On Error GoTo BuildTempTable_Error

    Dim strSQL As String

    CurrentDb.Execute ("DELETE *FROM tblTempCalendar")

    strSQL = "INSERT INTO tblTempCalendar ( ActivityID, Activity, ActivityCompleted, ActivityTime, ActivityDate, ActivityType, ActivityDesc ) " _
           & " SELECT tblActivity.ActivityID, IIf(IsNull([tblActivity].[ContactID])=False,[tblContact].[LastName] & ', ' & [tblContact].[FirstName] & ', ' & tblContact.Company,IIf(IsNull([tblActivity].[EmployeeID])=False,[tblEmployee].[EmployeeLastName] & ', ' & [tblEmployee].[EmployeeFirstName],'N/A')) AS Activity, tblActivity.ActivityCompleted, tblActivity.ActivityTime, tblActivity.ActivityDate, tblActivity.ActivityType, IIf(IsNull([activitytime]),'',[activitytime] & '   ') & IIf(IsNull([tblActivity].[employeeid]),'','Employee: ' & [tblEmployee].[employeelastname] & ', ' & [tblemployee].[employeefirstname] & '   ') & IIf(IsNull([tblActivity].[ContactID]),'','Contact: ' & [tblContact].[LastName] & ', ' & [tblContact].[FirstName] & '   ') & IIf(IsNull([tblActivity].[Description]),'','Description: ' & [tblActivity].[Description]) AS ActivityDesc " _
           & " FROM ((tblActivity LEFT JOIN tblEmployee ON tblActivity.EmployeeID = tblEmployee.EmployeeID) LEFT JOIN tblEmployee ON tblActivity.EmployeeID = tblEmployee.EmployeeID) "
    strSQL = strSQL & " WHERE format(ActivityDate,'yyyym') = " & intCalYear & intCalMonth
    If Nz(Me.comboFilterCompleted, 999) <> 999 Then
        strSQL = strSQL & " AND ActivityCompleted = " & Me.comboFilterCompleted
    End If
    If Nz(Me.comboFilterType, "(All)") <> "(All)" Then
        strSQL = strSQL & " AND ActivityType = '" & Me.comboFilterType & "'"
    End If
    If Nz(Me.comboFilterEmployee, 0) <> 0 Then
        strSQL = strSQL & " AND tblActivity.EmployeeID = " & Me.comboFilterEmployee
    End If

    If Nz(Me.comboFilterCompleted, 999) <> 999 Or Nz(Me.comboFilterType, "(All)") <> "(All)" Or _
       Nz(Me.comboFilterEmployee, 0) <> 0 Or strRecordSource = "qryActivityListFilter" Then
        Me.cmdShowAll.Visible = True
    Else
        Me.cmdShowAll.Visible = False
    End If

    CurrentDb.Execute (strSQL)

BuildTempTable_Exit:
    Exit Sub

BuildTempTable_Error:
    Call ErrorLog(Err.Description, Err.Number, Me.Name, Erl, "BuildTempTable")
    Resume BuildTempTable_Exit
End Sub

这是即时窗口的输出

INSERT INTO tblTempCalendar ( ActivityID, Activity, ActivityCompleted, ActivityTime, ActivityDate, ActivityType, ActivityDesc )  SELECT tblActivity.ActivityID, IIf(IsNull([tblActivity].[ContactID])=False,[tblContact].[LastName] & ', ' & [tblContact].[FirstName] & ', ' & tblContact.Company,IIf(IsNull([tblActivity].[EmployeeID])=False,[tblEmployee].[EmployeeLastName] & ', ' & [tblEmployee].[EmployeeFirstName],'N/A')) AS Activity, tblActivity.ActivityCompleted, tblActivity.ActivityTime, tblActivity.ActivityDate, tblActivity.ActivityType, IIf(IsNull([activitytime]),'',[activitytime] & '   ') & IIf(IsNull([tblActivity].[employeeid]),'','Employee: ' & [tblEmployee].[employeelastname] & ', ' & [tblemployee].[employeefirstname] & '   ') & IIf(IsNull([tblActivity].[ContactID]),'','Contact: ' & [tblContact].[LastName] & ', ' & [tblContact].[FirstName] & '   ') & IIf(IsNull([tblActivity].[Description]),'','Description: ' & [tblActivity].[Description]) AS ActivityDesc  FROM ((tblActivity LEFT JOIN tblEmployee ON tblActivity.EmployeeID = tblEmployee.EmployeeID) LEFT JOIN tblEmployee ON tblActivity.EmployeeID = tblEmployee.EmployeeID) WHERE format(ActivityDate,'yyyym') = 20142

1 个答案:

答案 0 :(得分:2)

ON条件需要引用唯一的项目。添加“as tblemployee2”并更改ON以引用tblemployee2.employeeid

FROM   ((tblactivity
     LEFT JOIN tblemployee
            ON tblactivity.employeeid = tblemployee.employeeid)
    LEFT JOIN tblemployee  as tblemployee2
           ON tblactivity.employeeid = tblemployee2.employeeid)