Answer 'No' to Save produces error (1 Viewer)

foshizzle

Registered User.
Local time
Today, 15:36
Joined
Nov 27, 2013
Messages
277
The code from the link below prompts a user to save the current record before changing records if the form is dirty. I've put this code in my form, but it produces an error anytime I select No to not save the record.


For instance, in a new record on the form, type something in any field and hit the new button. Answering no to the save prompt produces run-time error 2105 "You can't go to the specified record," which then highlights the DoCmd.GoToRecord , , acNewRec from the new button.



http://www.databasedev.co.uk/prompt_to_save.html


It's a simple form and I've uploaded it to this post. Can someone please assist?
 

Attachments

  • FleetRequisitions.zip
    206.6 KB · Views: 78

theDBguy

I’m here to help
Staff member
Local time
Today, 12:36
Joined
Oct 29, 2018
Messages
21,358
Hi. I'd say just add an error handler to your code and move on.
 

foshizzle

Registered User.
Local time
Today, 15:36
Joined
Nov 27, 2013
Messages
277
Hanks doe your response. Does that mean it would just bypass that portion of the code if the user answers no? Everything appears to work ok otherwise.

I’ve never just wrote error handling. Could you provide some assistance?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:36
Joined
Oct 29, 2018
Messages
21,358
In the click event of your "New" button, try something like:
Code:
Private Sub cmdNew_Click()
On Error GoTo errHandler

    ' Clear combobox
    Me.cboBrowse = ""

    'New Record
    DoCmd.GoToRecord , , acNewRec

errExit:
    Exit Sub
    
errHandler:
    If Err.Number <> 2105 Then
        MsgBox Err.Number & ". " & Err.Description
    End If
    Resume errExit
    
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:36
Joined
Oct 29, 2018
Messages
21,358
You’re welcome. Good luck with your project.
 

June7

AWF VIP
Local time
Today, 11:36
Joined
Mar 9, 2014
Messages
5,423
Following code works:
Code:
    'New Record
    If Me.NewRecord Then
        DoCmd.RunCommand acCmdSaveRecord
    Else
        DoCmd.GoToRecord , , acNewRec
    End If
Looking at the BeforeUpdate event. I think testing for Dirty is unnecessary. Certainly the record must be dirty. Why else would BeforeUpdate fire? But suppose it doesn't hurt.

Multiple similar name fields indicates a non-normalized data structure. It will be more difficult to add up total cost for each requisition. There will be lots of empty cells when less than 15 parts and if you need more than 15 parts the structure does not readily accommodate.
 
Last edited:

foshizzle

Registered User.
Local time
Today, 15:36
Joined
Nov 27, 2013
Messages
277
This particular form will rarely be used; it was a replacement for one built with MS works. I'm not sure how to insert rows dynamically so I kept it as is. However if you can be so kind as to point me in the right direction I'd like to check into it. Thanks
 

June7

AWF VIP
Local time
Today, 11:36
Joined
Mar 9, 2014
Messages
5,423
Data appears to be many-to-many relationship - each requisition can have many parts and each part can associate with many requestions. So instead of the 15 sets of Part Qty/Desc/Cost fields, there would be tables:

tblParts
PartID
PartDesc
PartCost

tblRequisionParts
ReqID_FK
PartID_FK
Quantity

Should probably be something similar for the 11 Cost___ fields but not clear what that data is.
 

Users who are viewing this thread

Top Bottom