Printable Version of Topic

Click here to view this topic in its original format

UtterAccess Forums _ Access Records _ How To Return The User To The Record They Were Viewing Prior To Canceling New Record Edits.

Posted by: NLarsen May 11 2019, 07:40 PM

I have a form for browsing records, editing existing records, and adding new records. The data are a record of maps in my rare map collection. I have 3 command buttons; “Edit Existing Map”, “Add a New Map”, and “Cancel Entry”

I need help with the “Cancel Entry” button mainly (button #3 below).

Firstly, I am not sure how best to use the Form.CurrentRecord property and where best to declare and set the variable I call crID (currently viewed record’s ID). I set it in the Form’s OnCurrent Event:

' Declares a variable for storing the current record in other Private Subs
Dim crId As Integer
crId = 1

------------------------------

Command Buttons:

1) The “Edit Existing Map” command button enables editing the current record and is called cmdEditMap with the following OnClick Event code and it works well:

Private Sub cmdEditMap_Click()

Me.AllowEdits = True

End Sub


2) The “Add a New Map” command button opens a new blank record ready for data input. This was created with the button wizard for adding a new record and is in Macro form. I would like to have this in VBA code but haven’t come up with the code for this yet. It works well as is for now.

3) The “Cancel Entry” button is the primary problem for me and where I need the most help. I want it to:
A) Cancel editing an existing record and stay on that record. This seem to work OK.
B) Cancel a new record that has not been edited (ie is Not Dirty), and take the user back to the record they were viewing when they clicked the “Add a New Map” button.
C) Cancel a new record the HAS been edited but not saved (ie. Is Dirty), and take the user back to the record they were viewing when they clicked “Add a New Map” button.

The code I have for this is:

Private Sub cmdCancelEntry_Click()

On Error Resume Next
DoCmd.GoToControl Screen.PreviousControl.Name
Err.Clear

' If the user clicked to add a new record but decides to cancel before making any inputs
' then I want this part of the code to cancel/close the new record and return the user to
' whatever record they were viewing prior and disables editing.

If ([Form].[NewRecord] And Not [Form].[Dirty]) Then
DoCmd.GoToRecord , , acGoTo, crId
Me.AllowEdits = False
End If

If (Not [Form].[NewRecord]) Then ' This portion will cancel editing and existing record and stay on that record.
DoCmd.RunCommand acCmdUndo
End If

If ([Form].[NewRecord] And [Form].[Dirty]) Then 'This portion cancels a new record that had some input, but user wants to cancel.
DoCmd.RunCommand acCmdUndo
DoCmd.GoToRecord , , acGoTo, crId 'Returns user to record they were viewing prior.
Me.AllowEdits = False
End If
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If

DoCmd.GoToRecord , , acGoTo, crId

Me.AllowEdits = False 'Puts the form back into no data entry allowed mode.
End Sub


Please help me understand how to get the currently viewed record’s ID into a variable and then use that to return the user to that record when they cancel in either of the two conditions described above.

Thanks!

Nils



Posted by: theDBguy May 11 2019, 07:49 PM

Hi Nils,

Welcome to UtterAccess!
welcome2UA.gif

I could be wrong but I don’t think a new record and dirty could not be anything but True together when you create a new record.

Posted by: gemmathehusky May 12 2019, 04:30 AM

if you are on a record, and them goto the new record - if you cancel the new record entry, the cursor will be at the last record.

you could try Runcommand acCmdRecordsGoToPrevious


Posted by: NLarsen May 12 2019, 03:18 PM

Hi Dave,

Thanks for the tip but this is not quite what I am after. If I am on record 20 of 477 and I then go to new record using my "Add a new Map" button, and then use your code to effectively cancel out of the new record, it takes me to back record 477 and not record 20. Since record 477 is the last record, it is in fact "previous" to a new record, which I get. Anyway, thanks!

Nils

Posted by: gemmathehusky May 12 2019, 04:07 PM

Well you probably need to research bookmarks.

Store a bookmark, then return to the stored bookmark. I don't particularly see why you would want to return to a previous record after adding a new one, but if gotoprevious doesn't do it, then you will have to do it manually.

Posted by: NLarsen May 12 2019, 06:48 PM

Hi Dave,

As I mentioned, I only want to return after CANCELING a new record addition. I did research Bookmark and it was not the way to go. I figured it all out. Here is what I ended up with:

At top of module declare crID variable (crID is for storing the primary key ID as string. My primary ID is called MapID in the table.)

Dim crID As String

-----------------------

Then my "Add a Map" button OnClick:

Private Sub cmdAddMap_Click()
Me.AllowEdits = True
crID = MapID 'sets the value of variable crID to the current map's Primary Key ID (called MapID).

' MsgBox "You will be returned to this map if edits canceled: MapID " + crID

DoCmd.GoToRecord , , acNewRec 'Go to new blank record.

End Sub


------------------------------

Then the "Cancel Entry" button OnClick cancels for all conditions and returns user to the Map they were viewing:


Private Sub cmdCancelEntry_Click()

On Error Resume Next
DoCmd.GoToControl Screen.PreviousControl.Name
Err.Clear

' If the user clicked to add a new record but decides to cancel before making any inputs


If ([Form].[NewRecord] And Not [Form].[Dirty]) Then

With Me.Recordset
.FindFirst "MapID=" & crID 'Returns user to record they were viewing prior.

If .NoMatch Then
MsgBox "Map not found"
End If
End With

Me.AllowEdits = False
End If

If (Not [Form].[NewRecord]) Then ' This portion will cancel editing and existing record and stay on that record.
DoCmd.RunCommand acCmdUndo
Me.AllowEdits = False 'Puts the form back into no data entry allowed mode.
Exit Sub
End If

If ([Form].[NewRecord] And [Form].[Dirty]) Then 'This portion cancels a new record that had some input, but user wants to cancel.
DoCmd.RunCommand acCmdUndo

With Me.Recordset
.FindFirst "MapID=" & crID 'Returns user to record they were viewing prior.

If .NoMatch Then
MsgBox "Map not found"
End If
End With

Me.AllowEdits = False
Exit Sub
End If
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If

End Sub


So, it was the FindFirst method that worked for me.

Thanks all!

Nils

Posted by: theDBguy May 13 2019, 09:41 AM

Hi Nils. Glad to hear you got it sorted out. Good luck with your project.