Question : Delete Function for single record in ADO has one line returns error

Dear Experts,

What do you see as in error about line 20 of this code:

    rsDelete.Open sql, cnn, , , adCmdText ?

Everything seems to work except that one line. Any thoughts?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
Private Sub cmdDelete_Click()

Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
    
Dim sql As String
Dim rsDelete As New ADODB.Recordset

'Build dynamic SQL statement based on
'record selected by user.

sql = "select * from Products where Main_ID = " & _
    Val(Me.txtRecordID.Value)
    
    'Assign updatable cursor and lock type properties.
    rsDelete.CursorType = adOpenDynamic
    rsDelete.LockType = adLockOptimistic
    
    'Open the Recordset object.
    rsDelete.Open sql, cnn, , , adCmdText 
    
    'Don't try to delete the record, if the
    'recordset did not find a row.
    If rsDelete.EOF = False Then
    
        'Update the record based on input from the user.
        With rsDelete
            .Delete
            .Update
            .Close
        End With
        
    'Close the form-level Recordset object and refresh
    'it to include the newly updated row.
    
    rsDelete.Close
    Set rsDelete = Nothing
    
   End If
End Sub

Answer : Delete Function for single record in ADO has one line returns error


why not use a simple method

Dim sql As String
sql = "delete * from Products where Main_ID = " & _
    Val(Me.txtRecordID.Value)

currentdb.execute sql, dbfailonerror
Random Solutions  
 
programming4us programming4us