Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2009, 11:33 AM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default ADO not locking records

Hi everyone ... this is my first post...
INTRODUCTION
I am an old time programmer who has been pushed into writting a Sales, Purchasing, stock, etc system in Access2007.

The last time I programmed anything was 2001 (remember Y2K).

I have done quite a lot of programming in VB4 and VB6 for stand alone programs using SAM files but ADO database stuff is all new to me.

BACKGROUND
The current situation is theat I have all the modules in place.
The edit/update forms are UNBOUND using VBA to read the records and populate the control.
The write functions read the control value and update the database.
Today I have taken my single database file and split between Back-End and Front-End.
We do not have a Server, just a networked disk-drive.
Everyhing has worked and three users can access and update the Back-End database simutaneously.

**** BIG PROBLEM ****
I am using the structure below to read and lock the records.
This code DOES NOT APPEAR TO LOCK THE RECORD.
In that I can update the same record on two PCs and trample over the first record to be updated.
If I use a bound screen I do get a warning on the second write.

Can anyone give me any pointers or is it just that I can lock the records unless we use a Server?
'================================================= =========================================
Function aisSOPOrderHdrADOReadRecord() As Boolean
Dim conAlexIS As ADODB.Connection
Set conAlexIS = CurrentProject.Connection
Dim rstRecord As New ADODB.Recordset
Dim strSQL As String
Dim intErrId As Integer
intErrId = 0
aisSOPOrderHdrADOReadRecord = True

On Error GoTo ErrorHandler
intErrId = 1
'establish the connection to the table
rstRecord.ActiveConnection = conAlexIS

intErrId = 2
'open the table via a query
strSQL = "SELECT * FROM tblSOPOrderHdr" _
& " WHERE [Order] = " & Me.txtOrder.Value & ""
rstRecord.Open strSQL _
, conAlexIS _
, adOpenDynamic _
, adLockPessimistic
' I have also tried using adLockOptimistic
intErrId = 3
If rstRecord.EOF Then
aisSOPOrderHdrADOReadRecord = False
Else
aisSOPOrderHdrADOReadRecord = True
intErrId = 4
Me.txtOrder.Value = rstRecord![Order]
Me.txtAcntNo.Value = rstRecord![AcntNo]
: : : :
: : : :
Me.txtVATRate.Value = rstRecord![VATRate] * 100#
Me.txtVATAmount.Value = rstRecord![VATAmount]
Me.cmbStatus.Value = rstRecord![Status]
End If ' If rstRecord.EOF Then...
MsgBox "Record Read - Waiting", vbOKOnly, "{aisSOPOrderHdrADOReadRecord}"
intErrId = 4
rstRecord.Close

Set rstRecord = Nothing
Set conAlexIS = Nothing
intErrId = -1
Exit Function
ErrorHandler:
Select Case Err.Number
Case 94 ' invalid use of null
' No Action Required
Resume Next
Case Else
Call UnresolvedError(Me.Name, "{aisSOPOrderHdr ADOReadRecord}", intErrId, "Failed")
aisSOPOrderHdrADOReadRecord = False
End Select
End Function
'================================================= ============================================
Thanks in anticipation
 
Old June 25th, 2009, 08:38 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

There is a lot of extra code in there it seems. I would try this at least:

Code:
'Dim conAlexIS As ADODB.Connection
Dim rstRecord As ADODB.Recordset
Dim strSQL As String
...
strSQL = "SELECT * FROM tblSOPOrderHdr " & _
               "WHERE [Order] = " & Me.txtOrder.Value    'is this a number or text? 
 
Set rstRecord = New ADODB.Recordset
rstRecord.Open strSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic
 
...
If you are using CurrentProject.Connection as your connection string, you don't need to declare or set it, just state it in the recordset.open line. Then VBA will autosense your cursor and lock.

Generally, I will use a separate ADO connection (DSN or Provider) for UPDATE, INSERT, DELETE on records, and only use CurrentProject.Connection when using SELECT statements. I think there is a problem with locking, especially with Jet. If you link SQL Server tables, you can only reliably use SELECT with CurrentProject.Connection.

Also, you can do this:

Code:
 
If rstRecord.EOF = True AND rstRecord.BOF = True Then
   aisSOPOrderHdrADOReadRecord = False
Else
   aisSOPOrderHdrADOReadRecord = True
   intErrId = 4
Me.txtOrder.Value = rstRecord![Order]
Me.txtAcntNo.Value = rstRecord![AcntNo]
: : : :
: : : :
Me.txtVATRate.Value = rstRecord![VATRate] * 100#
Me.txtVATAmount.Value = rstRecord![VATAmount]
Me.cmbStatus.Value = rstRecord![Status]
End If
Did that help?
__________________
mmcdonal

Look it up at: http://wrox.books24x7.com

Last edited by mmcdonal; June 26th, 2009 at 12:17 PM.. Reason: Add code
 
Old July 6th, 2009, 06:52 AM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mmcdonal View Post
... I think there is a problem with locking, especially with Jet. If you link SQL...

Did that help?
Hi Sorry for the long delay in replying... been pulled on and off the project

I have tried you suggestions but still have the problem.
According to the online help...

adLockPessimistic
Data cannot be changed by other users while locked
also Prevents other users from accessing data while locked

adLockOptimistic
Other users able to access data.
also Data can be changed by multiple users at once

I tried both lock settings and niether managed to lock the record against update by another user.

If you open the table in access and try to update the record on the second PC the record is locked, but this is not recognized by the ADO routines.

I also tried using ADO perssimistic locks on both PCs and could quite happily trample over the other users updates.
The result is that which ever record is written last is saved in the database.

I haven't tried using DAO.

As there are relatively few screens/forms I need to protect I will create a lock table, just to lock the primary record key e.g. Order No, GRN No, etc., in the database and control the locking myself.

Thanks for your help I will let you know if I find a way to use ADO reliably.

Stuart

Last edited by twentysixdays; July 6th, 2009 at 06:56 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
ADO vs DAO pessimistic locking knowledge76 Access VBA 3 December 3rd, 2008 12:17 PM
ADO Record Locking pjohanne VB Databases Basics 3 March 9th, 2006 05:42 AM
ADO and updating records tcarnahan Access 2 June 11th, 2004 09:26 AM
ADO Locking Skittle Pro VB Databases 9 November 14th, 2003 11:54 AM
locking of records vincentc Classic ASP Professional 4 August 19th, 2003 04:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.