MS Access 2010 - Prompting user to save changes - ms-access

I have developed a form and I would like to prompt the user to save changes before they navigate away from the modified record.
If the user attempts to navigate away, I want a prompt to appear asking if they wish to save changes, upon which they may select "Yes" or "No".
I have been informed that the Before Update event is the one I need to focus on, but I keep receiving the "the expression before update you entered as the event property setting produced the following error" message.
These are the steps I take before reaching the error:
Change view to Form View
Make a change anywhere in the record via the form
Attempt to navigate away from form via navi-buttons that I put into my form (which work just fine if no changes are made but which fail to do anything if a single change has been made)
Nothing happens, so I revert to Design View, to receive the following error notification
I press OK and then receive this message:
And then I go back to Square One.
Furthermore, any Conditional Formatting has stopped working altogether since this problem has arisen; I do not know for certain if the two are linked, but thought it worth mentioning.
Any ideas how this can be achieved (ideally error-free)? Unfortunately, I cannot post my system up as it deals with highly confidential data.
UPDATE:
I have tried a variety of codes which I have modified, all to no avail. At present, I have removed any such code altogether, but the code I have tried in the past is something to the effect of:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim ctl As Control
On Error GoTo Err_BeforeUpdate
If Me.Dirty Then
If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
"Save Record") = vbNo Then
Me.undo
End If
End If
Exit_BeforeUpdate:
Exit Sub
Err_BeforeUpdate:
MsgBox Err.Number & " " & Err.Description
Resume Exit_BeforeUpdate
End Sub

The default behaviour for a Microsoft Access bound form is to automatically save the record whenever the user does the following:
Moves to another record, Closes The form or explicitly clicks the save button on the ribbon
Therefore I thinks it may be redundant asking the user to confirm if they wish to save.
The forms before update event is generally used for validation so you can check the data that has been entered into the controls and decide in code whether to allow the data to be saved.
To prevent the data from being saved you would change the Cancel variable for example:
Cancel=True
MsgBox "There is a problem with the data entry", vbExclamation, "Please Check Your Data"
The user would then have to press escape or click Undo on the ribbon to escape out of edit mode
Regarding the error you are getting please look into and try the decompile switch documented at the following web page:
Decompile Switch
I occasionally get errors when working with and saving VBA code over and over. Doing the decompile can usually fix this problem. However make sure you do a backup of the database before you start.
Also you could try exporting out your form as a text file and then importing it back in again using the following code which will help if the form has become corrupted:
Application.SaveAsText acForm, stringFormName, stringFolderName & "\" & stringFormName & ".txt"
Application.LoadFromText acForm, stringFormName, stringFileName

Open your form in Design View
Right-click on the form and click on "Properties" from the pop-up menu
Scroll down to the "Before Update" event
All the way on the right you should see a box with an ellipse ("...") in it. Click that box to open the code associated with the Before Update event.
If it opens to a blank sub, you haven't assigned the event properly. Make sure the event is called "BeforeUpdate" (one word) and not "Before Update" (two words).
Your error indicates that this is likely the cause of the issue.

Related

MS Access - Command button wont function at all

I have tried to Google and review previous Stackoverflow posts but I cannot seem to find a solution and this seems like such a simple fix.
I put a Command button in my Form because i wanted to test exporting information within the form to an Excel spreadsheet.
Private Sub cmdExportFilename_Click()
Dim sFilename As String
sFilename = "C:\Users\Redacted\Desktop\exportResults.xlsx"
DoCmd.OutputTo acOutputQuery, "OpenComplaintsQuery", acFormatXLSX, sFilename, AutoStart:=True
End Sub
I made sure the OnClick event was set to [Event Procedure] but when i go into form view and click nothing happens not even an error so i feel like i am still missing something that is telling that button to run that code. i checked my Trust Center settings and changed it to enable code but still nothing.
Any advice is helpful!

Access 2016 set form field based on previous form value

I am creating an Access Database formed out of two things - Software and Licences. Licenses are attached to Software via their ID.
I have a form created for Software, and would like to create a button that, when clicked, opens a fresh "Add License" form which pre-populates the Software ID and that people can fill in the rest of the information on.
I've been using Macros from a similar Office 2016 Template and it continually falls over when trying to put this information into the new License form.
I've attached a screenshot of my macro below - I've gone through many iterations of this now and the error I get is 30024, which appears to indicate that it cannot find the field to put the SoftwareID into in the newly opened form.
I've also set the "Control Name" to just "SoftwareID" as this was also suggested elsewhere but this also does not work.
Any suggestions?
Screenshot of Macro in question
I couldn't get it to work using the embedded macro so I used a VBA macro instead which worked :). To do this (assuming you haven't done it before):
Open your "Software" form in design view
Show the property sheet (Ribbon -> Design Tab)
Click on your button
Click the "Event" tab on the Property sheet
Delete the "On Click" event
Right click your button and click "Build Event"
From the "Choose Builder" popup box, click "Code Builder"
Assuming you don't have any other macros, your code should look just like mine with the only differences being the name of your button (mine is Command163)
p.s. I Couldnt get the code tags to display properly so i just added some line breaks. Apologies for the improper indenting.
Option Compare Database
Private Sub Command163_Click()
DoCmd.RunCommand acCmdSaveRecord
openFreshAddLicenseForm (Me.ID)
End Sub
Public Function openFreshAddLicenseForm(ID As Integer)
On Error GoTo Macro1_Err
DoCmd.OpenForm "Add License"
DoCmd.GoToRecord , "", acNewRec
Forms![Add License]!SoftwareID = ID
Macro1_Exit:
Exit Function
Macro1_Err:
MsgBox Error$
Resume Macro1_Exit
End Function

MS-Access 2007 on WinXP, can't break out of requery looping in Form_Current sub

I set a form "frmMain" to automatically display when the database is opened by naming it in: Access Options, Current Database, Display Form: frmMain.
In a lapse of judgement of enormous proportion, I included the statement me.requery at the beginning of the class object module Form_frmMain's Private Sub Form_Current() routine.
Now, whenever the database opens, it starts requerying over and over until after a second or so it displays a message: "Run-time error '3420': Object invalid or no longer set." Selecting End or Debug both have the same effect: me.requery is highlighted in yellow and a new "Object invalid or no longer set." message is displayed.
I've tried multiple Ctrl-Breaks, and Escapes, and can't get the console to return any control to me. I can kill the process with the Task Manager, but that of course doesn't let me get into the VBA code to remove my ridiculous me.requery.
Can someone help me out here? Thank you! Dave
After you kill your program from task manager, open your db file(I assume it is .accdb) by pressing down the shift+enter keys.
After you open your file you`ll see your database screen in front of you. Just double click a module to open the VBA editor or simply press ALT+F11. Then, youll be able to find your function.

VBA is not saved when MS Access is closed

I have a database set up on MS Access 2007.
On the BeforeUpdate selection on the Form properties, I have placed the below macro:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Changes have been made to this record." & vbCrLf & vbCrLf & "Do you want to save these changes?", vbYesNo, "Save?") = vbYes Then
DoCmd.Save
Else
DoCmd.RunCommand acCmdUndo
End If
End Sub
When the VBA is saved, Forms run as expected.
The issue is: When Access is closed and reopened the form does not ask whether it needs to be saved (ie the VBA code is not executed). Please assist
How can I make the VBA code constant?
Form BeforeUpdate event is not triggered unless the record or any control's data is changed. In other words, if you open and close the form without making any changes (either manually or programmatically) the event will never be triggered. This might explain why this is not asking you the option.
If you are using the Form's Record Selector, then you can easily identify if the event would be triggered or not. IF the record selector goes form being a Triangle to a Pencil, the even will be triggered, if not it will not be because no changes are made.
Hope that helps.

How to present a MS Access Form to the end user?

I created a form in MS Access. Unfortunately I cannot publish to access services or make a package solution
I am looking for a user friendly way to present this form to the user. So far the user will open the ms access file , click on the form and fill it out.
I would like to have a way to provide the form ONLY. I do not want the user to see all the tables and the structure . . Is there any way i can separate the form from the tables, queries etc. list ?
I split the database, and gave a fe copy to the user, but it still sucks! All those panels and stuff. Does MS Access has anything to address this issue ?
The issue(s) of splitting the database, and the issues of creating a compiled accDE are NOT ONE BIT RELATED to hiding the Access interface.
100% separate question and issue.
Now, without question the above should be done for any access application, but THE ABOVE HAS ZERO TO DO WITH hiding the access interface.
Once you get the access UI hidden, then you can consider the idea of compile to accDE a good idea to PREVENT users from getting into the access UI parts.
Same goes for splitting. You really need to split. However, AGAIN the splitting has NOTHING to do with hiding the accsss interface.
So now, lets get on to hiding the access interface.
To hide all of the access interface and ONLY show the form, you need to add ONE LINE to your start up code (the forms on-load event is fine).
So, specify the form you want to display in the options.
Add this ONE LINE of VBA code to the forms on-load event.
DoCmd.ShowToolbar "Ribbon", acToolbarNo
The additional settings you require are:
[x] Display Navigation Pane <-- uncheck this box.
[x] Use Access Special Keys <-- uncheck this box
Set access to use tabbed interface, and un-check the box to display tabs.
The form MUST NOT be popup. It can be model, but NOT popup.
The result is you will ONLY see the form. This shows the result:
Now keep in mind to get back to "developer" mode, you have to exit, and then hold down the shift key during startup. When you get all of this working, then you want to compile to an accDE, and search out some answers on how to disable the shift key during startup to prevent your users from seeing the access UI.
So you ONLY need one line of code to achieve this goal. The rest is just choosing the correct settings in your application, and that one line of code on startup.
Steps for publishing the compiled accde file and making it ready for end user:
BackUp: Take backup of your accdb/mdb file
Uncheck following elements from Database Options
(Office Button=>Access Options=>Current Database) :
Display Navigation Pane
Allow Full Menus
Allow Default Shortcut Menus
Security: To prevent anyone bypassing the start-up by using the Shift key and access the table use below code :
Public Function DisableByPass()
On Error GoTo err_proc
'Disable By Pass Key in mde/accde db
Dim dbs As DAO.Database
Dim prp As Property
Dim strMDE As String
Set dbs = CurrentDb
With dbs
On Error Resume Next
strMDE = .Properties("MDE") 'If this is an MDE or ACCDE database, disable bypass key
If Err = 0 And strMDE = "T" Then
.Properties("AllowByPassKey") = 0
If Err.Number = 3270 Then
On Error GoTo err_proc
Set prp = .CreateProperty("AllowBypassKey", dbBoolean, False)
.Properties.Append prp
.Properties.Refresh
End If
End If
End With
exit_proc:
On Error Resume Next
dbs.Close
Set dbs = Nothing
Exit Function
err_proc:
MsgBox Err.Description
Resume exit_proc
End Function