Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Access 2003 VBA
This is the forum to discuss the Wrox book Beginning Access 2003 VBA by Denise M. Gosnell; ISBN: 9780764556593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Access 2003 VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 12th, 2007, 10:16 AM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 5 again

In deleterecord() we have the following code

If Not rsContacts.BOF Then
    rsContacts.MovePrevious
    'save the id of the current (previous) record
    intCurContact = rsContacts!intPartID
End If

However, if we delete the first record in the recordset then rsContacts.MovePrevious results in us being at BOF and the program bombs.

I added

If rsContacts.BOF Then rsContacts.MoveFirst

on the line after the rsContacts.MovePrevious. This solves the problem, but it's hardly elegant.

Comments?

Reply With Quote
  #2 (permalink)  
Old February 12th, 2007, 11:09 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perhaps a related issue would be what if there was only 1 record in the database and you deleted it - you'd have to consider that scenario also, yes?

Reply With Quote
  #3 (permalink)  
Old February 13th, 2007, 05:14 PM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Good point!

Reply With Quote
  #4 (permalink)  
Old February 20th, 2007, 03:02 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you're using rsContacts.MoveFirst - wouldn't that move you to the record (in the recordset, not the database) that you had wanted to delete? The ID you'd be saving would have been deleted from the database by the SQL statement, wouldn't it?

Reply With Quote
  #5 (permalink)  
Old February 20th, 2007, 06:02 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's what I came up with - not sure that it's fully tested, but it hasn't bombed when I've deleted any/all of the records in the table.

Code:
Sub DeleteRecord()

'don't let the user issue a delete command if in add mode
If blnAddMode = True Or rsContacts.RecordCount = 0 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 previous record in the local recordset since the
'current one is being deleted

If rsContacts.RecordCount > 1 Then
    If Not rsContacts.BOF Then
        rsContacts.MovePrevious
        If rsContacts.BOF Then
            rsContacts.MoveFirst
            rsContacts.MoveNext
        End If
    'save the id of the current (previous) record
    intCurContact = rsContacts!intContactId
    End If
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

If intCurContact <> 0 Then
'move back to the contact that was current before the
'requery
rsContacts.Find "[intContactId] = " & intCurContact


'populate the controls on the form
Call PopulateControlsOnForm

Else

Call ClearControlsOnForm

End If


End Sub
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 - End of chapter exercises whizzkid1892 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 30th, 2008 01:02 PM
chapter 7 - chapter 11 pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 3 January 6th, 2008 11:40 AM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 10:40 PM



All times are GMT -4. The time now is 02:49 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.