Updating a table in Access

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,345
I am creating have a multi-user application in Excel that writes data to, and reads data from, an Access database. There is a master data table in Access that can be updated by any of these users. However, I suspect that if one user is updating the table, then others will not be able to. In order to avoid problems, I would like to know how to tell if a table is 'locked', so that, if it is, I can put a delay in the updating code that waits for it to be free again - a bit like (in Excel)

Code:
1:
Workbooks.Open Filename:="blah blah.xls"
If ActiveWorkbook.ReadOnly = True Then
ActiveWorkbook.Close False
GoTo 1
End If

MY VB code to update the table in Access is
Code:
Sub add_data()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
strDB = Range("file_location").Value ' name of Acess databse
table_name = Range("Table").Value ' name of table in the database
Sheets("Data").Activate
ActiveSheet.AutoFilterMode = False
 lr = [a1].CurrentRegion.Rows.Count
var1 = Cells(1, 1): var2 = Cells(1, 2): var3 = Cells(1, 3): var4 = Cells(1, 4)
'Open the connection
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open strDB
End With
'Define the recordset
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
'**********************************************
'Open the table
rst.Open Source:=table_name, ActiveConnection:=cnn, CursorType:=adOpenDynamic, _
LockType:=adLockOptimistic, Options:=adCmdTable
'**********************************************
For r = 2 To lr
rst.AddNew
rst(var1) = Cells(r, 1)
rst(var2) = Cells(r, 2)
rst(var3) = Cells(r, 3)
rst(var4) = Cells(r, 4)
rst.Update
 Next r
rst.Close
cnn.Close

End Sub

Anyone advise on how to do this, please? I an fairly new to Access.
Cheers :biggrin:
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Alan2005

New Member
Joined
Oct 12, 2005
Messages
22
Hi pcc,

Well, there certainly is a way. It is all to do with how you describe the connection (I seem to remember...!)
I have read about this in one of a myriad of books on Excel / Access.
However, I cannot remember the full details now. I will look this up for you
tonight and revert tom, hopefully.
I do remember though that you do not have to do any fancy looping.
It is purely to do with the connection. i.e. It will store any changes and synchronise once available...that sort of thing.

Sorry can't be more specific this evening.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,236
Messages
5,571,050
Members
412,359
Latest member
misstoffeepenny
Top