Hello,
I am using ADO to create my delete code in an Access Project. The form I am using is a list box with contract numbers. If I wish to delete one I simply select it a message box pops up asking me whether I would like to delete the number I say yes. Now it does the delete the selected but I have not been able to figure out why it also delete the first record. Any ideas I would greatly appreciate.
Here is my code:
Private Sub cmdDelete_Click()
Dim rs As New ADODB.Recordset
Dim strContNo As String
'Get contract Number
Form_frmEnterContNo.lstContNo.SetFocus
strContNo = Form_frmEnterContNo.lstContNo.ItemData(Form_frmEnterContNo.lstContNo.ListIndex) & "'"
' Check to make sure contract was selected
If strContNo = "" Then
MsgBox "You must select a Contract Number to delete!", vbInformation
Exit Sub
End If
rs.Open "Select * from tblContractNo where ContractNo='" & strContNo, CurrentProject.Connection, _
adOpenStatic, adLockOptimistic
With rs
' Check record status
If .RecordCount > 1 Then
MsgBox "You are trying to delete more than record", vbCritical
ElseIf .RecordCount = 0 Then
.MoveFirst
MsgBox "Contract Number " & strContNo & " not found in database!", vbInformation
ElseIf vbYes = MsgBox("Are you sure you want to delete " & strContNo & "?", vbYesNo) Then
' Delete record
Do
.Delete
.MoveNext
'--Will use if needed--
'If .EOF Then
'.MovePrevious
'If .BOF Then
'MsgBox "The recordset is empty"
'End If
'End If
'End If--
Loop Until .EOF
End If
'Close record set
.Close
End With
' cleanup:
Set rs = Nothing
End Sub
I am using ADO to create my delete code in an Access Project. The form I am using is a list box with contract numbers. If I wish to delete one I simply select it a message box pops up asking me whether I would like to delete the number I say yes. Now it does the delete the selected but I have not been able to figure out why it also delete the first record. Any ideas I would greatly appreciate.
Private Sub cmdDelete_Click()
Dim rs As New ADODB.Recordset
Dim strContNo As String
'Get contract Number
Form_frmEnterContNo.lstContNo.SetFocus
strContNo = Form_frmEnterContNo.lstContNo.ItemData(Form_frmEnterContNo.lstContNo.ListIndex) & "'"
' Check to make sure contract was selected
If strContNo = "" Then
MsgBox "You must select a Contract Number to delete!", vbInformation
Exit Sub
End If
rs.Open "Select * from tblContractNo where ContractNo='" & strContNo, CurrentProject.Connection, _
adOpenStatic, adLockOptimistic
With rs
' Check record status
If .RecordCount > 1 Then
MsgBox "You are trying to delete more than record", vbCritical
ElseIf .RecordCount = 0 Then
.MoveFirst
MsgBox "Contract Number " & strContNo & " not found in database!", vbInformation
ElseIf vbYes = MsgBox("Are you sure you want to delete " & strContNo & "?", vbYesNo) Then
' Delete record
Do
.Delete
.MoveNext
'--Will use if needed--
'If .EOF Then
'.MovePrevious
'If .BOF Then
'MsgBox "The recordset is empty"
'End If
'End If
'End If--
Loop Until .EOF
End If
'Close record set
.Close
End With
' cleanup:
Set rs = Nothing
End Sub