Link to home
Start Free TrialLog in
Avatar of Clothahump
Clothahump

asked on

VB6, ADO, Oracle and Identities

I have a VB6 application that writes a record into an Oracle database.  The record's primary key is the employee id, which is an identity field.  However, after updating the record, I cannot retrieve the key in order to add subsequent child records.

VB6 using ADO 2.8
Connecting via an ODBC DSN that uses sqora32.dll (Oracle in OraHome92).
Transactions enabled.


Here's the code:

With rsEmp
    If bAdding Then
        .AddNew
    End If
    !namefirst = txtFname
    !namelast = txtLname
    lblEmpID.Caption = lCurrentEmp
    !hiredate = txtDateHired
    If IsDate(txtTermDate) Then
        !termdate = txtTermDate
    End If
    If chkSignature = 1 Then
        !issignonfile = "Y"
    Else
        !issignonfile = "N"
    End If
   
    If chkResume = 1 Then
        !iscv = "Y"
    Else
        !iscv = "N"
    End If
    If chkJobDesc = 1 Then
        !ISJOBDESC = "Y"
    Else
        !ISJOBDESC = "N"
    End If
    flexJobTitle.Row = 1
    !jobcode = sJobCode
    !empstatus = cmbStatus
    !emptype = cmbEmpType
    !reason = txtReason
    !Comments = txtComments & ""
    !Name = txtLname & ", " & txtFname
    .Update
    If bAdding Then
' crs working here - not getting the correct empid
        lCurrentEmp = !empid


At this point, lCurrentEmp is set to 0, as the empid field has not updated.  rsEmp was opened as:

    If OpenMode = READWRITE Then
        .CursorType = adOpenDynamic
        .LockType = adLockOptimistic
        .CursorLocation = adUseServer

Do I need to commit the transaction to get the record to update (the BeginTransaction was executed earlier in the code)?  If not, how do I get the value of the employee id after the new record is written?

Thanks in advance!




Avatar of newyuppie
newyuppie
Flag of Ecuador image

i think you have to commit the transaction in order to get the record to update.
Avatar of Clothahump
Clothahump

ASKER

Okay.  I found the solution.  Requerying will force the update.

I change the code as follows:

    .Update
    If bAdding Then
' refresh the table to force the update.  The new record added should now be the first
' in the table since we are ordered by empid desc
        .Requery
        .MoveFirst
        lCurrentEmp = !empid

This does what I want it to.
ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

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