?
Solved

Record Locking in MS SQL Server 2000 using VB app.

Posted on 2006-03-28
18
Medium Priority
?
214 Views
Last Modified: 2013-12-25
I converted the access database into ms sql server and I am using VB 6.0 app for front end interface. The problem is that I lost the record locking feature. The vb app does not recognize if the record is locked or not. I need to know the best way to have record locking for concurrent processes. Is there any stored procedure available that I can directly call or some other better way? Please let me know. Thanks!
Here is the code I am using for checking if the record is lock or not:

'Test method to determine whether a record is locked or not before editing it
Public Function IsItLocked(rstAny As ADODB.Recordset) As Boolean
    On Error GoTo IsItLocked_Error
    IsItLocked = False
   
    rstAny.Fields(0).Value = rstAny.Fields(0).Value
    Exit Function
   
IsItLocked_Error:
   
    If Err.Number = -2147467259 Then
        IsItLocked = True
        Exit Function
    Else
        MsgBox "Error #" & Err.Number & vbCrLf & Err.Description, vbCritical, "Unknown Error Found!"
    End If
End Function

Public Sub LockRecord(rstAny As ADODB.Recordset)
    ADODBConn.BeginTrans
    gblnInTrans = True
    rstAny.Fields(0).Value = rstAny.Fields(0).Value 'record should lock occur here
   
End Sub
0
Comment
Question by:US-IT
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 11

Expert Comment

by:leclairm
ID: 16311596
What lock type is your recordset using??
0
 

Author Comment

by:US-IT
ID: 16311658
It is  'adLockPessimistic' and the cursor type is 'adOpenKeyset'.

thanks!
0
 
LVL 11

Expert Comment

by:leclairm
ID: 16312422
I don't have books online on the computer I'm at now, but I know there is a locking hints section that explains how to lock records directly in your query.

Here's a head start:
http://www.mssqlcity.com/Articles/Adm/SQL70Locks.htm
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:US-IT
ID: 16312606
It's a great site, but I need a quicker solution. Do you have a working code in VB to invoke a lock and check if it is locked?
0
 
LVL 11

Expert Comment

by:leclairm
ID: 16312957
You can use the UPDLOCK locking with your select which will ensure nothing changes during your transaction.  I would try using begin trans / commit trans with a boolean variable that is set to true during your transaction that has a lock on the row/table and it's set to false after the commit trans.

I've never done this before though, so I'm not sure of any other methods of doing this.

More info...

http://www.informit.com/articles/article.asp?p=27020&rl=1
0
 

Author Comment

by:US-IT
ID: 16313337
How would I determine whether the record is locked or not?
0
 
LVL 11

Expert Comment

by:leclairm
ID: 16313417
You would have to pass the lock mode in your select statement, so I would imagine your lockrecord function should work.  Not so sure about the IsItLocked function though.

Sorry I couldn't be much more help...
0
 

Author Comment

by:US-IT
ID: 16313700
I tried and it did not work. thanks anyway. Is anyone else can be able to help in this problem?
0
 
LVL 4

Expert Comment

by:g_johnson
ID: 16314848
I don't think you can read if a SQL table has a lock applied.  If your application owns the database and/or table, you can set up another table to keep track of your locks so that your program doesn't attempt to do something you don't want it to do.  E.g., if user "A" is updating Table "T", don't let user "B" update table "T"

Does that help?
0
 

Author Comment

by:US-IT
ID: 16315154
Well, I am able read a record while another client is updating it. I applied this trick before, but did not find it efficient.  This adds more complexity and problems. I want to keep it simple. Any other suggestions?
0
 

Author Comment

by:US-IT
ID: 16315167
Also, I am able to update it while someone else is editing it.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16325284
If you are going to resort to editing in a recordset instead of using INSERT/UPDATE/DELETE statements than you are going to have to add a flag in the record to indicate whether the rows is being edited or not.  You will also have to allow for the user that "went to lunch" and left the row "locked"
0
 

Author Comment

by:US-IT
ID: 16365746
well, how would the client knows if the record is locked. Is there any stored procedure that can provide this information?
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 16377563
The simple answer is you do not.  You can only assume that if the row is "locked" for more than x number of minutes than it is time to "unlock" it.  This can be done manually or a job that runs every x minutes to clear all old locks.

I am afraid to say, that is the best you can do with recordset editing.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17664767
I believe I answered the question.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month15 days, left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question