Solved

Record Locking in VB5 (MS SQL 6.5)

Posted on 1998-12-15
8
376 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now