Update Recordset in one column if exsist
First I should apologise for my lack of knowledge with Visual Basic.
I have managed to get a function working that updates all fields in a
database if they exist in the current database. It checks to see firstly
if the "Product Reference" field exists and if it does it updates all the
values from the current database.
What I am trying to achieve is to have it only update the field
"nStockOnHand" and leave all the others as is, however I am really not to
great at coding in Visual Basic so any help greatly appreciated.
This is my script so far:
Option Compare Database
Option Explicit
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Function update1()
'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("C:\Users\simon\Documents\SellerDeck
2013\Sites\BGH dest\ActinicCatalog.mdb")
'Open source recordset
Dim rSource As Recordset
Set rSource = dSource.OpenRecordset("Product", dbOpenForwardOnly)
'Open dest recordset
Dim rDest As Recordset
Set rDest = dDest.OpenRecordset("Product", dbOpenDynaset)
'Loop through source recordset
While Not rSource.EOF
'Reset copy flag
bCopy = False
'Look for record in dest recordset
rDest.FindFirst "Product Reference = " & rSource.Fields("Product
Reference") & ""
If rDest.NoMatch Then
Else
'If found, check for differences
For Each fField In rSource.Fields
If rDest2.Fields(fField.Name) <>
rSource.Fields(fField.Name) Then
rDest.Edit
bCopy = True
Exit For
End If
Next fField
Set fField = Nothing
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 finish()
Dim x As Integer
For x = 1 To 10
Sleep 2000
DoEvents
Next x
Application.Quit
End Function
Thanks, Simon
No comments:
Post a Comment