473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Deleting record from bound form

122 100+
In Access 2007, I am trying to add a button to my sata entry split form that will delete the current record, after prompting, but I am running into a bizarre issue.

Here is the relevant code. Pressing the button triggers the btnDelete_Click() subroutine, which calls DeleteRecord() which in turn can call MoveLine().

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnDelete_Click()
  2.   L = txtLine.Value
  3.   If Form.NewRecord Then
  4.     'The current record has never been saved, so simply clear the form
  5.     Form.Undo
  6.     txtItem.SetFocus
  7.   Else
  8.     'Prompt user with confirmation dialog
  9.     rVal = MsgBox("Are you sure you want to delete this record?", vbYesNo)
  10.     If rVal = vbNo Then Exit Sub
  11.  
  12.     'Delete the current record and update datasheet
  13.     R = Form.CurrentRecord
  14.     DeleteRecord DetailID
  15.     Me.Requery
  16.  
  17.     'Go to the record after the one we just deleted, or go to the end if we deleted the last record
  18.     If R > Form.Recordset.RecordCount Then
  19.       DoCmd.GoToRecord acActiveDataObject, , acLast
  20.     Else
  21.       DoCmd.GoToRecord acActiveDataObject, , acGoTo, R
  22.     End If
  23.   End If
  24. End Sub
  25.  
  26. Private Sub DeleteRecord(ID)
  27.   'First, count the number of records in the order
  28.   Ord = cmbOrder.Value
  29.   Cnt = DCount(1, "OrderDetail", "OrderNumber = " & Ord)
  30.  
  31.   'Delete the specified record
  32.   DoCmd.SetWarnings False
  33.   Line = DLookup("LineID", "OrderDetail", "DetailID = " & ID)
  34.  
  35.   RunCommand acCmdDeleteRecord
  36.   DoCmd.SetWarnings True
  37.  
  38.   'If the record we just deleted was not the last record, shift any records after it down by one
  39.   If Line < Cnt Then MoveLine Line, Cnt, Ord
  40. End Sub
  41.  
  42. Private Sub MoveLine(Start, Finish, Ord)
  43.   'Don't do anything if start and finish are the same, or if there are no records to move
  44.   If Finish < 1 Then Finish = 1
  45.   If Start = Finish Then Exit Sub
  46.   Cnt = DMax("LineID", "OrderDetail", "OrderNumber = " & Ord)
  47.   If Cnt < 1 Then Exit Sub
  48.   If Finish > Cnt Then Finish = Cnt
  49.  
  50.   'If we're moving a record down, adjust the intervening records up and vice versa
  51.   If Start > Finish Then Adj = "+ 1" Else Adj = "- 1"
  52.  
  53.   'Renumber the affected lines
  54.   DoCmd.SetWarnings False
  55.   Query = "UPDATE [OrderDetail] SET [LineID] = [LineID] " & Adj & ", [RenumberLine] = [LineID] " & Adj & " WHERE [LineID] BETWEEN " & _
  56.           Start & " AND " & Finish & " AND [OrderNumber] = " & Ord & ";"
  57.   DoCmd.RunSQL Query, False
  58.   DoCmd.SetWarnings True
  59. End Sub
The issue is this: deleting the item itself works fine, so long as they don't then try to add another item before closing the form. If they delete and item and then add a new item, a really bizarre thing happens. The line appears twice in the split form portion, though the second one is unselectable. Selecting or scrolling the records near the phantom record causes them to sometimes display the incorrect information for that line. There are not two records in the underlying table, and closing the form and reopening it makes everything fine again.

I added the requery line specifically to try to get the form back in sync with the table, but it doesn't seem to help. Any suggestions as to how best to resolve this are greatly appreciated.
Apr 1 '10 #1
0 2208

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William Wisnieski | last post by:
Hello Everyone, Access 2000, I have a main unbound form with a bound datasheet subform . The subform is bound to a query that returns records based on criteria in the main form. The user...
5
by: Xiphias | last post by:
Hi, Can someone help me to create a drop box where I can also add new records? Here is the problem: I m working on a form with a drop box. When I select a '"name" in the drop box the form...
1
by: Mark | last post by:
This question refers to a main form with a continuous form subform. After an error occurs after entering several records in the subform, how can I delete all the data in the main form and all the...
7
by: Mathew Hill | last post by:
I am a beginner to the more technical aspects of Microsoft Access (2000) and was wondering if any one can help. I have 3 buttons on a form which add, delete and search for a record. However, when I...
5
by: Steve Strik | last post by:
My Problem: I have created a database here at work that is exhibiting some very strange behaviour. Essentially the database is structured in a manner where one table is a master record table...
0
by: Lyn | last post by:
This one is driving me crazy. I have a table which stores pictures in an OLE Object column, one per record with a few other text fields (caption, date, etc). This table is maintained with a...
46
by: DP | last post by:
hi, i've got a form, with a subform in it. i've got a delete button in the subform. the code i;ve got is; Private Sub cmdDeleteRecord_Click() msg = "Are you sure you want to delete this...
2
by: clickon | last post by:
I have set up a GridView control bound to an SQLDataSource control. Depending on which option a user selects in a DropDownList Each datasource is a sethe select command retrieves a list of...
2
by: robertns5411 | last post by:
I have two questions: 1) Say I have a form bound to a table. Assume the user is entering data into a new record at the bottom of the form. Now, suppose the user clicks a button on the form...
4
by: sphinney | last post by:
I'm not exactly sure how to start this post. My question is pretty simple, but it will take a little bit of context before I can state it. (And thanks in advance for taking the time to read this!) ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.