I created a MS Access database with multiple forms. One of the form is a switchboard that leads to other forms. I wanted to make sure that the switchboard form never closes. So in the switchboard form I did:
Private Sub Form_Unload(Cancel As Integer)
Cancel = True
MsgBox "You cannot close the switchboard"
End Sub
However, I realized that when a user wants to exit the database using the close database at the top it triggers the message box above. I understand why this happens as Access probable tries to close all of the open window before closing the database.
Is there a way to change my vba to understand that the form close is coming from database close event. Or is there any better way to prevent form close?
There are several way and everyone preferes different way of achieving this. As for being user-friendly, if the user wants to close the database they should be able to. So instead of saying you cannot, why don't you just ask Would you like to close? if yes allow them to close.
2> If you really want to prevent them closing the form, why don't you remove all close buttons, borderStyle=none, closebuttons =false maybe poup = true?
Related
I built an access Form frontend with a sharepoint list called "teammates" as the back end.
The form is simple, consists of two listboxes that lists all teammates, one box is for absent teammates, the other is for present teammates. I have some buttons and methods that allow to update a teammates status, thus moving them into either the "absent" list box or the "present" box.
I also have a "Refresh" button on the page.
Private Sub btnRefreshForm_Click()
DoCmd.RunCommand acCmdRefreshSharePointList
CurrentDb.TableDefs("teammates").RefreshLink
Me.lsbAbsentTeammates.Requery
Me.lsbPresentTeammates.Requery
Me.Requery
Me.Refresh
End Sub
My issue is this, when I share the FrontEnd with someone else, that person will send some teammates to the absent box, but I cannot see those changes, the Refresh button does not requery the data.
The only way I can see the updated data is if I close the front end and reopen it.
How can I see the updated data without having to close and reopen the application ?
I want the user to be able to review the word document that the macro generates before stopping the macro. This way, they don't have to enter all the data again, since, when the macro stops, all the data is erased. There are a lot of fields that I don't want the user to have to re-enter if something is wrong.
I've tried activating the macro in a couple different ways, from a button on a word document to starting it from VBA Alt+F11, but I get the same result: The user cannot edit or view the word document while the macro is running, even though it isn't doing anything.
'''
Private Sub CommandButton1_Click()
InputForm.Show
End Sub
'''
The user is not able to view the word document until the macro is stopped and all the data that was input is erased. Alternatively, if it is possible to save the data that has been input so that it is kept for next time the program runs, that would work since I have a "Clear All" button.
Somewhere, you have a subroutine that does something like this:
Sub foo()
UserForm1.Show
End Sub
You will need to change that to display the form vbModeless, in order to allow the user to interact with the Document. Note that this will allow the user to interact with the Document while the form is displayed.
Sub foo()
Dim uf As New UserForm1
uf.Show vbModeless
End Sub
If there is any additional executable code after the uf.Show, it may need to be refactored as well, as it will execute immediately after the form is Shown.
If you don't want the user to be able to interact until after the form does whatever it does, then do not use the vbModeless option, and add a QueryClose event handler to the form. This way we ask the user if they want to review; if they say "Yes", then we hide & re-display the form with the vbModeless option. Now, they'll have the form open and the document will be editable.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If MsgBox("Review document?", vbYesNo) = vbYes Then
Cancel = 1
Me.Hide
Me.Show vbModeless
End If
End Sub
Of course, that may not be quite what you want, either. But those finer points can be worked out separately. There's a lot of things I don't know about your use-case, which can almost certainly be accommodated with proper use of the UserForm. Do note that I can't entertain too many tangentially related follow-up questions, but this should be enough to get you started.
I'm not sure it's possible to persist values in the form beyond it's Terminate event which will happen when the user closes the form or if any code invokes the Unload <userform object>. It would certainly be possible to serialize that data into a text file, or persist it somehow within the Document's custom properties/customer data properties, but that should probably be a separate question.
what is the different in Close() and ActiveForm.Close() in VB?
form2.ActiveForm.Close() [ currently at Form 3 ]
form1.Close()
(different) form1.Close()
question: In Form 1 and Form 2, I can use obj/Form1.Close() and obj2/Form2.Close() without problem, In both form i can do, vise versa.
However, when it comes to Form 3, at this point i only can set Form1.close , But Form2. I had to use form2.ActiveForm.Close().
Why is this happening?
I would like to know what is the real meaning of using
Form.Close() & Form.ActiveForm.Close()
I had searched and failed to understand in every site. would anyone kindly explain?
is it Obj/form.ActiveForm.Close() meant => Close the current Active Obj/Form???
An active form is the form with the input focus. A form that is not visible cannot be the active form. The simplest way to activate a visible form is to click it or use an appropriate keyboard combination.
I found this useful article about active forms Here. Please go through that first.
Form.ActiveForm.Close() is hence used for closing an active form.
Like for example say you have an application with multiple forms and you want to close the active form, then you should be using an activeform.close().
The close() or me.close() would only close the form which contains the statement rather than the active form.
Some more useful information here
I need to close an Excel userform using VBA when a user has clicked a submit button and operations have been carried out.
How can I close a Userform from itself?
I have tried this but it returns a 361 error.
Unload Me
As specified by the top answer, I used the following in the code behind the button control.
Private Sub btnClose_Click()
Unload Me
End Sub
In doing so, it will not attempt to unload a control, but rather will unload the user form where the button control resides. The "Me" keyword refers to the user form object even when called from a control on the user form. If you are getting errors with this technique, there are a couple of possible reasons.
You could be entering the code in the wrong place (such as a
separate module)
You might be using an older version of Office. I'm using Office 2013. I've noticed that VBA changes over time.
From my experience, the use of the the DoCmd.... method is more specific to the macro features in MS Access, but not commonly used in Excel VBA.
Under normal (out of the box) conditions, the code above should work just fine.
Without seeing your full code, this is impossible to answer with any certainty. The error usually occurs when you are trying to unload a control rather than the form.
Make sure that you don't have the "me" in brackets.
Also if you can post the full code for the userform it would help massively.
Unload Me only works when its called from userform self. If you want to close a form from another module code (or userform), you need to use the Unload function + userformtoclose name.
I hope its helps
It should also be noted that if you have buttons grouped together on your user form that it can link it to a different button in the group despite the one you intended being clicked.
When a user start an Access 2007 database that have macros and vba, a security warning is shown. I want the user to deal with this warning, so if the the content is't enabled, the user should not be able to use the database.
Now I use a macro named AutoExec (opens a form that works like a menu), and that macro is run before the user deal with the security warning. But I want to check if the content is enabled and if not I will show a form that inform the user that they should enable the content.
So what I'm actually asking for is how do I do this:
If vba and macros is not enabled -> show form "information"
If vba and macros is enabled -> show form "start menu"
Ok, after a while I have the solution. Thanks for the answers who led me the right way.
This article from Microsoft is very helpful.
In the AutoExec-macro, I have two lines:
Line one: Conditions: [CurrentProject].[IsTrusted]=False and then I choose witch Form I want to open and in this case it is the "info about security warning form"
Line two: Conditions: [CurrentProject].[IsTrusted]=True and now open the "start menu form"
And that's all!
If the content is disabled, then you cannot check, since your code cannot run....
You might like to consider a start-up form ("information"). This will show without macros.
In addition, you can run some start-up code or a macro that closes the information form and opens the main form ("start menu"), if macros are disallowed, this will not run. However, I think you may get an unsightly warning.
EDIT
Set the timer interval to say, 100 and add a little code to Information form:
Private Sub Form_Timer()
DoCmd.Close acForm, "Information"
DoCmd.OpenForm "start menu"
End Sub
Just to add my solution -- I was just dealing with this issue.
By default, in database options have it set to open with form "notEnabled"
On this "not enabled" form, have some text, pictures, or what have you that lets the user know that he/she needs to 'enable content'.
In the on load event for this form, just put some VBA to open the actual form you want the user to be presented and close the "notEnabled" form.
This way, if the user opens the database without making it trusted, enabling content, they are stuck on the form that tells them how to do that. As SOON as it's trusted, the on-load event of the form will fire and redirect the user to whichever form you want, with content enabled.
If the user opens the database and already has trusted the file, they don't see the form telling them to make it trusted.
You can avoid this by setting the IsTrusted flag to TRUE in your AutoExec macro. See Transitioning Your Existing Access Applications to Access 2007 -- search for IsTrusted to get you to the heart of the explanation of how to handle it.
I don't know why people give suggestions that have not been tested yet. My solution is simple:
If: [CurrentProject].[IsTrusted]=False
RunMenuCommand: CloseDatabase
Else
If: [CurrentProject].[IsTrusted]=True
RunCode: (you run the code or macro you wanted to in the first place here)
This basically closes the database if the security warnings are coming on. If they are not, it opens just fine. The user that is the admin will need to decrease the macro security levels on the computer of whoever wants to access the database. This macro unlike others will actually run because it agrees with what Access wants.
You're Welcome!