473,772 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete a record from a dialog and requery the form

beacon
579 Contributor
Hi everybody,

This is probably going to sound unorthodox, but I have to log records that are deleted...I know, go figure.

Anyway, I have a form with a (continuous) subform, and on the subform I have a command button, called cmdDelete, that launches a dialog form, called frmDeleteCurren tRecord, where the user has to enter their name and a reason.

I have managed, through a ton of trial and tribulation, found a way to delete the current record on the subform from the query, qryEpisodeDetai l, using SQL. I had to pass parameters to the dialog form in order to create the delete query.

The problem I'm having, is that after the delete query runs and I'm returned to the subform, it says "#Deleted" in each of the controls on the subform. I have tried to refresh/requery in what feels like 100 different ways, but I keep getting error messages about how I need to save the current record.

Here's the code for the cmdDelete:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelete_Click()
  2.  
  3.     Dim strConfirmDelete As String, strConfirmDeleteTitle As String
  4.     Dim strCancelDelete As String, strCancelDeleteTitle As String
  5.     Dim strNullDelete As String, strNullDeleteTitle As String
  6.     Dim varParameter
  7.     Dim varWhere As String
  8.  
  9.     'Parameters to be passed 0-8
  10.     Dim strEpisodeDetailID, strPatientDetailIDFK, strFormIDFK, strDeficiencyIDFK
  11.     Dim strProgressNoteIDFK, strDeficiencyDate, strQuantity, strEmployeeIDFK
  12.     Dim strCorrectedDate
  13.  
  14.     strConfirmDelete = "Are you sure you want to delete this record?" + Chr(13) + Chr(13) + "You will be required to log the deletion."
  15.     strConfirmDeleteTitle = "Confirm Delete"
  16.     strCancelDelete = "The delete operation has been cancelled."
  17.     strCancelDeleteTitle = "Delete Cancelled"
  18.     strNullDelete = "The record you are trying to delete does not exist."
  19.     strNullDeleteTitle = "Delete Failed: No Record"
  20.  
  21.     varWhere = "[EpisodeDetailID] = " & Me.EpisodeDetailID
  22.  
  23.     strEpisodeDetailID = Me.EpisodeDetailID
  24.     strPatientDetailIDFK = Me.PatientDetailIDFK
  25.     strFormIDFK = Me.FormIDFK
  26.     strDeficiencyIDFK = Me.DeficiencyIDFK
  27.     strProgressNoteIDFK = Me.ProgressNoteIDFK
  28.     strDeficiencyDate = Me.DeficiencyDate
  29.     strQuantity = Me.Quantity
  30.     strEmployeeIDFK = Me.EmployeeIDFK
  31.     strCorrectedDate = Me.CorrectedDate
  32.  
  33.     varParameter = strEpisodeDetailID & ";" & strPatientDetailIDFK & ";" & strFormIDFK
  34.     varParameter = varParameter & ";" & strDeficiencyIDFK & ";" & strProgressNoteIDFK
  35.     varParameter = varParameter & ";" & strDeficiencyDate & ";" & strQuantity
  36.     varParameter = varParameter & ";" & strEmployeeIDFK & ";" & strCorrectedDate
  37.  
  38.     If vbYes = MsgBox(strConfirmDelete, vbExclamation + vbYesNo, strConfirmDeleteTitle) Then
  39.         Forms!frmDeficiency.Visible = False
  40.         If IsNull(DLookup("EpisodeDetailID", "qryEpisodeDetail", strWhere)) Then
  41.             MsgBox strNullDelete, vbInformation + vbOKOnly, strNullDeleteTitle
  42.             Me.FormIDFK.SetFocus
  43.             Exit Sub
  44.         Else
  45.             DoCmd.OpenForm "frmDeleteCurrentRecord", DataMode:=acFormAdd, _
  46.                 WindowMode:=acDialog, openArgs:=varParameter
  47.         End If
  48.     Else
  49.         MsgBox strCancelDelete, vbInformation + vbOKOnly, strCancelDeleteTitle
  50.         Me.FormIDFK.SetFocus
  51.         Exit Sub
  52.     End If
  53.  
  54. End Sub
  55.  
And here's the code for the cmdSubmit on the frmDeleteCurren tRecord:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSubmit_Click()
  2.  
  3.     Dim varEpisodeDetailID As String, varPatientDetailIDFK As String, _
  4.         varFormIDFK As String, varDeficiencyIDFK As String, _
  5.         varProgressNoteIDFK As String, varDeficiencyDate As String, _
  6.         varQuantity As String, varEmployeeIDFK As String, _
  7.         varCorrectedDate As String, varAuditorFName As String, _
  8.         varAuditorLName As String, varReason As String, varDateDeleted As String
  9.     Dim nullErrorTitle As String, nullErrorMessage As String
  10.     Dim strConfirmDelete As String, strConfirmDeleteTitle As String
  11.     Dim strCancelDelete As String, strCancelDeleteTitle As String
  12.     Dim ctl As Control
  13.     Dim strDeleteRecordSQL As String, strLogDeletionSQL As String
  14.     Dim strDeleteReasonSQL As String
  15.  
  16.     'Title for fields with null values
  17.     '------------------------------------
  18.     nullErrorTitle = ": Null Value"
  19.  
  20.     'String for null values
  21.     '------------------------
  22.     nullErrorMessage = " cannot be blank. Please enter a value in this field."
  23.  
  24.     'Test for null values
  25.     '-----------------------
  26.     For Each ctl In Me.Controls
  27.        If IsNull(ctl) And Not IsNothing(ctl.Tag) Then
  28.             ctlName = ctl.Tag
  29.             MsgBox ctlName + nullErrorMessage, vbExclamation, ctlName + nullErrorTitle
  30.             ctl.SetFocus
  31.             Exit Sub
  32.         End If
  33.     Next ctl    ' End of null value test
  34.  
  35.     varAuditorFName = Me.AuditorFirstName
  36.     varAuditorLName = Me.AuditorLastName
  37.     varReason = Me.ReasonDeleted
  38.     varDateDeleted = Me.DateDeleted
  39.     varEpisodeDetailID = Me.EpisodeDetailIDFK
  40.     varPatientDetailIDFK = Me.PatientDetailIDFK
  41.     varFormIDFK = Me.FormIDFK
  42.     varDeficiencyIDFK = Me.DeficiencyIDFK
  43.     varProgressNoteIDFK = Me.ProgressNoteIDFK
  44.     varDeficiencyDate = Me.DeficiencyDate
  45.     varQuantity = Me.Quantity
  46.     varEmployeeIDFK = Me.EmployeeIDFK
  47.     varCorrectedDate = Me.CorrectedDate
  48.  
  49.     strLogDeletionSQL = "INSERT INTO qryDeletedRecordsLog ( PatientDetailIDFK, EpisodeDetailID, FormIDFK, ProgressNoteIDFK, DeficiencyIDFK, DeficiencyDate, Quantity, EmployeeIDFK, CorrectedDate ) " & _
  50.                         "SELECT qryEpisodeDetail.PatientDetailIDFK, qryEpisodeDetail.EpisodeDetailID, qryEpisodeDetail.FormIDFK, qryEpisodeDetail.ProgressNoteIDFK, qryEpisodeDetail.DeficiencyIDFK, qryEpisodeDetail.DeficiencyDate, qryEpisodeDetail.Quantity, qryEpisodeDetail.EmployeeIDFK, qryEpisodeDetail.CorrectedDate " & _
  51.                         "FROM qryEpisodeDetail " & _
  52.                         "WHERE (((qryEpisodeDetail.EpisodeDetailID)= " & varEpisodeDetailID & "));"
  53.  
  54.     strDeleteReasonSQL = "UPDATE qryDeletedRecordsLog " & _
  55.                          "SET AuditorFirstName ='" & varAuditorFName & "', " & _
  56.                          "AuditorLastName ='" & varAuditorLName & "', " & _
  57.                          "ReasonDeleted = '" & varReason & "', " & _
  58.                          "DateDeleted = #" & varDateDeleted & "# " & _
  59.                          "WHERE (((qryDeletedRecordsLog.EpisodeDetailID)= " & varEpisodeDetailID & "));"
  60.  
  61.     strConfirmDelete = "Are you sure you want to delete this record?" + Chr(13) + Chr(13) + "Warning: The action cannot be undone."
  62.     strConfirmDeleteTitle = "Confirm Delete"
  63.     strCancelDelete = "The delete operation has been cancelled."
  64.     strCancelDeleteTitle = "Delete Cancelled"
  65.  
  66.     strDeleteRecordSQL = "DELETE * FROM tblEpisodeDetail " & _
  67.                          "WHERE tblEpisodeDetail.EpisodeDetailID = " & varEpisodeDetailID & ";"
  68.  
  69.     If vbYes = MsgBox(strConfirmDelete, vbExclamation + vbYesNo, strConfirmDeleteTitle) Then
  70.         DoCmd.RunSQL strLogDeletionSQL
  71.         DoCmd.RunSQL strDeleteReasonSQL
  72.         DoCmd.RunSQL strDeleteRecordSQL
  73.         CloseMe
  74.         'THIS IS WHERE IT WON'T UPDATE - Sorry for the all caps, just an attention-getter.
  75.         Forms![frmDeficiency]![fSubEpisodeDetail].Refresh
  76.         Forms!frmDeficiency.Visible = True
  77.     Else
  78.         Exit Sub
  79.     End If
  80.  
  81. End Sub
  82.  
My intention is to have the delete command function just like the delete command on the toolbar, except for the fact that I need to go to another form real quick, have the user jot down some info, and then return to the updated subform.

It's late, so if this is unclear, please forgive me. I'll be happy to clarify if anything doesn't make sense.
Feb 15 '09 #1
10 7171
RuralGuy
375 Recognized Expert Contributor
You are opening the frmDeleteCurren tRecord form in Dialog mode so the code in the 1st form has stopped. Simply do Me.Requery in the line after the OpenForm. Refresh will not get rid of *deleted* records. It takes a Requery.
Feb 15 '09 #2
beacon
579 Contributor
Hi Rural,

I want to make sure I understand, so forgive me if I'm just re-stating what you said. (I'm one of those weirdos that has to repeat things to make sure I can envision what I'm being told)

So I need to put Me.Requery on the subform, right after I open the frmDeleteCurren tRecord?

And if I do this, when I choose my submit command on the dialog that effectively deletes the record, I'll be returned to the requeried subform?
Feb 15 '09 #3
RuralGuy
375 Recognized Expert Contributor
You are correct. There is no reason to attempt to Requery the 1st form from the other form. The code will return *only* after you close the 2nd form or make it invisible.
Feb 15 '09 #4
beacon
579 Contributor
Thanks Rural.

After I made the main form visible again, I had to set the focus to a different location in order to get the requery to work.

I ended up just setting the focus to the tab control that the subform is on and it accepted the event.

Again, thanks for your help. I worked on this issue for at least 2 hours last night...I didn't realize exactly where control went when I opened the dialog.

Cheers!
Feb 15 '09 #5
RuralGuy
375 Recognized Expert Contributor
Excellent! Glad you got it sorted.
Feb 15 '09 #6
matthewslyman
20 New Member
I have an unusual design that Microsoft Access 2003/2007 won't cooperate with... I have forced cooperation on everything except one issue - record deletion.

My form design involves a recursively nested form. In other words, the form, m_settings_menu editor has a subform, m_settings_menu editor. It contains code invoked on the Form_Open event that modifies the form's recordset (so that the root form only displays root nodes in the tree, and nested forms only display the child nodes of their parent.) The forms are displayed as data-sheets. This implements a tree structure, fully editable as a Microsoft Access datasheet, with open/close tree branch buttons in the form of open/close subdatasheet. In the table, "ui_modes_l kp", the menu elements (and submenu elements, and subsubmenu elements etc.) are specified. Root nodes are indicated with Null entries in the parent_mode

With or without database relationships (enforcing referential integrity), the standard MS Access DELETE events just don't seem to work... When I delete a record from the root form, it works fine... But when I delete records from child subforms, they appear to have been deleted (they disappear from the form) but I see a funny symbol in the record selector of the parent forms, all the way back to the root node... It's like a "no entry" road-sign but in black instead of red. When I close the forms completely and reopen them, I see that the deleted record is back there again. I have tried to trace the problem and it seems like some of my other events may be messing with the normal operation of Access's record deletion routines, AFTER the FORM_DELETE event and BEFORE the BEFORE_DELETE_C ONFIRM event...

So to work around this problem, I thought I'd manually implement the DELETE function when the FORM_DELETE event is invoked, and cancel the event using
DoCmd.CancelEve nt
This leaves a row in the subdatasheet with "Deleted" in place of all the fields. I have attempted to shift the focus away and run DoCmd.Requery, and various VBA based Requery methods (all from within the relevant subform), but it just won't get rid of the "Deleted" record... The VBA-based methods just give error messages saying "The record has been deleted."
This technique currently looks messy but at least it actually deletes the record...

I know my design is a little weird in some respects but I'd be VERY grateful for anyone who can help me with this.
Mar 11 '09 #7
RuralGuy
375 Recognized Expert Contributor
You will get better response if you start a new thread rather than tagging on to an existing one.
Mar 11 '09 #8
matthewslyman
20 New Member
OK - I'm going out now... I'll do that later.
Mar 11 '09 #9
matthewslyman
20 New Member
Here's the address of the new discussion on deleting records within recursively nested subforms with self-join relationships on a single table:

http://bytes.com/topic/access/answer...delete-requery
Mar 13 '09 #10

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

Similar topics

8
4066
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: DoCmd.SetWarnings False DoCmd.RunCommand acCmdDeleteRecord DoCmd.SetWarnings True End If ExitHere: Me!SubName.SetFocus
3
3974
by: Uwe Range | last post by:
Hi to all, I am displaying a list of records in a subform which is embedded in a popup main form (in order to ensure that users close the form when leaving it). It seems to be impossible to delete a record in this subform. When I switched modal off and tried to delete a record from the list, I deleted a record on another form (below the popup form).
3
7825
by: Maria | last post by:
Is there another way to delete the current record in a subform from the main form, another subform or a sub-subform other than setting focus on a field in the subform and using run command acCmdDelete Record? Thanks! Maria
3
5276
by: David Altemir | last post by:
I have a button on "Form A" in the right margin of every record in my recordset. When you click on a button, a dialog box ("Form B") pops up that gives more details about that particular record. The problem is that when I close the dialog, the cursor jumps to the beginning of the recordset on "Form A". I want it to go back to the record where I pressed the button. What's the best way to set the focus to a particular record when...
16
11794
by: MartinR | last post by:
I would like to know the code that i should use to delete a record without the message box saying "You are about to delete a record, are you sure..." poping up. At the moment i am using the code: DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70 DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70 Any ideas to help me
4
4001
by: Steven | last post by:
Hi, Would need some thought about using a button of the existing form to search record before deleting it. Any quick help is very appreciated. Steve
2
1790
by: Phil Stanton | last post by:
Deleting a record on a membership form. Does some VB checks to see if the family are still in the list. Warns me there is no going back if I delete the record, so I say OK and it appears to delete OK. Record count goes down by 1, but if I select "ShowAllRecords", there he is back and the recordCount back to the original value Theories gratefully received Phil
3
1909
by: Constantine AI | last post by:
Hi i am trying to DELETE a Record if the details do not exist within a table. Here is my code below; Dim strSQL As String Dim strSQL2 As String Dim Reply As String Dim db As Database Dim rst As Recordset Dim SeqItemNo As Integer DoCmd.SetWarnings False
31
6681
by: matthewslyman | last post by:
I have an unusual design and some very unusual issues with my code... I have forced Access to cooperate on everything except one issue - record deletion. My form design involves a recursively nested form. In other words, the form, m_settings_menueditor_recursive has a single subform; m_settings_menueditor_recursive (both are viewed as datasheets - so the form is its own subdatasheet.) The Form_Open event modifies the form's recordset so...
0
9620
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.