Back To Top


MS Access VBA Update Field From Joined Table Function For versions 2000, 2002, 2003, 2007

Go here To Update a field in the Query Builder.
Go here To Get all Fields in a Table.
Go here To Check if Table exists in VBA.
Go here To Check if Field exists in VBA.

 

The VBA Update Field From Joined Table Sample is available for download here in a zipped MS Access Database. The below picture (click to enlarge) shows the Single form and module.

MS Access Form 

The Actual VBA Update Field From Joined Table Code , you should be able to copy/paste right into a module:


Function
updateJoinedTable(Table1 As String , Field1 As String , _
      Table2
As String , Field2 As String , WhereStrValue As String , _
      strValue
As String , JoinTable1 As String , JoinTable2 As String ) As Boolean

  
' Updates a Field2 in Table2 Joined to another Table1 Given Criteria
   ' From a Field1 in Table1
   '
   ' Accepts
   ' Table1: Name of table in which the Field1 is located
   ' Field1: Name of the field to update
   ' Table2: Name of table in which the Field2 is located
   ' Field2: Name of the field with given criteria
   ' WhereStrValue: Given Criteria
   ' strValue: String to update Field1 to
   ' Join1: Field on Table1 to join
   ' Join2: Field on Table2 to Join
   ' Returns True on success, false otherwise
   'USAGE: updateField "Table1", "Field1", Table2, "Field2", "WhereStrValue", 
"strValue", "Join1", "Join2"
  
On Error GoTo errhandler
  
Dim strSql As String , Db As dao.Database


  
'Create the Update SQL Code from our Four String Values
   'Create the Update SQL Code from our Four String Values
  
strSql = "UPDATE " & Table1 & _
        
" INNER JOIN " & Table2 & " ON [" & Table1 & "]." & JoinTable1 & " = [" &  Table2 & "]." & JoinTable2 & _
        
" Set [" & Table2 & "]." & Field2 & " = '" & strValue & "'" & _
        
" WHERE ((([" & Table1 & "]." & Field1 & ")='" & WhereStrValue & "'));" 

  
'Print the SQL so we can paste into the query builder if there are errors
  
Debug.Print strSql 

  
'Use Current Database
  
Set Db = CurrentDb() 

  
'Run the SQL Query
  
Db.Execute strSql

  
'If no errors return true
  
updateJoinedTable = True
ExitHere:

  
Set Db = Nothing

  
'Notify the user the process is complete.
  
MsgBox "Update Complete" & updateJoinedTable
  
Exit Function

errhandler:
  
'There is an error return false
  
updateJoinedTable = False
   With
Err
      MsgBox
"Error " & .Number & vbCrLf & .Description, _
            vbOKOnly
Or vbCritical, "updateJoinedTable"
  
End With
   Resume
ExitHere
End Function


For the above VBA Update Field From a Joined Table Function in a usable Form with Update Button, download the Sample database in ZIP format. The sample includes two tables to test on.

You may post this tutorial on your website or in a forum. If you do please maintain a Link to Eraseve AP.



For further MS Access tutorials go here: MS Access tutorials

Go here To Change a Field's Data Type.

 

Did you find what you were looking for?
What would you suggest?
mail@eraserve.com
Your Name:
Your Message:
Your Email:

    

 

Custom Search