MS Access - Command button wont function at all - ms-access

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!

Related

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.

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.

Hyperlink "deactivated" after VBA on textbox

I've been using VBA and available modules to integrate the browse for directory and browse for file functionalities in two different MS Access projects. Here are the example files where I took the functionalities from:
Browse to folder: http://www.lebans.com/callbackbrowser.htm
Browse to file: http://www.access-programmers.co.uk/forums/showthread.php?t=97787
In both my .mdb projects, I have tried to browse for a file/folder, then output the address to a hyperlink textbox, linked to a hyperlink field in a table.
It has mostly worked, but unfortunately, with both these functionalities, the hyperlink stops working... The output to the textbox and to the table seems perfect, but when I click on the hyperlink, it does not take me to Windows Explorer.
To make the hyperlink work, I have to double click on the textbox, (which then lets me edit the field) remove the last letter, then type it back again, and tab out. Then, when I click, Windows Explorer opens normally, as a hyperlink should.
What is the solution? I've already tried:
DoCmd.RunCommand acCmdSaveRecord
Without success...
Thanks for your help
If I understand you correctly, you want to open an explorer window for a specified directory set in the textbox?
If so, you'll need to use the textbox on-click event, something like this:
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Shell "C:\windows\explorer.exe """ & TextBox1.Value & "", vbNormalFocus
End Sub
Or, you can use the double-click event:
Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Shell "C:\windows\explorer.exe """ & TextBox1.Value & "", vbNormalFocus
End Sub
By the way, I could only test this in Excel - I don't have Access installed, but it should be the same.
I found the answer to my question on stackoverflow:
Access - Hyperlinks Aren't Linking
Turns out you need to concatenate #'s before and after the hyperlink address for it to work properly as a hyperlink.