I have wrote some vbscript that updates all new fields in one access database from a table in another database however I am having problems with duplicate primary keys.
I can't change the structure of the database so I can't remove the primary keys however ideally I would like it to auto populate the primary key. Here is my table structure (their are two tables)
Table 'Order':
Order Sequence Number About 20 more rows of data that do not have to be unique
a primary key e.g 2 other data
Table 'OrderDetail':
OrderDetail OrderSequence Some other rows that don't need to be unique
a primary key the key from Order some other data
My first problem is having the primary key for both tables auto populate so they are unique, then my second problem is matching the two rows that are being added and if the primary key changes on 'Order' table for 'Order Sequence Number' update it on 'OrderSequence' in the table 'OrderDetail'.
Here is my vbscript that works copying values if they are unique:
Public Function dhupdate1()
'Temp field
Dim fField As Field
Dim bCopy As Boolean
'Open source database
Dim dSource As Database
Set dSource = CurrentDb
'Open dest database
Dim dDest As Database
Set dDest = DAO.OpenDatabase("\\BMCDONALD-PC\SellerDeck 2013\Sites\New_Site\ActinicCatalog.mdb")
'Open source recordset
Dim rSource As Recordset
Set rSource = dSource.OpenRecordset("OrderDetail", dbOpenForwardOnly)
'Open dest recordset
Dim rDest As Recordset
Set rDest = dDest.OpenRecordset("OrderDetail", dbOpenDynaset)
'Loop through source recordset
While Not rSource.EOF
'Reset copy flag
bCopy = False
'Look for record in dest recordset
rDest.FindFirst "OrderDetailID = " & rSource.Fields("OrderDetailID") & ""
If rDest.NoMatch Then
'If not found, copy record
rDest.AddNew
bCopy = True
End If
'If copy flag is set, copy record
If bCopy Then
For Each fField In rSource.Fields
rDest.Fields(fField.Name) = rSource.Fields(fField.Name)
Next fField
Set fField = Nothing
rDest.Update
End If
'Next source record
rSource.MoveNext
Wend
'Close dest recordset
rDest.Close
Set rDest = Nothing
'Close source recordset
rSource.Close
Set rSource = Nothing
'Close dest database
dDest.Close
Set dDest = Nothing
'Close source database
dSource.Close
Set dSource = Nothing
End Function
Public Function dhupdate2()
'Temp field
Dim fField As Field
Dim bCopy As Boolean
'Open source database
Dim dSource As Database
Set dSource = CurrentDb
'Open dest database
Dim dDest As Database
Set dDest = DAO.OpenDatabase("\\BMCDONALD-PC\SellerDeck 2013\Sites\New_Site\ActinicCatalog.mdb")
'Open source recordset
Dim rSource As Recordset
Set rSource = dSource.OpenRecordset("Order", dbOpenForwardOnly)
'Open dest recordset
Dim rDest As Recordset
Set rDest = dDest.OpenRecordset("Order", dbOpenDynaset)
'Loop through source recordset
While Not rSource.EOF
'Reset copy flag
bCopy = False
'Look for record in dest recordset
rDest.FindFirst "[Order Number] = '" & rSource.Fields("Order Number") & "'"
If rDest.NoMatch Then
'If not found, copy record
rDest.AddNew
bCopy = True
End If
'If copy flag is set, copy record - ignore errors
If bCopy Then
For Each fField In rSource.Fields
On Error Resume Next
rDest.Fields(fField.Name) = rSource.Fields(fField.Name)
On Error GoTo 0
Next fField
Set fField = Nothing
rDest.Update
End If
'Next source record
rSource.MoveNext
Wend
'Close dest recordset
rDest.Close
Set rDest = Nothing
'Close source recordset
rSource.Close
Set rSource = Nothing
'Close dest database
dDest.Close
Set dDest = Nothing
'Close source database
dSource.Close
Set dSource = Nothing
End Function
I have been reading about auto populating if its not unique however I am getting confused where I need these two functions to get both rows for one order and changing both numbers for the order sequence. I am still fairly new to VB so any help is really appreciated.
Thanks, Simon
AutoNumber
fields? – GoudaAutoNumber
and have removed the tags – Dorcy