使用多条件表单更新表

时间:2019-01-23 06:44:28

标签: ms-access access-vba

我有一个多条件搜索表单,该表单根据过滤器的选择来搜索TblABC中的前20条记录。单击“分配”按钮时,将为用户分配记录。这将更新TblABC中的ID字段(当前为空白)。

当前停留在更新声明中...感谢您的帮助!谢谢!

Private Sub BtnAssign_Click()

Dim strfilter As String

'Filters the form with comboboxes

If Nz(Me.cboIndustry, "") <> "" Then
strfilter = strfilter & "([Major clusters] = '" & Trim(Me.cboIndustry) & "') AND "
End If

If Nz(Me.cboIncorp, "") <> "" Then
    strfilter = strfilter & "([Years Incorporated] = '" & Trim(Me.cboIncorp) & "') AND "
End If

If Nz(Me.cboIndustry, "") <> "" Then
strfilter = strfilter & "([Major clusters] = '" & Trim(Me.cboIndustry) & "') AND "
End If

... ...

'Assign record to User - Updates Tabel ABC

Dim strAssign As String

strAssign = "Update Top 20 * [TblABC] set [ID] = '" & Trim(Me.cboID) & "'"
strAssign = strAssign & " where " & strfilter

DoCmd.RunSQL strAssign

End Sub

“搜索”按钮与此配合使用非常好。它根据用户选择的过滤条件从TblABC中搜索前20条记录。

Private Sub BtnSearch_Click()

Dim strfilter As String 

'Filters the form with comboboxes

If Nz(Me.cboIndustry, "") <> "" Then strfilter = strfilter & "([Major clusters] = '" & Trim(Me.cboIndustry) & "') AND " End If

If Nz(Me.cboIncorp, "") <> "" Then
    strfilter = strfilter & "([Years Incorporated] = '" & Trim(Me.cboIncorp) & "') AND " End If

If Nz(Me.cboIndustry, "") <> "" Then strfilter = strfilter & "([Major clusters] = '" & Trim(Me.cboIndustry) & "') AND " End If

... ...

'Search for Top 20 records based on criteria from TblABC

Dim strwhere As String

strwhere = "Select Top 20 * from [TblABC]" 
strwhere = strwhere & " where " & strfilter

Frm.RecordSource = strwhere

End Sub

1 个答案:

答案 0 :(得分:0)

您不能在TOP n查询中使用UPDATE。这是一个很奇怪的要求。

如果您确实必须执行此操作,则必须执行以下操作:

UPDATE table
SET foo = 'bar'
WHERE table.PrimaryKeyColumn IN
(SELECT TOP 20 PrimaryKeyColumn FROM table
 WHERE <strFilter> 
 ORDER BY <sortColumn> )