Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete code is deleting selected record and first record

Status
Not open for further replies.

access8

Programmer
Jun 21, 2002
27
US
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. :D 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'm still stuck on this code has anyone been able to figure out why it does this?
 
I think it has to do with the itemdata and listindex. I'm still trying to figure it out. Please let me know if any of you have found the solution to my problem.

Thank you :D
 
Can anyone pleeeze help me on this code I'm really stuck. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top