I am new to VBA in Access database, the following code tries to combine two columns in Table_2, but one of the column name needs to be defined by a field value from Table_1, I tried to run the code, but it returns "Error updating: Too few parameters. Expected 1." I am not sure where is the problem.
Appreciate if someone can help. Thanks a lot.
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
UPDATE: The following codes are with actual field names:
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
Below are screenshots of the two tables