Solved

Record Locking in VB5 (MS SQL 6.5)

Posted on 1998-12-15
8
379 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 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