UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> How To Return The User To The Record They Were Viewing Prior To Canceling New Record Edits., Access 2016    
 
   
NLarsen
post May 11 2019, 07:40 PM
Post#1



Posts: 3
Joined: 11-May 19



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


Go to the top of the page
 
theDBguy
post May 11 2019, 07:49 PM
Post#2


Access Wiki and Forums Moderator
Posts: 75,284
Joined: 19-June 07
From: SunnySandyEggo


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.

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
gemmathehusky
post May 12 2019, 04:30 AM
Post#3


UtterAccess VIP
Posts: 4,541
Joined: 5-June 07
From: UK


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


--------------------
Dave (Male)

(Gemma was my dog)
Go to the top of the page
 
NLarsen
post May 12 2019, 03:18 PM
Post#4



Posts: 3
Joined: 11-May 19



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
Go to the top of the page
 
gemmathehusky
post May 12 2019, 04:07 PM
Post#5


UtterAccess VIP
Posts: 4,541
Joined: 5-June 07
From: UK


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.

--------------------
Dave (Male)

(Gemma was my dog)
Go to the top of the page
 
NLarsen
post May 12 2019, 06:48 PM
Post#6



Posts: 3
Joined: 11-May 19



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
This post has been edited by NLarsen: May 12 2019, 06:59 PM
Go to the top of the page
 
theDBguy
post May 13 2019, 09:41 AM
Post#7


Access Wiki and Forums Moderator
Posts: 75,284
Joined: 19-June 07
From: SunnySandyEggo


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

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    21st May 2019 - 03:40 AM