I am attempting to write code that should perform the following:
- insert a new data row into the ORDERS table
- obtain the new IDENTITY field value (the field name is actually "ordernum" but it is an identity field type) from the ORDERS table
- insert data row(s) into the ORDERSDET table with the corresponding IDENTITY field value
- do all of the above in a nice, neat transaction
I am using VB.2005, PSQL 9.5, and an ODBC connection. My code (at least the first two steps, step two doesn't work) is included below. The insert works fine but I do not know how to include step 2 in the transaction.
Sub Scan_TICKHISH_For_New_Tickets()
Dim fromdate As Long
Dim ticketnum As Long
Dim transdate As Long
Dim newticket As Boolean
Dim linecount As Integer = 1
Dim transcmd As OdbcCommand
Dim transspcmd As OdbcCommand
Dim SOCmd As OdbcCommand
Dim ordernum As Long
Dim SOTransConn As Odbc.OdbcConnection
Try
fromdate = Year(DateAdd(DateInterval.Month, -1, Now)) * 10000 + Month(DateAdd(DateInterval.Month, -1, Now)) * 100 + 1
ASCconn = New OdbcConnection(ASCConnStr)
ASCconn.ConnectionTimeout = 30
ASCconn.Open()
SOconn = New OdbcConnection(SOConnStr)
SOconn.ConnectionTimeout = 30
SOconn.Open()
'SCAN FOR *POSTED* TICKETS THAT HAVE 999 IN SLSP3
'cmdstring = "select ticketnum, transdateyyyymmdd, tickhish.customernum, locationnum,salesperson1,shiptofirstname,shiptolastname,shiptoaddrline1,shiptoaddrline2,shiptocity,shiptostate,shipto1st5zip,firstname,lastname from tickhish,customer where customer.customernum=tickhish.customernum and transdateyyyymmdd>" & fromdate & " and salesperson3=999 and postedflag='Y'"
cmdstring = "select ticketnum, transdateyyyymmdd from tickhish where transdateyyyymmdd>=" & fromdate & " and salesperson3=999 and postedflag='Y'"
Dim ASCCmd As New OdbcCommand(cmdstring, ASCconn)
ASCreader = ASCCmd.ExecuteReader()
'Process The Result Set
While (ASCreader.Read())
'CHECK TO SEE IF THIS TICKET ALREADY EXISTS IN THE SPECIAL ORDERS TABLE
ticketnum = ASCreader("ticketnum")
transdate = ASCreader("transdateyyyymmdd")
cmdstring = "select ticketnum from orders where ticketnum=" & ticketnum & " and transdateyyyymmdd=" & transdate
SOCmd = New OdbcCommand(cmdstring, SOconn)
SOreader = SOCmd.ExecuteReader()
newticket = True
While SOreader.Read
newticket = False
End While
SOreader.Close()
If newticket Then
'open the connection
SOTransConn = New OdbcConnection(SOConnStr)
SOTransConn.ConnectionTimeout = 30
SOTransConn.Open()
' Make the transaction.
Dim SOtrans As Odbc.OdbcTransaction = SOTransConn.BeginTransaction(IsolationLevel.ReadCommitted)
'Make a Command for this connection
' and this transaction.
cmdstring = "INSERT INTO orders (ordernum,transdateyyyymmdd, ticketnum) values ("
cmdstring += "0,"
cmdstring += ASCreader("transdateyyyymmdd") & ","
cmdstring += ticketnum & ");"
transcmd = New OdbcCommand(cmdstring, SOTransConn, SOtrans)
transcmd.ExecuteNonQuery()
cmdstring = "select ordernum from orders where ticketnum=" & ticketnum & " and transdateyyyymmdd=" & transdate
SOCmd = New OdbcCommand(cmdstring, SOconn)
SOreader = SOCmd.ExecuteReader()
While SOreader.Read
ordernum = SOreader("ordernum")
End While
SOreader.Close()
MsgBox(ordernum)
SOtrans.Commit()
SOTransConn.Close()
SOTransConn.Dispose()
End If
End While
ASCreader.Close()
SOconn.Close()
ASCconn.Close()
SOconn.Dispose()
ASCconn.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
Finally
End Try
End Sub
- insert a new data row into the ORDERS table
- obtain the new IDENTITY field value (the field name is actually "ordernum" but it is an identity field type) from the ORDERS table
- insert data row(s) into the ORDERSDET table with the corresponding IDENTITY field value
- do all of the above in a nice, neat transaction
I am using VB.2005, PSQL 9.5, and an ODBC connection. My code (at least the first two steps, step two doesn't work) is included below. The insert works fine but I do not know how to include step 2 in the transaction.
Sub Scan_TICKHISH_For_New_Tickets()
Dim fromdate As Long
Dim ticketnum As Long
Dim transdate As Long
Dim newticket As Boolean
Dim linecount As Integer = 1
Dim transcmd As OdbcCommand
Dim transspcmd As OdbcCommand
Dim SOCmd As OdbcCommand
Dim ordernum As Long
Dim SOTransConn As Odbc.OdbcConnection
Try
fromdate = Year(DateAdd(DateInterval.Month, -1, Now)) * 10000 + Month(DateAdd(DateInterval.Month, -1, Now)) * 100 + 1
ASCconn = New OdbcConnection(ASCConnStr)
ASCconn.ConnectionTimeout = 30
ASCconn.Open()
SOconn = New OdbcConnection(SOConnStr)
SOconn.ConnectionTimeout = 30
SOconn.Open()
'SCAN FOR *POSTED* TICKETS THAT HAVE 999 IN SLSP3
'cmdstring = "select ticketnum, transdateyyyymmdd, tickhish.customernum, locationnum,salesperson1,shiptofirstname,shiptolastname,shiptoaddrline1,shiptoaddrline2,shiptocity,shiptostate,shipto1st5zip,firstname,lastname from tickhish,customer where customer.customernum=tickhish.customernum and transdateyyyymmdd>" & fromdate & " and salesperson3=999 and postedflag='Y'"
cmdstring = "select ticketnum, transdateyyyymmdd from tickhish where transdateyyyymmdd>=" & fromdate & " and salesperson3=999 and postedflag='Y'"
Dim ASCCmd As New OdbcCommand(cmdstring, ASCconn)
ASCreader = ASCCmd.ExecuteReader()
'Process The Result Set
While (ASCreader.Read())
'CHECK TO SEE IF THIS TICKET ALREADY EXISTS IN THE SPECIAL ORDERS TABLE
ticketnum = ASCreader("ticketnum")
transdate = ASCreader("transdateyyyymmdd")
cmdstring = "select ticketnum from orders where ticketnum=" & ticketnum & " and transdateyyyymmdd=" & transdate
SOCmd = New OdbcCommand(cmdstring, SOconn)
SOreader = SOCmd.ExecuteReader()
newticket = True
While SOreader.Read
newticket = False
End While
SOreader.Close()
If newticket Then
'open the connection
SOTransConn = New OdbcConnection(SOConnStr)
SOTransConn.ConnectionTimeout = 30
SOTransConn.Open()
' Make the transaction.
Dim SOtrans As Odbc.OdbcTransaction = SOTransConn.BeginTransaction(IsolationLevel.ReadCommitted)
'Make a Command for this connection
' and this transaction.
cmdstring = "INSERT INTO orders (ordernum,transdateyyyymmdd, ticketnum) values ("
cmdstring += "0,"
cmdstring += ASCreader("transdateyyyymmdd") & ","
cmdstring += ticketnum & ");"
transcmd = New OdbcCommand(cmdstring, SOTransConn, SOtrans)
transcmd.ExecuteNonQuery()
cmdstring = "select ordernum from orders where ticketnum=" & ticketnum & " and transdateyyyymmdd=" & transdate
SOCmd = New OdbcCommand(cmdstring, SOconn)
SOreader = SOCmd.ExecuteReader()
While SOreader.Read
ordernum = SOreader("ordernum")
End While
SOreader.Close()
MsgBox(ordernum)
SOtrans.Commit()
SOTransConn.Close()
SOTransConn.Dispose()
End If
End While
ASCreader.Close()
SOconn.Close()
ASCconn.Close()
SOconn.Dispose()
ASCconn.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
Finally
End Try
End Sub