Hi there
I am a little confused on Error Handler in ADO recordset.
Following codes Work. (so do not be intimitate by the size of it)
Basically How does the Error handler work? I have some Idea about the concept of it, But I am not quite sure “for example, In a situation that we are trying to find out the Duplicate Entries”, does the Error Handler checks for all Updatable Fields or Picks any particular one?
In bellow codes, The Error Handler checks for Duplicate only for “OrderID” field, and does not mind the other ones, Otherwise one can Enter Duplicates in any other fields except the “OrderID”
Private Sub Accept_Click()
On Error GoTo ErrHandler
Dim demoConn As New ADODB.Connection
Dim sqlcmd As New ADODB.Command
Dim r2 As New ADODB.Recordset
Dim demoPath As String
Dim v_OIDID As Integer
Dim v_QIDID As Integer
Dim Tax1 As Double
Dim Tax2 As Double
Dim Total As Double
Dim error_fld As String
Dim msg_warning As String
error_fld = ""
'check for entered values
If IsNull(Me!OID) And IsNull(Me!QID) Then
error_fld = "OIDID and QIDID"
ElseIf IsNull(Me!OID) Then
error_fld = "OIDID"
ElseIf IsNull(Me!QID) Then
error_fld = "QIDID"
Else
v_OIDID = Me!OID
v_QIDID = Me!QID
Tax1 = Me!Tax1
Tax2 = Me!Tax2
Total = Me!Total
End If
If Len(error_fld) > 0 Then
msg_warning = "you have to give a value for " & error_fld & "."
MsgBox msg_warning
Else
demoPath = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=TT2"
demoConn.ConnectionString = demoPath
demoConn.Open
Set sqlcmd.ActiveConnection = demoConn
sqlcmd.CommandText = "Select * From OrdersOrder;"
r2.CursorLocation = adUseClient
r2.Open sqlcmd, , adOpenKeyset, adLockOptimistic
r2.AddNew
r2("OrderID") = v_OIDID
r2("QouteID") = v_QIDID
r2("CustomerID") = CustomerID.Value
r2("CompanyName") = ShipName.Value
r2("OrderDate") = OrderDate.Value
r2("tax1") = Tax1
r2("tax2") = Tax2
r2("Total") = Total
r2.Update
r2.Close
demoConn.Close
Set r2 = Nothing
Set demoConn = Nothing
Set sqlcmd = Nothing
Exit Sub
ErrHandler:
Select Case Err.Number
Case 3219 'operation not allowed
Resume Next
Case -2147217900 'duplicate record
MsgBox "Order ID Has Been Issued Already"
Resume Next
Case Else 'unknown error
MsgBox "Unknown error:" & Err.Number & " " & Err.Description
Resume Next
End Select
End If
End Sub
Best regards
Sanan
I am a little confused on Error Handler in ADO recordset.
Following codes Work. (so do not be intimitate by the size of it)
Basically How does the Error handler work? I have some Idea about the concept of it, But I am not quite sure “for example, In a situation that we are trying to find out the Duplicate Entries”, does the Error Handler checks for all Updatable Fields or Picks any particular one?
In bellow codes, The Error Handler checks for Duplicate only for “OrderID” field, and does not mind the other ones, Otherwise one can Enter Duplicates in any other fields except the “OrderID”
Private Sub Accept_Click()
On Error GoTo ErrHandler
Dim demoConn As New ADODB.Connection
Dim sqlcmd As New ADODB.Command
Dim r2 As New ADODB.Recordset
Dim demoPath As String
Dim v_OIDID As Integer
Dim v_QIDID As Integer
Dim Tax1 As Double
Dim Tax2 As Double
Dim Total As Double
Dim error_fld As String
Dim msg_warning As String
error_fld = ""
'check for entered values
If IsNull(Me!OID) And IsNull(Me!QID) Then
error_fld = "OIDID and QIDID"
ElseIf IsNull(Me!OID) Then
error_fld = "OIDID"
ElseIf IsNull(Me!QID) Then
error_fld = "QIDID"
Else
v_OIDID = Me!OID
v_QIDID = Me!QID
Tax1 = Me!Tax1
Tax2 = Me!Tax2
Total = Me!Total
End If
If Len(error_fld) > 0 Then
msg_warning = "you have to give a value for " & error_fld & "."
MsgBox msg_warning
Else
demoPath = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=TT2"
demoConn.ConnectionString = demoPath
demoConn.Open
Set sqlcmd.ActiveConnection = demoConn
sqlcmd.CommandText = "Select * From OrdersOrder;"
r2.CursorLocation = adUseClient
r2.Open sqlcmd, , adOpenKeyset, adLockOptimistic
r2.AddNew
r2("OrderID") = v_OIDID
r2("QouteID") = v_QIDID
r2("CustomerID") = CustomerID.Value
r2("CompanyName") = ShipName.Value
r2("OrderDate") = OrderDate.Value
r2("tax1") = Tax1
r2("tax2") = Tax2
r2("Total") = Total
r2.Update
r2.Close
demoConn.Close
Set r2 = Nothing
Set demoConn = Nothing
Set sqlcmd = Nothing
Exit Sub
ErrHandler:
Select Case Err.Number
Case 3219 'operation not allowed
Resume Next
Case -2147217900 'duplicate record
MsgBox "Order ID Has Been Issued Already"
Resume Next
Case Else 'unknown error
MsgBox "Unknown error:" & Err.Number & " " & Err.Description
Resume Next
End Select
End If
End Sub
Best regards
Sanan