Sub UpdateRecordFromAnotherTable
Dim oDS As Object
Dim oConnection As Object
Dim oStatement As Object
Dim sSQL As String
'This does not work from button push event or running from macro tool. I get a variable not defined error
oDS = ThisDatabaseDocument.CurrentController
oDS.connect()
oConnection = oDS.ActiveConnection()
'The following works when called from a button push event
' Get the current database connection
' oConnection = ThisComponent.Parent.DataSource.getConnection("","")
' Create a SQL statement objectPa
oStatement = oConnection.createStatement()
' Construct the SQL UPDATE statement
' Replace "TargetTable", "TargetColumn", "SourceTable", "SourceColumn", "JoinColumn", and "Condition"
' with your actual table and column names.
sSQL = "UPDATE ""BidItemDetailT"" " & _
" SET ""Description"" = (SELECT ""ResorceT"".""Description"" FROM ""ResorceT"" " & _
"WHERE ""ResorceT"".""Resorce_ID"" = ""BidItemDetailT"".""Resorce_ID"") " & _
"WHERE ""BidItemDetailT"".""Resorce_ID"" = 2"
'Set mulitple fields value from another table
'sSQL = "UPDATE ""BidItemDetailT"" " & _
' " set ""BidItemDetailT"".""Description"" = ""ResorceT"".""Description"", " & _
' " ""BidItemDetailT"".""Price"" = ""ResorceT"".""Price"" "& _
' "From ""ResorceT"" WHERE ""BidItemDetailT"".""Resorce_ID"" = 8 "
' "WHERE ""BidItemDetailT"".""Resorce_ID"" = 8"
' Execute the SQL statement
oStatement.executeUpdate(sSQL)
' Close the connection (optional, but good practice if not immediately reused)
'oConnection.close()
MsgBox "Record updated successfully!", 0, "Update Status"
End Sub
this is the whole sub routine the parts that are commented out don’t work. The second sSQL string is where i am trying to copy some of the values from another table.
Are attachments possible?