A family of Microsoft relational database management systems designed for ease of use.
Hello all,
I figured it out. I just called the tables separately and then after I populated the first table, I pulled the last record number, populated it in a variable, and then added that variable into the second table's EmployeeID field.
rstEmployee.Open "tblEmployees", cnn, adOpenKeyset, adLockOptimistic
rstWorkInfo.Open "tblWorkInfo", cnn, adOpenKeyset, adLockOptimistic
'Add new record to each table and copy fields from Word document
With rstEmployee
.AddNew
!Name = doc.FormFields("Name").Result
!Street = doc.FormFields("Street").Result
!City = doc.FormFields("City").Result
!State = doc.FormFields("State").Result
!Zip = doc.FormFields("Zip").Result
!Phone = doc.FormFields("Phone").Result
.Update
End With
rstEmployee.Close
Set rstEmployee = Nothing
lngEmployeeID = DLast("[EmployeeID]", "tblEmployees")
With rstWorkInfo
.AddNew
!EmployeeID = lngEmployeeID
!Salary = doc.FormFields("Salary").Result
!Title = doc.FormFields("Title").Result
!Active = doc.FormFields("Status").Result
.Update
End With
rstWorkInfo.Close
Set rstWorkInfo = Nothing
I would not expect this to work reliably, because DLast doesn't really return the "last", or most recently added, value from a table. It's fairly arbitrary unless there's a explicit ordering in place for the records it's looking at. Apparently it has worked for you so far, maybe because Access used the default primary-key ordering of the table as a matter of convenience, but I wouldn't want to rely on it, and there's no guarantee that it will continue to bring back the EmployeeID you just added. This would be a better approach:
'------ start of code ------
With rstEmployee
.AddNew
!Name = doc.FormFields("Name").result
!Street = doc.FormFields("Street").result
!City = doc.FormFields("City").result
!State = doc.FormFields("State").result
!Zip = doc.FormFields("Zip").result
!Phone = doc.FormFields("Phone").result
.Update
' Get the employee ID from the record we just added.
.Bookmark = .LastModified
lngEmployeeID = !EmployeeID
End With
rstEmployee.Close
Set rstEmployee = Nothing
With rstWorkInfo
.AddNew
!EmployeeID = lngEmployeeID
!Salary = doc.FormFields("Salary").result
!Title = doc.FormFields("Title").result
!Active = doc.FormFields("Status").result
.Update
End With
rstWorkInfo.Close
Set rstWorkInfo = Nothing
'------ end of code ------