访问VBA - 更新变量列(参数太少)

时间:2016-04-10 20:22:21

标签: sql vba ms-access access-vba

我是Access数据库中VBA的新手,以下代码尝试在Table_2中组合两列,但其中一个列名需要由Table_1中的字段值定义,我试图运行代码,但它返回“更新错误:参数太少。预计1.”我不确定问题出在哪里。

感谢有人可以提供帮助。非常感谢。

Function test()
On Error Resume Next
  Dim strSQL As String
  Dim  As String
  Dim  txtValue As String
  txtValue = Table_1![Field_A]

  Set ws = DBEngine.Workspaces(0)
  Set db = ws.Databases(0)

On Error GoTo Proc_Err
  ws.BeginTrans

  strSQL = "UPDATE Table_2 INNER JOIN Table_1 ON Table_2.id = Table_1.id SET Table_2.Field_Y = Table_2!txtValue & Table_2![Field_Z]"
  db.Execute strSQL, dbFailOnError

  ws.CommitTrans

Proc_Exit:
  Set ws = Nothing
  Set db = Nothing
  Exit Function

Proc_Err:
  ws.Rollback
  MsgBox "Error updating: " & Err.Description
  Resume Proc_Exit
End Function

更新:以下代码包含实际字段名称:

Function CombineVariableFields()
On Error Resume Next

  Dim ws As Workspace
  Dim strSQL As String
  Dim fieldname As String
  fieldname = Table_1![SelectCombineField]

  Set ws = DBEngine.Workspaces(0)
  Set db = ws.Databases(0)

On Error GoTo Proc_Err
  ws.BeginTrans

  strSQL = "UPDATE Table_2 INNER JOIN Table_1 ON Table_2.BookType = Table_1.BookType SET Table_2.CombinedField = [Table_2]!fieldname & [Table_2]![BookName]"
  Debug.Print strSQL
  db.Execute strSQL, dbFailOnError

  ws.CommitTrans

Proc_Exit:
  Set ws = Nothing
  Exit Function

Proc_Err:
  ws.Rollback
  MsgBox "Error updating: " & Err.Description
  Resume Proc_Exit
End Function

以下是两个表的屏幕截图

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:1)

尝试使用DLookup Function并将结果与​​Update字符串连接以获得预期的命令。

编辑:
您还可以打开记录集,动态构建更新并在循环中执行它。例如:

Set rst = db.OpenRecordset("Select distinct SelectCombinetField FROM Table_1", dbOpenDynaset)
with rst 
  do while not .eof
     strSQL = "UPDATE Table_2 SET Table_2.[" & !SelectCombinetField & "] = (select txtValue from Table_1 where SelectCombinetField = '" & SelectCombinetField & "' and id = Table_2.id Where somting....)"
     db.Execute strSQL, dbFailOnError
     .MoveNext
  Loop
end with

如果只是示例,因为我的所有描述都不清楚。

答案 1 :(得分:0)

正如@ adam-silenko所建议的那样,我使用了DLookup功能,然后修复了“太少参数错误”。这是我使用的代码。它修复了错误,但它产生了另一个问题,我在另一个问题上提出了here

Function CombineVariableFields_NoLoop()
On Error Resume Next

  Dim ws As Workspace
  Dim strSQL As String
  Dim fieldname As String

  fieldname = DLookup("[SelectCombineField]", "Table_1") 

  Set ws = DBEngine.Workspaces(0)
  Set db = CurrentDb()

On Error GoTo Proc_Err
  ws.BeginTrans

  strSQL = "UPDATE Table_2 INNER JOIN Table_1 ON Table_2.BookType = Table_1.BookType SET Table_2.CombinedField = [Table_2]![" & fieldname & "] & ' - ' & [Table_2]![BookName]" 
  db.Execute strSQL, dbFailOnError

  ws.CommitTrans

Proc_Exit:
  Set ws = Nothing
  Exit Function

Proc_Err:
  ws.Rollback
  MsgBox "Error updating: " & Err.Description
  Resume Proc_Exit

End Function

答案 2 :(得分:0)

DLookup的例子:

strSQL = "UPDATE Table_2 " _
  & "SET Table_2.CombinedField = DLookup(" _
    & "DLookup(""[SelectCombineField]""" _
      & ", ""Table_1""" _
      & ", ""BookType = """""" & Table_2.BookType & """""""")" _
    & ", ""[Table_2]""" _
    & ", ""Table_2_ID = "" & Table_2.Table_2_ID)" _ 'you must replace Table_2_ID with proper value
    & " & "" - "" & [Table_2]![BookName]" 
db.Execute strSQL, dbFailOnError

但这是一个较慢的解决方案。使用Recordset的解决方案更好。