S
Sandy
I recently purchased a book "Beginning Access 2003 VBA" in an attempt to
learn more about - naturally enough - Access VBA.
The book gives examples but this particular one has an error (I think). If I
delete the first record then all is fine however if I delete the last
record then I receive :
"Run-time error ('3021')
Either BOF or EOF is True, or the current record has been deleted. Requested
operation requires a current record."
I have indicated the problem piece of code between aterisks.
Code :-
Sub DeleteRecord()
'don't let the user issue a delete command if in add mode
If blnAddMode = True Then
Exit Sub
End If
Dim intResponse As Integer
'confirm that user really wants to delete record
intResponse = MsgBox("Are you sure you want to delete this record?",
vbYesNo)
'if the user cancels delete, then exit this procedure
If intResponse = vbNo Then
Exit Sub
End If
'declare and create new command object
Dim cmdCommand As ADODB.Command
Set cmdCommand = New ADODB.Command
'create a new connection instance and open it using the connection string
Set cnCh5 = New ADODB.Connection
cnCh5.Open strConnection
'declare variable to store current contact
Dim intCurContact As Integer
intCurContact = 0
'generate SQL command to delete current record
Dim strSQL As String
strSQL = "DELETE FROM tblContacts WHERE intContactId = " &
rsContacts!intContactId
'set the command to the current connection
Set cmdCommand.ActiveConnection = cnCh5
'set the delete SQL statement to the command text
cmdCommand.CommandText = strSQL
'execute the delete command against the database
cmdCommand.Execute
***************************************
'move to the next record in the local recordset since the
'current one is being deleted
If Not rsContacts.EOF Then
rsContacts.MoveNext
'save the id of the current (next) record
intCurContact = rsContacts!intContactId
End If
****************************************
'while connected to the database, go ahead and
'repopulate the recordset to make sure it contains
'the most current values from the database.
Set rsContacts.ActiveConnection = cnCh5
rsContacts.Requery
Set rsContacts.ActiveConnection = Nothing
'move back to the contact that was current before the
'requery
rsContacts.Find "[intContactId] = " & intCurContact
'populate the controls on the form
Call PopulateControlsOnForm
End Sub
Thanks
Sandy
learn more about - naturally enough - Access VBA.
The book gives examples but this particular one has an error (I think). If I
delete the first record then all is fine however if I delete the last
record then I receive :
"Run-time error ('3021')
Either BOF or EOF is True, or the current record has been deleted. Requested
operation requires a current record."
I have indicated the problem piece of code between aterisks.
Code :-
Sub DeleteRecord()
'don't let the user issue a delete command if in add mode
If blnAddMode = True Then
Exit Sub
End If
Dim intResponse As Integer
'confirm that user really wants to delete record
intResponse = MsgBox("Are you sure you want to delete this record?",
vbYesNo)
'if the user cancels delete, then exit this procedure
If intResponse = vbNo Then
Exit Sub
End If
'declare and create new command object
Dim cmdCommand As ADODB.Command
Set cmdCommand = New ADODB.Command
'create a new connection instance and open it using the connection string
Set cnCh5 = New ADODB.Connection
cnCh5.Open strConnection
'declare variable to store current contact
Dim intCurContact As Integer
intCurContact = 0
'generate SQL command to delete current record
Dim strSQL As String
strSQL = "DELETE FROM tblContacts WHERE intContactId = " &
rsContacts!intContactId
'set the command to the current connection
Set cmdCommand.ActiveConnection = cnCh5
'set the delete SQL statement to the command text
cmdCommand.CommandText = strSQL
'execute the delete command against the database
cmdCommand.Execute
***************************************
'move to the next record in the local recordset since the
'current one is being deleted
If Not rsContacts.EOF Then
rsContacts.MoveNext
'save the id of the current (next) record
intCurContact = rsContacts!intContactId
End If
****************************************
'while connected to the database, go ahead and
'repopulate the recordset to make sure it contains
'the most current values from the database.
Set rsContacts.ActiveConnection = cnCh5
rsContacts.Requery
Set rsContacts.ActiveConnection = Nothing
'move back to the contact that was current before the
'requery
rsContacts.Find "[intContactId] = " & intCurContact
'populate the controls on the form
Call PopulateControlsOnForm
End Sub
Thanks
Sandy