VBA is not saved when MS Access is closed - ms-access

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.

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 docmd.outputto of a report doesn't cause report_load or other events to fire

Please do not close my question again - I've included specific behavior needed and code example of what causes the problem
Option Compare Database
Option Explicit
Sub testreportoutputto()
'DoCmd.OpenReport "report1", acViewPreview
'Stop
' this technique does not fire the report_load event
DoCmd.OutputTo acReport, "report1", acFormatPDF, "c:\temp\report.pdf", True
End Sub
Access 2016 on Premises I've looked at the other similar postings but none quite fit my circumstances.
I issue a docmd.openreport ... and it works fine. This report has a few on events, such as Report_load, Header_format, etc to dynamcially change the header page, etc.
However, when I do a docmd.outputto acreport, reportname, ... to send it directly to a PDF, the on events do not fire. How do I get VBA control during the formatting process when it writes it directly to disk. I guess I could bring up the report in print preview and do some kind of sendkeys to save the report but seems a bit awkward to me.
The desired behavior is for the Report_load procedure to fire when the docmd.output command is issued
example code docmd.output to acreport, "report name", afformatpdf, "c:\filelocation"
It appears that only report load doesn't fire on outputto -
So I'm able to work around that -
thanks

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

Cannot close and reopen Form in Access (OLE Connetion)

I'm a VBA noob but maybe you can help:
I want to refresh all Forms (subforms) and queries on my Main Form "FinalForm". However I use Access as a frontend to SQL server. So apparently the usual buttons (created with the wizard, like refreh, new record etc.) won't work.
I created a (stupid) workaround by closing and reopening the form:
Private Sub Befehl71_Click()
DoCmd.Close acForm, "FinalForm"
DoCmd.OpenForm FormName:="FinalForm"
End Sub
This works fine within the vba editor but fails if triggered by button (something about an ole communication error).
How can I fix this ?
The standard VBA procedure is
Me.Requery
For a subform only
Me.PUT_SUBFORM_NAME_HERE.Form.Requery

MS Access 2010 - Prompting user to save changes

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.