Link to home
Start Free TrialLog in
Avatar of chansb
chansb

asked on

Record Locking in VB5 (MS SQL 6.5)

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.
Avatar of HOIMEI
HOIMEI

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
Avatar of chansb

ASKER

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
Avatar of chansb

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of cedricd
cedricd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chansb

ASKER

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
   




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

Avatar of chansb

ASKER

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
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.