Solved

Record Locking in VB5 (MS SQL 6.5)

Posted on 1998-12-15
8
383 Views
Last Modified: 2013-12-25
I am using the VB5 reading MS SQL 6.5.
My Code as bellow:-

mysql = "begin transaction icm"
IMSDatabase.Execute mysql, 64

sqlselect = "select item_code, qty1, cost1, qty2, cost2, qty3, cost3,"
sqlselect = sqlselect + "qty4, cost4, qty5, cost5 from icm_cost_lot (tablockx) where "
sqlselect = sqlselect + "item_code = '" + TempItemCode2 + "'"
Set item_cost = IMSDatabase.OpenRecordset(sqlselect, 2, 64)


mysql = "update icm_cost_lot set qty" + Trim(Combo_LotNo) + " = qty" + Trim(Combo_LotNo) + " + " + Format(MainQuantity, "0.00")
mysql = mysql + " where item_code = '" + TempItemCode2 + "'"
IMSDatabase.Execute mysql, 64

mysql = "commit transaction icm"
IMSDatabase.Execute mysql, 64

This locking method is working in VB4 but in VB5, there is no error message and the locking is not working.

Anybody know the correct way of record locking in VB5 , please reply me. Thanks in advice.
0
Comment
Question by:chansb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 

Expert Comment

by:HOIMEI
ID: 1498315
Hi chansb,
    The better to way is browse through the recordset item_cost,
Check the condition of records one by one and assign the new value  of the field.  In this way, the recordset will automatically lock the record /page according to the parameter of openrecordset.
    e.g while not item_cost.EOF
                  if item_cost!itemcode = TempItemCode2 then
                       item_cost![field_name]= [new value]
                  end if
                  item_cost.movenext
          wend
Hope this help!

May
0
 

Author Comment

by:chansb
ID: 1498316
Hello HOIMEI,
Sorry that this answer is not what I want. My quention is how to lock the record in sql 6.5 database using the VB5 before we update the record.

Please note that the method I use is SQL pass thru statement.

regards,
chansb
0
 

Author Comment

by:chansb
ID: 1498317
The locking is only start after the line (execute) and before commit.

mysql = "update icm_cost_lot set qty" + Trim(Combo_LotNo) + " = qty" + Trim(Combo_LotNo) + " + " + Format(MainQuantity, "0.00")
mysql = mysql + " where item_code = '" + TempItemCode2 + "'"
THIS LINE --> IMSDatabase.Execute mysql, 64

IF in VB4 this record is lock the moment openrecordset.
eg:- Set item_cost = IMSDatabase.OpenRecordset(sqlselect, 2, 64).


regards,
chansb

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 2

Accepted Solution

by:
cedricd earned 50 total points
ID: 1498318
open a recordset and edit it (vb will lock the recordset's records)
set item_cost = db.openrecordset(sql,dbopendynaset,0,dbpessimistic)
dbpessimistic : lock the records on the edit method

item_cost.edit
0
 

Author Comment

by:chansb
ID: 1498319
Hi cedricd,
The locking still after the update execte line.

Dim G_WrkSpace as workspace
Dim IMSdatabase as database

G_SQLDB = "ODBC;DATABASE=IMS;UID=sa;PWD=;DSN=IMS;"
Set G_WrkSpace = Dbengine.workspaces(0)
Set IMSDatabase = g_wrkspace.OpenDatabase("", False, False, G_SQLDB)

G_WrkSpace.BeginTrans

sqlselect = "select item_code, qty1, cost1, qty2, cost2, qty3, cost3,"
sqlselect = sqlselect + "qty4, cost4, qty5, cost5 from icm_cost_lot where " 
sqlselect = sqlselect + "item_code = '" + TempItemCode2 + "'"
Set item_cost = IMSDatabase.OpenRecordset(sqlselect, dbOpenDynaset, 0, dbPessimistic)


mysql = "update icm_cost_lot set qty" + Trim(Combo_LotNo) + " = qty" + Trim(Combo_LotNo) + " + " + Format(MainQuantity, "0.00")
    mysql = mysql + " where item_code = '" + TempItemCode2 + "'"
item_cost.edit
THIS LINE --> IMSDatabase.Execute mysql, 64
item_cost.Update
g_wrkspace.CommitTrans
   




0
 
LVL 2

Expert Comment

by:cedricd
ID: 1498320
try this then :

set g_wrkspace = createworkspace("Name","","",dbuseodbc)
Set IMSDatabase = g_wrkspace.OpenDatabase("", False, False, G_SQLDB)

G_WrkSpace.BeginTrans

sqlselect = "select item_code, qty1, cost1, qty2, cost2, qty3, cost3,"
sqlselect = sqlselect + "qty4, cost4, qty5, cost5 from icm_cost_lot where " 
sqlselect = sqlselect + "item_code = '" + TempItemCode2 + "'"
Set item_cost = IMSDatabase.OpenRecordset(sqlselect, dbOpenDynaset, 0, dbPessimistic --> locked on edit (or dboptimistic to lock on update)


mysql = "update icm_cost_lot set qty" + Trim(Combo_LotNo) + " = qty" + Trim(Combo_LotNo) + " + " + Format(MainQuantity, "0.00")
    mysql = mysql + " where item_code = '" + TempItemCode2 + "'"
item_cost.edit

IMSDatabase.Execute mysql, dbFailOnError (as it's use on a transaction, it's locked until the committrans)
      
item_cost.Update (locked)
g_wrkspace.CommitTrans

0
 

Author Comment

by:chansb
ID: 1498321
Hi cedricd,
set g_wrkspace = createworkspace("Name","","",dbuseodbc)
Set IMSDatabase = g_wrkspace.OpenDatabase("", False, False, G_SQLDB)

G_WrkSpace.BeginTrans

sqlselect = "select item_code, qty1, cost1, qty2, cost2, qty3, cost3,"
sqlselect = sqlselect + "qty4, cost4, qty5, cost5 from icm_cost_lot where " 
sqlselect = sqlselect + "item_code = '" + TempItemCode2 + "'"
Set item_cost = IMSDatabase.OpenRecordset(sqlselect, dbOpenDynaset, 0, dbPessimistic)


mysql = "update icm_cost_lot set qty" + Trim(Combo_LotNo) + " = qty" + Trim(Combo_LotNo) + " + " + Format(MainQuantity, "0.00")
    mysql = mysql + " where item_code = '" + TempItemCode2 + "'"
DID NOT LOCK AT HERE -> item_cost.edit

START LOACK AT HERE-> IMSDatabase.Execute mysql, dbFailOnError

item_cost.Update
g_wrkspace.CommitTrans
0
 
LVL 2

Expert Comment

by:cedricd
ID: 1498322
normally, during the update command, the records which there are being affected by this will be locked.
if it doesn't work then open a recordset like
set rs = db.openrecordset("Select qty from icm_cost_lot where item_code = '"+tempitemcode2"'",dbopendynaset,0,dbpessimistic)
while ...
    rs.edit
    rs("qty")=...
    rs.update
wend

it's a little less fast but you control the locking yourself.
it's all i can propose you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

738 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