|
|
Question : Doesn't Work on Linked Tables
|
|
The following code works if the tables are in the same database. I get a "3219-Invalid Option" when the tables are linked to another database.
What to do?
Public Function UpdateOrAddEmployeeRecords() On Error GoTo Err_UpdateOrAddEmployeeRecords_Click
Dim DB As DAO.Database, CO As DAO.Recordset, RO As DAO.Recordset Dim RETVAL As Variant, SEQ As Long, COUNTRECS As Long, MSGTXT As String
Set DB = CurrentDb() Set CO = DB.OpenRecordset("tblTraining_P", dbOpenTable) Set RO = DB.OpenRecordset("tblEmployees", dbOpenTable) RO.Index = "PrimaryKey" ' specify the key field in your target table
' next 5 lines get the source table recordcount and set up a progress bar so you can follow how your process is going CO.MoveLast CO.MoveFirst COUNTRECS = CO.RecordCount iNumberOfPRecords = COUNTRECS MSGTXT = "Adding Employee Records........." RETVAL = SysCmd(acSysCmdInitMeter, MSGTXT, COUNTRECS)
' now loop through source recordset and use SEEK to see if the target exists ' if it does (Not RO.NoMatch) then edit the existing record ' if it does not, insert a target record
Do Until CO.EOF RO.Seek "=", CO![fldSSN] ' search for exact match on Key Field
If Not RO.NoMatch Then RO.Edit RO![fldLastName] = CO![fldLastName] RO![fldFirstName] = CO![fldFirstName] RO![fldMiddleInitial] = CO![fldMiddleInitial] If CO![fldSupervisorStatus] = "No" Then RO![fldSupervisorStatus] = 0 Else RO![fldSupervisorStatus] = -1 End If 'RO![fldSupervisorStatus] = CO![fldSupervisorStatus] RO![fldSubOrganizationIndex] = CO![fldSubOrganizationIndex] RO![fldStatus] = CO![fldStatus] RO.Update Else RO.AddNew RO![fldSSN] = CO![fldSSN] RO![fldLastName] = CO![fldLastName] RO![fldFirstName] = CO![fldFirstName] RO![fldMiddleInitial] = CO![fldMiddleInitial] If CO![fldSupervisorStatus] = "No" Then RO![fldSupervisorStatus] = 0 Else RO![fldSupervisorStatus] = -1 End If 'RO![fldSupervisorStatus] = CO![fldSupervisorStatus] RO![fldSubOrganizationIndex] = CO![fldSubOrganizationIndex] RO![fldStatus] = CO![fldStatus] RO.Update End If
' update the progress bar and get next record from source SEQ = SEQ + 1 RETVAL = SysCmd(acSysCmdUpdateMeter, SEQ) CO.MoveNext Loop
Exit_UpdateOrAddEmployeeRecords_Click: ' now tidy up and release variables CO.Close RO.Close DB.Close Set CO = Nothing Set RO = Nothing Set DB = Nothing RETVAL = SysCmd(acSysCmdRemoveMeter) Exit Function
Err_UpdateOrAddEmployeeRecords_Click: If Err.Number = 3021 Then MsgBox "There are no P records to process." Else MsgBox "Update Or Add Employee Records module error " & Err.Number & ", " & Err.Description End If iMsg = 1 Resume Exit_UpdateOrAddEmployeeRecords_Click
End Function
|
Answer : Doesn't Work on Linked Tables
|
|
1. You need to use dbOpenDynaset instead of dbOpenTable. 2. You can't Seek on a linked table. Use the Findfirst method.
|
|
|
|
|