Access 2007 - LostFocus Event - ms-access

On a form I have a Command button. After the user clicks the button I want to disable the button so it can't be clicked again. I know that you can't disable a control while it still has the focus. So I created this event procedure:
Sub Command1_LostFocus()
Me.Command1.Enabled = False
End Sub
After I clicked the command button, and then tabbed to another control, I expected the above Sub to run. But I get the error message "You can't disable a control while it has the focus". I'm surprised because I thought that when the event procedure was executed Command1 had lost the focus.
Any suggestions on how I can disable a Command button after clicking it?

Move the focus to another control:
Me!SomeControl.SetFocus
Me!Command1.Enabled = False
And do name your controls something meaningful.

Related

Hiding the Navigation Pane cause a problem when exiting the database

I just found something strange with MS-Access 2016. I have a database with 2 forms: one hidden, one for a menu. The autoexec will open the hidden form and that one will open the menu form.
I unchecked some options, under Current Database e.g. [Display Navigation Pane] and [Allow Full Menus].
I have an exit button/command on the menu form. On Click, it calls DoCmd.Quit. But before exiting, I am getting an MS-Access window asking for the parameter that is normally when the form open, not when it is closing.
After doing a lot of testing and I mean a lot, I realised that the problem is related to the fact that I am disabling the [Display Navigation Pane]. If I keep the navigation pane, but instead I unchecked all Object Type, then everything works correctly.
So, my question is why disabling the navigation pane can create something like that and is there a way to fix it?
Yes, my solution works, but I don’t like it since users can open and close the navigation pane using the shutter bar; nothing shows but it is still distracting and users can call for support.
The problem is not related to the fact that the first form is hidden, but because it is open. If I add a DoCmd.Close acForm, “F_Parameter” before my DoCmd.Quit then it works.
It is easy to reproduce the problem:
Create a new database
Create two forms:
F_Parameter
F_Menu
Create a Macro named Autoexec:
OpenForm
Form Name: F_Parameter
Window Mode: Hidden
In form F_Parameter create an event On Load
Private Sub Form_Load()
DoCmd.OpenForm “F_Menu”
End Sub
In form F_Menu create a Button named Btn_Exit with an event On Click
Private Sub Btn_Exit_Click()
DoCmd.Quit
End Sub
In form F_Menu create an event On Load
Private Sub Form_Load()
MsgBox “Hello”
End Sub
go File, Options, Current Database and
uncheck Display Navigation Pane and
uncheck Allow Full Menus
Save, exit and open the database:
You will get a “Hello” because the F_Menu form open, now click on the exit and you will get another “Hello”. This second one should not be there because the form should be closing not opening.
I was able to reproduce the issue described. I don't think it has anything to do with the Navigation Pane.
This is happening because while the DoCmd.Quit event is occurring, the hidden F_Parameter form is becoming "unhidden" in order for Access to close it. This causes the F_Parameter Form.Load event to run, which is essentially opening F_Menu, thus causing the Form.Load event on F_Menu to run.
I was able to solve this by first explicitly closing F_Parameter, then quitting:
DoCmd.Close ObjectType:=acForm, ObjectName:="F_Parameter", Save:=acSaveNo
DoCmd.Quit

Cancelling the MS Access SpellCheck dialog with {ESC} key causes subform updating error #NAME

In MS Access 2013, I have a form with several subforms. On one of the subforms is a RichText multiline textbox. The user can click a button to spellcheck the textbox, and, the textbox is also automatically spellchecked when a [Save] button on the subform is clicked.
The spellcheck works fine. But, if the user presses {ESCAPE} when the spellcheck dialog appears in order to dismiss the dialog, the data in a different subform does not get updated properly and shows #NAME for all fields.
But, if the user presses the [Cancel] button or clicks the [X] at the top right of the dialog to close the spellcheck dialog, the subform gets updated just fine.
Furthermore, if I put in a breakpoint right after the command that calls the spellcheck dialog and then step through the code line by line, the subform gets updated just fine even when the {ESC} key is pressed.
I tried moving the spellcheck to the form's AfterUpdate event instead of the [Save] button's OnClick event but that didn't change the problem.
Does anyone know a graceful way of preventing the use of {ESC} to dismiss the spellcheck dialog from preventing other subforms from updating correctly?
I've posted an answer in case it helps someone out, but, I'd love it someone posted/suggested a more elegant solution because I don't particularly like my workaround. I'll wait awhile before marking my answer as accepted so others can post a better one!
So, the sloppy workaround that worked for me was to add a SendKeys "{RIGHT}", True after the Spellcheck command as follows:
'spell check the progress note
If Me.[Note].Tag = "Locked" Then
MsgBox "Cannot spell check a locked note.", vbOKOnly + vbInformation, "Locked"
Else
'If the Note contains data run the Spell Checker after data is entered.
If Len(Me.[Note] & "") > 0 Then
DoCmd.SetWarnings False
Me.[Note].SetFocus 'set focus to the control to spell check
With Me.[Note]
.SelStart = 0
.SelLength = Len(Me.[Note])
DoCmd.RunCommand acCmdSpelling
SendKeys "{RIGHT}", True
.SelLength = 0
End With
DoCmd.SetWarnings True
End If
End If
I think this is sloppy and should not be necessary and I have no idea why it solves the problem. So, I'd love it if someone else provides a more elegant solution!

Userfriendly forms for links in MS Access

I'm trying to create a form with the following behavior:
Clicking it when it's empty opens the dialogue window of inserting a hyperlink
Clicking it when there's a hyperlink opens the hyperlink
Clicking a nearby "Clear" button clears the form deleting the hyperlink (if present)
I'm currently stuck with step 2. The code for hyperlink insertion window is this one:
Private Sub PSIC_Click()
Me.[PSIC].SetFocus
On Error GoTo browse_stop
RunCommand acCmdInsertHyperlink
browse_stop:
End Sub
When I try to apply different if then variations it doesn't work as expected. Either I fail in properly applying if then or in determining that the form is empty.
The OnClick() event of your hyperlink textbox would be:
Private Sub PSIC_Click()
On Error GoTo browse_stop
If ISNULL(Me!PSIC) Then
RunCommand acCmdInsertHyperlink
End If
browse_stop:
End Sub
I removed SetFocus because clicking on the textbox should automatically set the focus, but if you need it there for some other reason it wasn't really hurting anything.
The "Clear form" command button's OnClick() event would be:
Private Sub cmdClearForm_Click()
Me!PSIC = NULL
End Sub
To open a hyperlink from a form in MS Access:
In Design View make sure that the hyperlink is in its own text box and then go to the 'property sheet' then half way down you should see the option for Hyperlink Address. In here add the address of the hyperlink.
If you want to click on an image and have it open a hyperlink then right-click on the image go to hyperlink.

Removing focus from subForm on a mainForm in Access

I have a really frustrating problem. Basically I have a mainForm with 3-4 subForms all in datasheet view. What is a generic way to REMOVE focus from subForm after a record is selected and set it to a control on mainForm?
I tried:
OnCurrent Event:
Me.Parent.Form.SetFocus
Me.Parent.[Control].SetFocus
and it works half the time. The problem is, however, when user selects the record by clicking into some field in the subForm. Then the highlightedfields are either A) the field in the subForm, B) the field in the mainForm. Decided seemingly at random.
How do I CANCEL/REMOVE the focus from the subForm completely before setting the new focus elsewhere?
That would be to move focus to a control on the parent form:
Me.Parent!SomeControl.SetFocus
A workaround for this peculiar problem that solved it for me in the end was to make the field in the subForm look like a hyperlink (ie. user can "click" on it).
Then make an OnClick Event which sets the correct focus, that is:
Public Sub [Field]_Click()
On Error GoTo Goto_Err
'some other code
Me.Parent.[Control].SetFocus
Goto_Exit:
Exit Sub
Goto_Err:
MsgBox Error$
Resume Goto_Exit
End Sub
This workaround is not ideal as the user has to click the field itself, rather than simply move to an another record by pressing up/down arrow.

Pausing code to passing Value of User input

I'm using Access 2010 and would like to have a user click a button in Form1 called "Save As" that initiates VBA code, and before the code is done running, have it pause, open up Form2 (where the user enters a "Save As" title, and clicks Save), and then have the code continue running in Form1 using the string entered in Form2 as a variable.
I'm just not sure how to "pause" my code in Form1 as it waits for the user to make an input. Any ideas on how to approach this?
From Form1, open Form2 in dialog mode. Form1 code will wait on Form2.
Add a "done" button to Form2, and add a procedure to its click event which reopens Form2 hidden hides the form by setting its .Visible property to False. Making it hidden breaks it out of dialog mode. So after the user inputs her value, she can click "done", flow control will resume in Form1 and can read the input value from the control on the now-hidden Form2.
With this approach, Form2 need not know anything about Form1. So Form2 can be used to gather input for any arbitrary form ... or other code.
So assuming Form1 has a command button named cmdSaveAs, use something like this as its click event procedure.
Private Sub cmdSaveAs_Click()
DoCmd.OpenForm "Form2", acNormal, WindowMode:=acDialog
' the next line is not executed while Form2 is modal (open in dialog mode);
' but after Form2 is hidden, we can read the value of the text
' box named txtInput
MsgBox "User input was: " & Forms!Form2!txtInput
End Sub
You'll want to open Form2 using Docmd.OpenForm with the WindowMode parameter set to acDialog. This causes code on the calling form (Form1) to stop and wait for the called form (Form2) to close. On Form2, when you click the Save button, you could write the Save As title to a hidden textbox control on Form1 or set a property on Form1 to the value you want. Make sure you plan for what you want to happen if the user doesn't enter a value for the Save As title when you return to Form1.