C
Coderz
Hi,
I have a table called key_master. This table looks like:
TableName NextNo <== This is a column
Customers 22
Users 9
Payments 11
The NextNo column saves a value that auto increments each time a user
clicks the Add New Record in my application.
Since I came from VB 6.0 and would like to port all my code to VB.NET
2008 I just want to know what is the best way of incrementing a value
using .NET?
This is the code I am using in VB 6
'Call a procedure called getIndex to get the current value and
increment by 1:
PK = getIndex("Customers")
Public Function getIndex(ByVal srcTable As String) As Long
On Error GoTo err
Dim RS As New Recordset
Dim RI As Long
RS.CursorLocation = adUseClient
RS.Open "SELECT * FROM [KEY_MASTER] WHERE TableName = '" &
srcTable & "'", CN, adOpenStatic, adLockOptimistic
RI = RS.Fields("NextNo")
CN.BeginTrans
RS.Fields("NextNo") = RI + 1
RS.Update
CN.CommitTrans
getIndex = RI
srcTable = ""
RI = 0
Set RS = Nothing
Exit Function
err:
''Error when incounter a null value
If err.Number = 94 Then
getIndex = 1
Resume Next
Else
MsgBox err.Description
End If
CN.RollbackTrans
End Function
Thanks for any help.
http://www.sourcecodester.com - Download free source code
I have a table called key_master. This table looks like:
TableName NextNo <== This is a column
Customers 22
Users 9
Payments 11
The NextNo column saves a value that auto increments each time a user
clicks the Add New Record in my application.
Since I came from VB 6.0 and would like to port all my code to VB.NET
2008 I just want to know what is the best way of incrementing a value
using .NET?
This is the code I am using in VB 6
'Call a procedure called getIndex to get the current value and
increment by 1:
PK = getIndex("Customers")
Public Function getIndex(ByVal srcTable As String) As Long
On Error GoTo err
Dim RS As New Recordset
Dim RI As Long
RS.CursorLocation = adUseClient
RS.Open "SELECT * FROM [KEY_MASTER] WHERE TableName = '" &
srcTable & "'", CN, adOpenStatic, adLockOptimistic
RI = RS.Fields("NextNo")
CN.BeginTrans
RS.Fields("NextNo") = RI + 1
RS.Update
CN.CommitTrans
getIndex = RI
srcTable = ""
RI = 0
Set RS = Nothing
Exit Function
err:
''Error when incounter a null value
If err.Number = 94 Then
getIndex = 1
Resume Next
Else
MsgBox err.Description
End If
CN.RollbackTrans
End Function
Thanks for any help.
http://www.sourcecodester.com - Download free source code