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 55 56 57 58 59 60 61 62 63 64
| Public Sub AccedPortfolio()
Dim cn As ADODB.Connection
Dim rs, rs2 As ADODB.Recordset
Dim sDUNS As String, sSupplierName As String, sRiskScore As Variant
Dim sSupplierID As String, sFY As String, sQuickRatio As Variant
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
"Data Source=C:\Users\290866\Desktop\vba\MS Access\Database2.accdb;"
' open a recordset
Set rs = New ADODB.Recordset
Set rs2 = New ADODB.Recordset
rs.Open "CompanyInformation", cn, adOpenKeyset, adLockOptimistic, adCmdTable
rs2.Open "All_FY", cn, adOpenKeyset, adLockOptimistic, adCmdTable
Range("A2").Activate ' row 1 contains column headings
Do While Not IsEmpty(ActiveCell)
sDUNS = ActiveCell.Value
sSupplierName = ActiveCell.Offset(0, 1).Value
sRiskScore = ActiveCell.Offset(0, 2).Value
sFY = ActiveCell.Offset(0, 48).Value
sSupplierID = ActiveCell.Offset(0, 49).Value
sQuickRatio = ActiveCell.Offset(0, 50).Value
rs.Filter = "DUNS='" & sDUNS & "' AND SupplierName='" & sSupplierName & "'"
If rs.EOF Then
Debug.Print "No existing record - adding new..."
rs.Filter = ""
rs.AddNew
rs("DUNS").Value = sDUNS
rs("SupplierName").Value = sSupplierName
Else
Debug.Print "Existing record found..."
End If
rs("RiskScore").Value = sRiskScore
rs.Update
Debug.Print "...record update complete."
rs2.Filter = "FY='" & sFY & "' AND SupplierName='" & sSupplierName & "'"
If rs2.EOF Then
rs2.AddNew
rs2.Fields("FY") = sFY
Else
Debug.Print "Existing record found..."
End If
rs2("QuickRatio").Value = sQuickRatio
rs2.Update
Debug.Print "...record update complete."
ActiveCell.Offset(1, 0).Activate ' next cell down
Loop
rs.Close
Set rs = Nothing
rs2.Close
Set rs2 = Nothing
cn.Close
Set cn = Nothing
End Sub |
Partager