Question : Import and export rows in Access 2003?

Hi,

I have an Access 2003 database application that has tables of linked "static" data that are only editable by the administrator, and several interlinked tables of "user" data that can be customized and added to by users. The database administrator wants to be able to update the "static" data periodically, but does not want the users to lose the data that they've already added. Is there a way to import multiple tables of linked data using VBA?

Thanks to anyone who can help...

...Jay

Answer : Import and export rows in Access 2003?

Hi Jay

Since you asked so nicely...   :-)

The

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
Public Function SynchTableFromMaster( _
  MasterDB As String, _
  TableName As String, _
  PKeyName As String) As Boolean
Const cTempTable = "tmpSynchTable"
Dim db As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim sSQL As String
Dim sSourceFields As String
Dim sDestFields As String
Dim iSynched As Integer
Dim iAdded As Integer
On Error GoTo ProcErr
  Set db = CurrentDb
' first link the master table
On Error Resume Next
' delete the temp linked table if it exists
  db.TableDefs.Delete cTempTable
On Error GoTo ProcErr
  Set tbl = db.CreateTableDef(cTempTable)
  tbl.SourceTableName = TableName
  tbl.Connect = ";DATABASE=" & MasterDB
  db.TableDefs.Append tbl
' construct a SQL string for the update query
  sSQL = "UPDATE " & TableName & " as T inner join " & cTempTable _
    & " as S on T.[" & PKeyName & "]=S.[" & PKeyName & "] SET "
  For Each fld In tbl.Fields
    If fld.Name <> PKeyName Then
      sSQL = sSQL & "T.[" & fld.Name & "]=S.[" & fld.Name & "], "
    End If
  Next fld
  ' remove last ", " and add ";"
  sSQL = Left(sSQL, Len(sSQL) - 2) & ";"
  db.Execute sSQL, dbFailOnError
  iSynched = db.RecordsAffected
' construct a SQL string for the append query
  sSQL = "INSERT INTO " & TableName & " SELECT * FROM " & cTempTable _
    & " where [" & PKeyName & "] not in (select [" & PKeyName _
    & "] from " & TableName & ");"
  db.Execute sSQL, dbFailOnError
  iAdded = db.RecordsAffected
  MsgBox iSynched & " existing records synchronized" _
    & vbCrLf & iAdded & " new records added"
  SynchTableFromMaster = True
ProcEnd:
  On Error Resume Next
  db.TableDefs.Delete cTempTable
  Exit Function
ProcErr:
  MsgBox "Error synching table " & TableName & vbCrLf _
    & vbCrLf & Err.Description
  Resume ProcEnd
End Function
Random Solutions  
 
programming4us programming4us