I'm trying to set focus on TextBox but Focus() method returns false
this.NameTextBox.Focus(FocusState.Programmatic);
However in msdn it is written:
Focus(FocusState value)
Return value: True if focus was set to the control, or focus was
already on the control. False if the control is not focusable.
and
You typically pass FocusState.Programmatic as the parameter to
indicate the control obtained focus through a deliberate call to the
Focus method. For example, if clicking an "Edit" button causes focus
to be set on a TextBox, use the Programmatic focus state.
So why does my command not work?
Related
I have created a front and back-end database MS Access solution (I am limited to MS Access 2013) for my team, utilizing forms for data entry, lookup, and editing. It is working well, but every now and then a user will forget they are in "Filter by form", and enter data into the filter instead of the actual form. When they realize their mistake, they have to re-enter all of the data.
Is it possible to change the appearance of the form when you are in "Filter by Form"? For instance, change the background color, or add a text box notification so that it is very obvious to the user that they are in "Filter by Form"?
You could use the Me.FilterOn property to help determine whether the form filter (ie., Home→Advanced→Filter By Form ) is on.
You might need to play with it a bit but from my experiment, you could have something in the OnOpen event that checks the property and acts appropriately.
I believe the catch is that code won't execute when it's in Filter mode, so therefore, you'd have to make the default appearance the "Filter appearance" (perhaps add a Graphic or Textbox that says "Filter Mode"), and then if Me.FilterOn = False you can hide that label in the OnOpen event.
Also:
The Apply Filter button indicates the state of the Filter and FilterOn properties. The button remains disabled until there is a filter to apply. If an existing filter is currently applied, the Apply Filter button appears pressed in.
To apply a filter automatically when a form or report is opened, specify in the OnOpen event property setting of the form either a macro that uses the ApplyFilter action or an event procedure that uses the ApplyFilter method of the DoCmd object.
(Source)
More Information:
Office.com : Apply a filter to view select records (Form Filter)
expertsexchange : Determine if filter is applied
MSDN : Form.FilterOn Property (Access)
I have a form with several groups of controls and I want to be able to toggle enabled/disabled those groups individually with a Toggle Button control. When the form loads, I programmatically assign a UDF called ToggleGroup to each toggle button's AfterUpdate() event handler. I have tested this part of the code and know that it performs as intended.
I want the groups to start in a disabled state, so I want to trigger the AfterUpdate() handler for each toggle button after I assign the function. I know that I could achieve this in a few other ways (I could change the properties of the grouped controls in Design View or I could call ToggleGroup on them as part of the form's initialization), but for my own curiosity and interest, I want to know if the method I described is feasible.
'''GroupTags is a collection of all Tags associated with control groups
For Each iTag In GroupTags
CtrlName = "Toggle_" & iTag
Me(CtrlName).AfterUpdate = _
"=ToggleGroup( ...valid arguments... )"
'''Here: trigger the AfterUpdate event for this control based on CtrlName
Next iTag
Objective: Show a programmatic modal form with the appropriate control initially selected and ready for user input or interaction.
Code:
Dim frm As Form
Set frm = New Form_Popup_Sign
With frm
'Show window and SetFocus appropriately
.Visible = True
!CommandCancel.SetFocus
'.Modal = False: .Modal = True
'Loop until response (change in form.visible state) is received on popup
'User clicking Cancel or Sign sets .Visible = False
Do While .Visible
Sleep 1 'Sleeping makes CPU effect negligible
DoEvents
Loop
'Return popup_sign handle and leave form open to expose results
load_Popup_Sign = frm.hWnd
End With
' Form is destroyed later
Results:
The target control's GotFocus event DOES fire.
The form appears but appears to have nothing focused (selected).
In the forms pictured below, when the user clicks Cancel or Sign, Form_Popup.Visible is set to False, allowing the code execution to continue.
What appears (highlighted but not selected)
What I expect (button selected, user can hit enter or space to push button)
Things I've Tried
The following workarounds make the SetFocus'd control get focused:
Modality is programmatically turned on and off (see commented code line)
I would like to not need to do this
Interestingly, if form is saved as not modal and then modal is turned on at the line noted above, this no longer works. Weird.
Manual user actions:
Clicking the Header form section background
Alt-tabbing or clicking away to another app and back
Things that don't work:
Setting focus multiple times
Setting focus to form itself (which modality should do by default)
Setting focus to different control types (which is my actual intent), e.g. textboxes
Do not appear selected and cannot immediately type text in like when focused normally
Tabbing interestingly shifts "focus" but never selects the controls which have "focus".
Buttons "focused" in this manner have the peripheral highlight but no "selected" indication (thin dotted line around caption).
Textboxes and listboxes "focused" in this manner have no indication they have been tabbed to and cannot be typed into.
Setting TabIndex for the desired control to 0 and both selecting nothing or that control.
References:
MSDN VBA Form.Modal property
MSDN VBA SetFocus method
Working in both A2003 & A2007.
How do we ensure that a selected TextBox gets the focus when the form loads? If we put MyTextBox.SetFocus in the Form_Load then we get the error:
can't move the focus to the control
This form is designed for rapid data entry, and the form somewhat rearranges itself based on the last used settings. So there are several different textboxes any of which may need the focus depending on the user. We can't just fix it in design time by giving MyTextBox TabIndex=0.
The help says something about calling Repaint which just doesn't make any sense at all:
You can move the focus only to a
visible control or form. A form and
controls on a form aren't visible
until the form's Load event has
finished. Therefore, if you use the
SetFocus method in a form's Load event
to move the focus to that form, you
must use the Repaint method before the
SetFocus method.
The best bet in this case, is to ensure that the textbox to get focus is numbered 0 in the Tab Index property.
You cant set the focus as the controls don’t really exist yet, try putting the code in the OnActivate event instead
Or just put a DoCmd.Repaint in the OnLoad event before trying to set the focus. Both should work but I'm not near a computer to check
In my experience, I've always gotten that error when the control I was trying to set focus to was either 1)not visible or 2)not enabled. I assume you've already checked those, but it would be worth double checking at runtime when you get the error message (especially since you said you are shuffling the controls at runtime).
I use the .SetFocus method pretty regularly without trouble. I don't recall ever getting an error message when setting focus to a control that already has it as Remou stated in his answer.
I believe there is also a third case that occurs if you try to set focus to a control in the form header/footer of a bound form that has had all of its records filtered out. I know that situation causes "disappearing" contents in an unbound combo box, but I think it may also play havoc with the SetFocus method. If you are opening the form in Data Entry mode, though, that should not be an issue.
Move SetFocus to the form's On Current event. Should work then unless perhaps the form's record source contains no records and you've set the form's Allow Additions property to No. In that case your text box will not be available to SetFocus on, but in my testing it doesn't throw an error.
I have a method called checkKeyCode(obj) which simply takes the character you type into a textbox and replaces it with its keyCode.
This method is called from onkeydown attribute of textbox input (onkeydown="return checkKeyCode(this)").
The problem is then I want to be able to press tab and focus to the next element. I don't have to know the element's id or name or tag. Basically I want to combine my method's functionality with the default functionality of browser when you press Tab key (which sets focus to the next element in the form).
Can this be done. If so how?
Any help would be greatly appreciated.
Check the keycode in your handler and if the value is 9, it's a tab. Exit your handler if it is and return false.
Also, don't use the onkeydown event. Use onkeyup instead.