Updating a table in Access

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,348
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:
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,553
Messages
5,838,075
Members
430,527
Latest member
MyFace2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top