访问VBA:连接动态列并执行循环

时间:2016-04-12 18:17:52

标签: sql vba ms-access access-vba

我的问题与以下两个名为Table_1和Table_2的Access表有关。

Table_1 enter image description here

以下代码旨在通过连接同一个表的另外两列来更新[Table_2.CombinedField]列。两列中的一列必须是[Table_2.BookName],另一列在Table_1中定义。
例如,正如您在Table_1中看到的那样,新颖的BookType应该使用Author与BookName连接,研究BookType应该使用PublishYear等。这意味着Table_2中用于连接的列基于Table_1。 理想情况下,以下代码的目标结果应为:

CombinedField
tom - titleA
约翰 - titleB
2010年 - titleC
2011年 - 标题D
company5 - titleE

但是,正如您在上面的Table_2截图中看到Table_2.CombinedField。该代码仅使用Table_1的第一行(作者)并适用于Table_2的所有行。

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

我的问题
我想我应该使用类似循环的东西。但是,我真的不知道如何在这种情况下将循环应用于代码。 (对不起,我是VBA新手)。下面的编码只是我的猜测,感谢有人可以帮助指出应该是什么样的确切代码,以便为Table_2.CombinedField生成我的目标结果。非常感谢。

以下代码仅为我的猜测

Function CombineVariableFields_Loop()
On Error Resume Next

  Dim ws As Workspace
  Dim strSQL As String
  Dim fieldname As String
  Set ws = DBEngine.Workspaces(0)
  Set db = CurrentDb()

On Error GoTo Proc_Err
  ws.BeginTrans

    Set rst = db.OpenRecordset("Select distinct SelectCombineField FROM Table_1", dbOpenDynaset)
    With rst
    Do While Not .EOF
       fieldname = DLookup("[SelectCombineField]", "Table_1", "BookType = " & DLookup("BookType", "Table_2"))
       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
       .MoveNext
    Loop
    End With

  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 :(得分:1)

试试这个:

Set rst = db.OpenRecordset("Select SelectCombineField, BookType FROM Table_1 Where BookType In(Select Distinct BookType From Table_2)", dbOpenDynaset)
With rst
Do While Not .EOF
   strSQL = "UPDATE Table_2 SET Table_2.CombinedField =  [Table_2].[" & !SelectCombineField & "] & ' - ' & [Table_2].[BookName] Where BookType = '" & !BookType & "'"
   db.Execute strSQL, dbFailOnError
   .MoveNext
Loop
End With

答案 1 :(得分:0)

这可能不是 BEST 方式,但应该有效:

 "UPDATE Table_2 a INNER JOIN Table_1 b ON a.BookType = b.BookType" _
      & "SET Table_2.CombinedField = " _
           & "Iif(b.[SelectCombinedField = 'Author', a.[Author], " _
               & " Iif(b.[SelectCombinedField = 'PublishYear', a.[PublishYear], " _
                    & "a.[Publisher])) & ' - ' & a.[BookName]"