Selecting a specific value in VBA Excel to Access - excel

I'm having problems to specify a value to start adding fields to my table, i need to insert all the values with the condition of this specific value in the MOD_CODE column from Excel to be inserted in this way to Access.
.Fields("MOD_CODE") = Sheets(wsName).Cells(iX, 2).Value
It inserts all the fields correctly from Excel to Access but i can not make that picks a specific value in this field
Dim Conn As ADODB.Connection, RecSet As ADODB.Recordset
Dim fila As Long, primerFila As Integer, ultimaFila As Long, iX As Long
Dim dataSource As String, Tabla As String
Dim wsName As String
Dim rFound As Range
dataSource = Sheets("parametros").[B1]
Tabla = Sheets("parametros").[B2]
wsName = Sheets("parametros").[B3]
primerFila = Sheets("parametros").[B4]
Set Conn = New ADODB.Connection
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & dataSource & ";"
'Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & dataSource & ";"
Set RecSet = New ADODB.Recordset
RecSet.Open Tabla, Conn, adOpenKeyset, adLockOptimistic, adCmdTable
ultimaFila = WorksheetFunction.CountA(Sheets(wsName).Range("A:A"))
For iX = primerFila To ultimaFila
With RecSet
.AddNew
.Fields("PLANT") = Sheets(wsName).Cells(iX, 1).Value
.Fields("MOD_CODE") = Sheets(wsName).Cells(iX, 2).Value
.Fields("BOM_KEY") = Sheets(wsName).Cells(iX, 3).Value
.Fields("ASSY_ADDRESS") = Sheets(wsName).Cells(iX, 4).Value
.Update
End With
Next iX
RecSet.Close
Set RecSet = Nothing
Conn.Close
Set Conn = Nothing
End Sub

Related

Import from closed workbooks using ADODB

After so many hours in that field, I could be able to get data from all the worksheets in closed workbook and could get data from specific columns using ADODB.
#Siddharth Rout helped me to be able to get the sheet names in the order of tab.
The following code works fine for only one closed workbook. But in fact I am trying to do the same and get all the data from the specific column (Reference - Ref No - Number ..) from several workbooks
Sub ImportFromClosedWorkbook()
Dim e, ws As Worksheet, cn As ADODB.Connection, rs As ADODB.Recordset, rsHeaders As ADODB.Recordset, b As Boolean, sFile As String, shName As String, strSQL As String, iCol As Long
sFile = ThisWorkbook.Path & "\Sample.xlsx"
Dim con As Object
Set con = CreateObject("DAO.DBEngine.120")
Dim rsData As ADODB.Recordset
Set cn = New ADODB.Connection
cn.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & sFile & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"";"
Set ws = ThisWorkbook.ActiveSheet
Dim db As Object, i As Long
Set db = con.OpenDatabase(sFile, False, True, "Excel 12.0 XMl;")
For i = 0 To db.TableDefs.Count - 1
sName = db.TableDefs(i).Name
b = False
strSQL = "SELECT * FROM [" & sName & "]"
Set rsHeaders = New ADODB.Recordset
rsHeaders.Open Source:=strSQL, ActiveConnection:=cn, Options:=1
For iCol = 0 To rsHeaders.Fields.Count - 1
For Each e In Array("Ref No", "Reference", "Number")
If e = rsHeaders.Fields(iCol).Name Then
b = True: Exit For
End If
Next e
If b Then Exit For
Next iCol
If b Then
strSQL = "SELECT [" & e & "] FROM [" & sName & "]"
Set rsData = New ADODB.Recordset
Set rsData = cn.Execute(strSQL)
ws.Range("A" & ws.Cells(Rows.Count, 1).End(xlUp).Row + 1).CopyFromRecordset rsData
rsData.Close
End If
Next i
db.Close: Set db = Nothing
Set con = Nothing
cn.Close: Set cn = Nothing
End Sub
Is it suitable to build a public procedure or what's the best approach in that case and how can I release the objects in correct way?
I would break out your code even more - there are distinct activities which could be factored out into reusable methods.
FYI your tableDefs objects already contains the field names, so there's no need to separately query for those.
Eg:
Sub ImportFromClosedWorkbook()
Dim sFile As String, sheetName As String, colName As String, rs As ADODB.Recordset
Dim cols As Collection, col
sFile = ThisWorkbook.FullName
Set cols = FindColumns(sFile, Array("Ref", "Reference", "RefNo"))
'loop found columns
For Each col In cols
sheetName = col(0)
colName = col(1)
Debug.Print "##", sheetName, colName
Set rs = WorkBookQuery(sFile, "Select [" & colName & "] from [" & sheetName & "]")
If Not rs.EOF Then
' ActiveSheet.Cells(Rows.Count, "A").End(xlUp).CopyFromRecordset rs
End If
Next col
End Sub
'given a workbook path, find all column headings matching andname in arrNames
'returns a collections of [sheetName, columnName] arrays
Function FindColumns(wbFullPath As String, arrNames) As Collection
Dim tabledefs As Object, td As Object, f As Object, rv As New Collection
Set tabledefs = CreateObject("DAO.DBEngine.120") _
.OpenDatabase(wbFullPath, False, True, "Excel 12.0 XMl;").tabledefs
For Each td In tabledefs
For Each f In td.Fields
'Debug.Print td.Name, f.Name
If Not IsError(Application.Match(f.Name, arrNames, 0)) Then
rv.Add Array(td.Name, f.Name)
End If
Next f
Next td
Set FindColumns = rv
End Function
'run a SQL query against a workbook
Function WorkBookQuery(wbFullPath As String, SQL As String) As ADODB.Recordset
Dim rs As ADODB.Recordset
With New ADODB.Connection
.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & wbFullPath & "';" & _
"Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"";"
Set WorkBookQuery = .Execute(SQL, Options:=1)
End With
End Function
There seems to be a logical error in the process of cycling through the fields. It would be nice to use a user-defined function that checks if the field name exists.
Sub ImportFromClosedWorkbook()
Dim e, ws As Worksheet, cn As ADODB.Connection, rs As ADODB.Recordset, rsHeaders As ADODB.Recordset, b As Boolean, sFile As String, shName As String, strSQL As String, iCol As Long
Dim sField As String
sFile = ThisWorkbook.Path & "\Sample.xlsx"
Dim con As Object
Set con = CreateObject("DAO.DBEngine.120")
Dim rsData As ADODB.Recordset
Set cn = New ADODB.Connection
cn.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & sFile & "';" & "Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;"";"
Set ws = ThisWorkbook.ActiveSheet
Dim db As Object, i As Long
Set db = con.OpenDatabase(sFile, False, True, "Excel 12.0 XMl;")
For i = 0 To db.TableDefs.Count - 1
sName = db.TableDefs(i).Name
b = False
strSQL = "SELECT * FROM [" & sName & "]"
Set rsHeaders = New ADODB.Recordset
rsHeaders.Open Source:=strSQL, ActiveConnection:=cn, Options:=1
For iCol = 0 To rsHeaders.Fields.Count - 1
' For Each e In Array("Ref No", "Reference", "Number")
' If e = rsHeaders.Fields(iCol).Name Then
' b = True: Exit For
' End If
' Next e
' If b Then Exit For
' Next iCol
' If b Then
sField = rsHeaders.Fields(iCol).Name
If isField(sField) Then
strSQL = "SELECT [" & sField & "] FROM [" & sName & "]"
Set rsData = New ADODB.Recordset
Set rsData = cn.Execute(strSQL)
ws.Range("A" & ws.Cells(Rows.Count, 1).End(xlUp).Row + 1).CopyFromRecordset rsData
rsData.Close
End If
Next iCol
Next i
db.Close: Set db = Nothing
Set con = Nothing
cn.Close: Set cn = Nothing
End Sub
Function isField(sField As String) As Boolean
Dim vName As Variant, e As Variant
vName = Array("Ref No", "Reference", "Number")
For Each e In vName
If e = sField Then
isField = True
Exit Function
End If
Next e
End Function
If all the files have the same structure and are in a folder, you could use the FileSystemObject reference as below:
"https://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba"
and you could run the existing code in a loop in the file system code, hope that works

Dynamic query call and connection string

Sub SavedConfiguration()
Dim cnn1 As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim iCols As Integer
Const TABLE_NAME = "TableName"
Const DRIVER = "{SQL Server}"
Dim conf
conf = Array("D11", "database", "D12", "server")
Dim wb As Workbook, wsConfig As Worksheet, wsOut As Worksheet
Dim i As Integer, strConn As String, sQry As String
Set wb = ThisWorkbook
Set wsConfig = Worksheets("Settings")
Set wsOut = Worksheets("SavedConfig")
' construct connection string
strConn = "driver=" & DRIVER & ";"
For i = 1 To UBound(conf) Step 2
strConn = strConn & conf(i) & "=" & wsConfig.Range(conf(i - 1)).Value & ";"
Next
' Debug.Print strConn
sQry = Worksheets("SQL-COMMON").Range("B3").Value
' connect to db and run sql
Set cnnl = New ADODB.Connection
cnnl.ConnectionString = strConn
cnnl.ConnectionTimeout = 30
cnnl.Open
wsConfig.Range("H35") = Now
' output
mrs.Open sQry, cnn1
For iCols = 0 To mrs.Fields.Count - 1
wsOut.Cells(1, iCols + 1).Value = mrs.Fields(iCols).Name
Next
Worksheets("SavedConfig").Range("A2").CopyFromRecordset mrs
wsConfig.Range("D35") = Now
mrs.Close
cnn1.Close
End Sub
I have edited the code, am getting unspecified error while executing it
How to make the connection string more dynamic instead of hard coding above, and picking up the values from cells of the worksheet of excel.
How to add the code for showing the Last run start time and date and last run end time and date of the query which is being executed, getting tabulated automatically in the excel cells.
Build the connection string by concatenating each parameter.
Sub Conn2SQL()
Const TABLE_NAME = "TableName"
Const DRIVER = "{SQL Server}"
Dim conf
conf = Array("A12", "database", "A13", "server", _
"A14", "password", "A15", "uid")
Dim wb As Workbook, wsConfig As Worksheet, wsOut As Worksheet
Dim i As Integer, strConn As String, sQry As String
Set wb = ThisWorkbook
Set wsConfig = wb.Sheets("Sheet1")
Set wsOut = wb.Sheets("SavedConfig")
' construct connection string
strConn = "driver=" & DRIVER & ";"
For i = 1 To UBound(conf) Step 2
strConn = strConn & conf(i) & "=" & wsConfig.Range(conf(i - 1)).Value & ";"
Next
'Debug.Print strConn
sQry = "SELECT * FROM [" & TABLE_NAME & "]"
' connect to db and run sql
Dim cnn1 As New ADODB.Connection, mrs As New ADODB.Recordset
Dim iCols As Integer
With cnn1
.ConnectionString = strConn
.ConnectionTimeout = 30
.Open
End With
wsConfig.Range("H35") = "LAST RUN STARTED " & Now
cnn1.Execute "USE " & wsConfig.Range(conf(0))
' output
mrs.Open sQry, cnn1
For iCols = 0 To mrs.Fields.Count - 1
wsOut.Cells(1, iCols + 1).Value = mrs.Fields(iCols).Name
Next
wsOut.Cells(2, 1).CopyFromRecordset mrs 'A2
wsConfig.Range("D35") = "LAST RUN COMPLETED " & Now
i = wsOut.Cells(Rows.Count, 1).End(xlUp).Row
MsgBox i & " rows added to " & wsOut.Name, vbInformation
mrs.Close
cnn1.Close
End Sub
Test the connection string with this code
Sub testconnect()
Dim conn As New ADODB.Connection, rs As New ADODB.Recordset, s As String
s = "" ' put connection string here
With conn
.ConnectionString = s
.ConnectionTimeout = 30
.Open
End With
Set rs = conn.Execute("SELECT CURRENT_TIMESTAMP")
MsgBox "Time is " & rs(0), vbOKOnly
conn.Close
End Sub

Front-End Excel and back-end access update data

I need a small help. I have an Access file name "DB_MLL.accdb" with table name "tblMLL" total 31 columns including primary key. I'm using front-end Excel and back-end access to fetch the data. I have two buttons to pull data from Acess and push back to access. Pulling data from access is working fine but pushing back is not working. I'm using the below codes. request you please guide me where I am doing wrong.
Sub PushTableToAccess()
Dim cnn As ADODB.Connection
Dim MyConn
Dim rst As ADODB.Recordset
Dim i As Variant, j As Variant
Dim Rw As Long
Sheets("Data").Activate
Rw = Range("A65536").End(xlUp).Row
Set cnn = New ADODB.Connection
MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
With cnn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:="tblMLL", ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
Options:=adCmdTable
'Load all records from Excel to Access.
For i = 3 To Rw
rst.AddNew
For j = 1 To 31
If Cells(i, j).Value = "" Then
rst(Cells(2, j).Value) = ""
Else
rst(Cells(2, j).Value) = Cells(i, j).Value
End If
Next j
rst.Update
Next i
' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
MsgBox "Data Upload Completed successfully."
End Sub
Your code seems to have a bit of a fix.
Sub PushTableToAccess()
Dim cnn As ADODB.Connection
Dim MyConn
Dim rst As ADODB.Recordset
Dim i As Variant, j As Variant
Dim Rw As Long
Dim strConn As String
Sheets("Data").Activate
Rw = Range("A65536").End(xlUp).Row
Set cnn = New ADODB.Connection
MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & MyConn
cnn.Open strConn
Set rst = New ADODB.Recordset
wirh rst
.CursorLocation = adUseServer
.Source = "tblMLL"
.Options = adCmdTable
.ActiveConnection = strConn
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Open
'Load all records from Excel to Access.
For i = 3 To Rw
.AddNew
For j = 1 To 31
If Cells(i, j).Value = "" Then
.Fields(Cells(2, j).Value) = ""
Else
.Fields(Cells(2, j).Value) = Cells(i, j).Value
End If
Next j
.Update
Next i
End With
' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
MsgBox "Data Upload Completed successfully."
End Sub

Error 424 when copying a table in access

I'm ridiculously new to SQL. I'm trying to write a bit of code from Excel VBA to reach out to an Access DB file, drop a table, and copy data from another table in access to that first table based on the value of one of the columns.
So far, I'm just trying to copy one table to another and I'm getting 424: Object required. What am I missing?
Sub Update()
Dim cnnAccess As ADODB.Connection
Dim rstAccess As ADODB.Recordset
Set cnnAccess = New ADODB.Connection
Set rstAccess = New ADODB.Recordset
cnnAccess.ConnectionString = "Driver={Microsoft Access Driver (*.mdb,*.accdb)};Dbq= (path to file)\(nameofDB).accdb;Uid=ID;Pwd=PASSWORD;"
cnnAccess.Open
cnn.Execute "Drop Table tbl_daily" <-- Error hits on this line
cnn.Execute "SELECT * From tbl_TMD where date = 42856 INTO tbl_daily FROM
tbl_TMD"
End Sub
Any insight would be very much appreciated.
Try these two scripts below. Also, remember to set a reference to Microsoft DAO x.xx Object Library
Sub DAOCopyFromRecordSet(DBFullName As String, TableName As String, _
FieldName As String, TargetRange As Range)
' Example: DAOCopyFromRecordSet "C:\FolderName\DataBaseName.mdb", _
"TableName", "FieldName", Range("C1")
Dim db As Database, rs As Recordset
Dim intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
Set db = OpenDatabase(DBFullName)
Set rs = db.OpenRecordset(TableName, dbOpenTable) ' all records
'Set rs = db.OpenRecordset("SELECT * FROM " & TableName & _
" WHERE " & FieldName & _
" = 'MyCriteria'", dbReadOnly) ' filter records
' write field names
For intColIndex = 0 To rs.Fields.Count - 1
TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
' write recordset
TargetRange.Offset(1, 0).CopyFromRecordset rs
Set rs = Nothing
db.Close
Set db = Nothing
End Sub
If you want more control with the data import, you can customize the macro below:
Sub DAOFromAccessToExcel(DBFullName As String, TableName As String, _
FieldName As String, TargetRange As Range)
' Example: DAOFromAccessToExcel "C:\FolderName\DataBaseName.mdb", _
"TableName", "FieldName", Range("B1")
Dim db As Database, rs As Recordset
Dim lngRowIndex As Long
Set TargetRange = TargetRange.Cells(1, 1)
Set db = OpenDatabase(DBFullName)
Set rs = db.OpenRecordset(TableName, dbOpenTable) ' all records
'Set rs = DB.OpenRecordset("SELECT * FROM " & _
TableName & " WHERE " & FieldName & _
" = 'MyCriteria'", dbReadOnly) ' filter records
lngRowIndex = 0
With rs
If Not .BOF Then .MoveFirst
While Not .EOF
TargetRange.Offset(lngRowIndex, 0).Formula = .Fields(FieldName)
.MoveNext
lngRowIndex = lngRowIndex + 1
Wend
End With
Set rs = Nothing
db.Close
Set db = Nothing
End Sub

Update Excel from MS Access

I tried the code in this link to push and retrieved the data between Excel and Access. I modified the code based on my file path as following:
EDITED NEW CODE BLOCK
Sub UpdateMDB()
Dim accConn As Object, accRST As Object
Dim accFile As String, accStr As String
Dim lastrow As Long, i As Long
lastrow = Workbooks(1).Sheets(1).Cells(Workbooks(1).Sheets(1).Rows.Count, "A").End(xlUp).Row
accFile = "Z:\Documents\Database\Database1.mdb"
accStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & accFile & ";"
Set accConn = CreateObject("ADODB.Connection")
Set accRST = CreateObject("ADODB.Recordset")
accConn.Open accStr
accRST.Open "SELECT * FROM Table1", accConn, adOpenKeyset, adLockOptimistic, adCmdTableDirect
If Not (accRST.BOF And accRST.EOF) Then
accRST.MoveFirst
Else
MsgBox "No records in Access table.", vbInformation
accRST.Close: accConn.Close: Set accRST = Nothing: Set accConn = Nothing
Exit Sub
End If
Do While Not accRST.EOF
For i = 1 To lastrow
If accRST!ID = Workbooks(1).Sheets(1).Range("A" & i) _
And accRST!Field1 <> Workbooks(1).Sheets(1).Range("B" & i) Then
accRST!Field1.Value = Workbooks(1).Sheets(1).Range("B" & i)
End If
Next i
accRST.Update
accRST.MoveNext
Loop
accRST.Close: accConn.Close
Set accRST = Nothing: Set accConn = Nothing
End Sub
INITIAL CODE BLOCK
Sub GetMDB()
Dim cn As Object
Dim rs As Object
strFile = "Z:\Documents\Database\Database1.mdb"
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile & ";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "SELECT * FROM Table1"
rs.Open strSQL, cn
With Worksheets(1)
For i = 0 To rs.Fields.Count - 1
.Cells(1, i + 1) = rs.Fields(i).Name
Next
rs.MoveFirst
.Cells(2, 1).CopyFromRecordset rs
End With
End Sub
Sub UpdateMDB()
Dim cn As Object
Dim rs As Object
''It would probably be better to use the proper name, but this is
''convenient for notes
strFile = Workbooks(1).FullName
''Note HDR=Yes, so you can use the names in the first row of the set
''to refer to columns
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
''Selecting the cell that are different
strSQL = "SELECT * FROM [Sheet1$] s " _
& "INNER JOIN [;Database=Z:\Documents\Database\Database1.mdb;].Table1 t " _
& "ON s.id=t.id " _
& "WHERE s.Field1<>t.Field1"
rs.Open strSQL, cn, 1, 3 ''adOpenKeyset, adLockOptimistic
''Just to see
''If Not rs.EOF Then MsgBox rs.GetString
''Editing one by one (slow)
rs.MoveFirst
Do While Not rs.EOF
rs.Fields("t.Field1") = rs.Fields("s.Field1")
rs.Update
rs.MoveNext
Loop
''Batch update (faster)
strSQL = "UPDATE [;Database=Z:\Documents\Database\Database1.mdb;].Table1 t " _
& "INNER JOIN [Sheet1$] s " _
& "ON s.id=t.id " _
& "SET t.Field1=s.Field1 " _
& "WHERE s.Field1<>t.Field1 "
cn.Execute strSQL
End Sub
Reading data from Access to Excel GetMDB() macro works fine, But when I tried to update the data from Excel to Access, code gives me following error:
Run-time error '3021':
Either BOF or EOF is True, or the current record has been deleted.
Requested operation requires a current record.
I checked the mdb, xlsx and sheet path and names are correct. Anyone got a similar problem as well and how to overcome? Thanks.
You cannot run UPDATE queries using Excel workbook sources as any SQL queries using workbooks are read-only from last saved instance and cannot be updated. Excel simply is not a database to do such transactions with no record-level locking mechanism, read/write access, or relational model. Though you can run append (INSERT INTO ... SELECT *) and make-table queries (SELECT * INTO FROM ...), you cannot run UPDATE that aligns to live values.
However, you can read in an Access recordset and iterate through the Excel cells aligning by ID matches. Below assumes the Excel Sheet's ID column is in Column A and Field1 is in Column B.
Dim accConn As Object, accRST As Object
Dim accFile As String, accStr As String
Dim lastrow As Long, i As Long
Const adOpenKeyset = 1, adLockOptimistic = 3, adCmdTableDirect = 512
lastrow = Workbooks(1).Sheets(1).Cells(Workbooks(1).Sheets(1).Rows.Count, "A").End(xlUp).Row
accFile = "Z:\Documents\Database\Database1.mdb"
accStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & accFile & ";"
Set accConn = CreateObject("ADODB.Connection")
Set accRST = CreateObject("ADODB.Recordset")
accConn.Open accStr
accRST.Open "SELECT * FROM Table1", accConn, adOpenKeyset, adLockOptimistic, adCmdTableDirect
If Not (accRST.BOF And accRST.EOF) Then
accRST.MoveFirst
Else
Msgbox "No records in Access table.", vbInformation
accRST.Close: accConn.Close: Set accRST = Nothing: Set accConn = Nothing
Exit Sub
End If
Do While Not accRST.EOF
For i = 1 to lastrow
If accRST!ID = Workbooks(1).Sheets(1).Range("A" & i) _
And accRST!Field1 <> Workbooks(1).Sheets(1).Range("B" & i) Then
accRST!Field1.Value = Workbooks(1).Sheets(1).Range("B" & i)
End If
Next i
accRST.Update
accRST.MoveNext
Loop
accRST.Close: accConn.Close
Set accRST = Nothing: Set accConn = Nothing
Notes:
If IDs between Excel worksheet and Access table are not one-to-one (i.e., Excel has multiple rows of same ID), the last Field1 value following the If logic will be inserted to corresponding Access row.
Above may be extensive processing if database rows and Excel cells are large. The best option is simply to use Access for all data entry/management and avoid the update needs. Since Excel is a flatfile, consider using it as the end use application and Access as central data repository.

Resources