PDA

View Full Version : Change data in two tables from form



oxicottin
06-29-2020, 05:19 PM
Hello, I have a form that has no record source. On this form I want to change data from a text box txtChangeVersionNumber for two different tables

tbl-version_fe_master \Field: fe_version_number
tbl-fe_version \Field: fe_version_number

How can I do this?

oxicottin
06-29-2020, 07:11 PM
I figured it out but is there a better way?


Private Sub cmdSaveVersion_Click()
'Required Reference: Microsoft DAO 3.6 Object Library
Dim db As DAO.Database
Dim rs As DAO.Recordset

'Check if there is a version number
If IsNull(Me.txtChangeVersionNumber) Then
MsgBox "A version number must be provided! ", vbCritical, "Entry Error"
Exit Sub
Else

'Update table master version
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl-version_fe_master", dbOpenDynaset)
rs.Edit
rs!fe_version_number = Me.txtChangeVersionNumber
rs.Update
rs.Close
Set rs = Nothing

'Update table version
Set db = CurrentDb
Set rs = db.OpenRecordset("tbl-fe_version", dbOpenDynaset)
rs.Edit
rs!fe_version_number = Me.txtChangeVersionNumber
rs.Update
rs.Close
Set rs = Nothing

Me.txtVersion_fe_master.Requery
Me.txtFe_version.Requery
End If
End Sub

OBP
06-30-2020, 02:31 AM
How do you know which records to update in the 2 tables.
I can't see in your code where you open the appropriate record.
Why use an unbound form, why not a from based on a query with the fields from both tables and update one manually and the other by VBA in the after update event of the updated field?