为什么更新sql语句不能识别表字段引用

时间:2015-03-06 21:19:15

标签: ms-access

我正在尝试使用UPDATE SQL语句根据另一个字段的值有条件地填充某些表字段。出于某种原因,我收到一条错误,指出它无法找到| 1字段。我检查了表格参考和字段的拼写,但无法弄清楚为什么它找不到它们。代码是(它是引起错误的strUpdateProd行):

Private Sub btnProduct_Click()

    Dim dlg As FileDialog
    Dim strFileName As String
    Dim strUpdateProd As String

    Set dlg = Application.FileDialog(msoFileDialogFilePicker)

    With dlg
        .Title = "Select Audit File to Import"
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Text Files", "*.txt", 1
        If .Show = -1 Then
            strFileName = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With

    DoCmd.RunSQL ("DELETE * FROM tblProductImport")
    DoCmd.TransferText acImportFixed, "Product Import Spec", "tblProductImport", strFileName

    strUpdateProd = "UPDATE [tblProductImport] " _
        & "SET [tblProductImport].[Register] = '" & IIf([tblProductImport].[InvestSplit1] = "R", "RRSP", "TFSA") & "' " _
        & "WHERE [tblProductImport].[HolderID] IS NOT NULL;"

    DoCmd.RunSQL strUpdateProd
    DoCmd.SetWarnings True

End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

strUpdateProd = "UPDATE [tblProductImport] " _
    & "SET [tblProductImport].[Register] = IIf([tblProductImport].[InvestSplit1] = 'R', 'RRSP', 'TFSA') " _
    & "WHERE [tblProductImport].[HolderID] IS NOT NULL;"