Access Form Controls Flicker on control being setFocus from Class - ms-access

I have been working on getting the flickering on a Access Form to stop.
The same problem appears in Access 2013 and 2016.
I have 24 toggle buttons on a form. Due to certain limitations of OptionGroup control, I have built all the functionality I need into a group of Toggle Buttons instead. This is attached to the buttons through a class for each set of buttons.
Everything is working correctly but when the user presses left, right, up or down arrows the controls flicker.
The Code:
Private Sub tglS_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = 1 Then Exit Sub
Select Case KeyCode
Case 39, 40
KeyCode = 0
tglSI.SetFocus
tglSI.Value = True
tglSI_AfterUpdate
Case 37, 38
KeyCode = 0
tglNA.SetFocus
tglNA.Value = True
tglNA_AfterUpdate
End Select
End Sub
I have stepped through the code and found it is whenever a control is SetFocus.
All controls flicker both bound and unbound controls. The toggle buttons are on a tab page and flickers the controls off the tab as well.
I have tried placing Application.Echo statements on each side of the SetFocus statement but no change.
Thanks for any help you can provide.

It's hard to tell if you did this so just to be sure, did you set a value to echo?
Application.Echo False
Select Case KeyCode
Case 39, 40
KeyCode = 0
tglSI.SetFocus
tglSI.Value = True
tglSI_AfterUpdate
Case 37, 38
KeyCode = 0
tglNA.SetFocus
tglNA.Value = True
tglNA_AfterUpdate
End Select
Application.Echo True

your code refers to a function tglSI_AfterUpdate and tglNA_AfterUpdate - do those functions or subroutines change the echo of the form? I have had bad luck with the echo actually causing the flicker, along with conditional formatting that may alter the visibility of controls during events too. Do the toggle buttons have images on them or text?

Related

Toggle visibility of other fields on form based upon combobox selection - MS Access

Question...
How can I toggle the visibility of several other fields (checkboxes/textboxes) on form based upon the selection of a combobox item. The image below shows a listbox but either way, how do use vba code to turn on or off visibility of all the fields in the grey box. Basically, if the combobox selection is scheduled then visible=true. Else visible=false How can I code this???
Use combobox AfterUpdate event and probably form Current event as well. So build a procedure that can be called from both events, something like:
Sub Form_Current()
SetVisible
End Sub
Sub cbo1_AfterUpdate()
SetVisible
End Sub
Sub SetVisible()
Me.tbx1.Visible = Me.cbo1 = "scheduled"
Me.cbx1.Visible = Me.cbo1 = "scheduled"
End Sub
Alternative is to use Conditional Formatting for textboxes and comboboxes (sorry, not applicable to other controls) to Enable/Disable as well as set colors so appear not visible.

Tab control in VB

I'm working on a legacy VB6 application. I'm sure this probably relates to VB.NET so i will tag it, but please let me know if it's completely different(which I dont think it is) then I'll remove the tag to avoid confusion.
Here is my issue....
I have a Tab control with multiple tabs: 0 - 3. On TabStuff.Tab = 0, I have a few textboxes and comboboxes. The user uses keyboard TAB to move from Indexed controls. What happens is once they get to the last control which is a textbox called txtCity - and click keyboard TAB once more, it brings them to TabStuff.Tab=1.
My issue is I do VALIDATE on txtCity - I call a function that verifies that a couple of the fields aren't NULL and if one of the fields is in fact NULL then I show a MSgBox and try to setFocus on that control. But instead, when OK is clicked on msgbox, it goes to the next tab which is TabStuff.tab=1 which is not correct.
Here's some of my code...
Dim FirstName, City as String
flag=false
firstName = txtName.text
city = txtcity.text
if FirstName="" or isnull(FirstName) then
msgbox "Please enter Name"
tabstuff.tab=0
txtname.setfocus
exit sub
elseif city = "" or isnull(city) then
msgbox "Please enter city"
tabstuff.tab=0
txtcity.setfocus
exit sub
end if
flag=true
This code is in txtCITY_VALIDATE
So in case city was empty, it shows MsgBox, stays on Tab=0 and setfocus on that control, but instead it goes to the next tab=1 and sets focus on the first control of that tab.
EDIT:
in txtCITY_LostFocus
If Flag = False Then
TabStuff.Tab = 0
Exit Sub
End If
I added this but it still goes to tabstuff.tab=1 setting the focus on first control of the tab
EDIT 2:
In a new project i created txt1 and txt2 - i set TabIndex 0 and 1 respectively.
Private Sub Txt1_Validate(Cancel As Boolean)
If Txt1.Text = "" Then
MsgBox "no text in txt1"
Txt1.SetFocus
End If
End Sub
This is the code I use. I click TAB on txt1 without entering any text, so this gets executed, but after msgbox, the focus gets set on txt2
For some extremely weird reason - i seem to have been getting this discrepancy because I was doign it in the VALIDATE property. When i entered the same code in LostFOCUS it seems to work fine. Thanks everyone for your help with this!

MS Access de-select listbox after running macro/query

So I have a form (frmBookingForm) in Access, and a listbox (lstMyBookings) that displays the results of a query.
Below this, I have a button (cmdDeleteBooking) which runs a delete query using the lstMyBookings selection as input. The button then runs a macro that firstly checks whether a record in the listbox is selected, and if one is, runs the delete query and requeries the data, so the deleted record is no longer shown in the listbox. If not, an error message is displayed.
However, if I then click the button again, it again runs the delete query, even though there is apparently nothing selected in the list box.
Essentially, how can I 'clear' the listbox selection?
I'd prefer a solution that can be done in a macro format, as I have little to no understanding of VBA. However, if providing a VBA solution, I'd greatly appreciate a short explanation of it.
Thanks :)
Looks like this website has a nice little function to do it. Essentially, you need to test if it's a multiselect, and then do one of two things. I suppose if you know ahead of time that it is/isn't a multiselect, you wouldn't even need the "if" statement:
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
If the control's MultiSelect property is set to None, this code just sets List0 to Null. If the control's MultiSelect property is set to anything else, the code loops through all items that are currently selected, and sets the Selected property of that item to False. My example assumes your control is called List0.
EDIT
To use this code, use an event instead of a macro. Here's how you do this:
Right click on the button, select properties
In the Property Sheet window, click on the "Event" tab
Click inside of the "On Click" blank area, and click the dropdown arrow, then select "[Event Procedure]"
Click the ellipsis ("...") to go into the code editor.
In the code editor, your should already have your event for the button (let's assume the button is called Command1:
Private Sub Command1_Click()
End Sub
Add your code in between (assuming the listbox is called List0):
Private Sub Command1_Click()
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
End Sub

Checkbox controls subform visibility - how to keep it visible when form is reopened?

A checkbox controls whether a subform is visible. If the checkbox is "true," the subform is visible. The problem is, when I close and reopen the form the subform is no longer visible even though the checkbox is still true. I have to uncheck and re-check the checkbox before the subform becomes visible again. Here's what I'm using:
Private Sub RefBoardCkbx_Click()
If RefBoardCkbx.Value = True Then
[Admin Sep - Awaiting Prelim SubBox].Visible = True
Else
[Admin Sep - Awaiting Prelim SubBox].Visible = False
End If
End Sub
Obviously, there has to be some way for the form to automatically re-run the code everytime it opens--I don't know how to make it do that!
Use the main form's On Load event to make the subform visible when the form opens.
Me.[Admin Sep - Awaiting Prelim SubBox].Visible = True
You probably also want to put a check in the check box at the same time.
Me.RefBoardCkbx.Value = True
As a side point, consider whether this version of the click event procedure makes sense to you ...
Private Sub RefBoardCkbx_Click()
Me.[Admin Sep - Awaiting Prelim SubBox].Visible = Me.RefBoardCkbx.Value
End Sub

How to hide button control in VBA

Is there anybody out here still programming VBA?
I'm trying to get this code to work
Private Sub button3_click()
'hide main buttons
button1.Visible = False
button2.Visible = False
button3.Visible = False
'show submenu buttons
button4.Visible = True;
button5.Visible = True;
End Sub
What I'm trying to do basically is that I have a main form that has 5 main button controls. 2 of them are hidden on startup. So when I click button 3, I want to hide the first 3 main buttons, and "unhide" the other two.
When trying to execute this event, I got an error
"Runtime Error 2165 - You can't hide a control that has the focus".
Has anybody come across this aspect of programming before? I'm sure it's doable. I just don't understand what went wrong here...
Change the focus to one of the visible control, before hiding the current one
Private Sub button3_click()
'show submenu buttons
button4.Visible = True
button5.Visible = True
DoEvents 'execute any pending events, to make sure the button 4 and 5 are really visible
button4.SetFocus 'change the focus to a now visible control
DoEvents 'execute any pending events, to make sure that button4 really has the focus
'now you can hide the other buttons
'hide main buttons
button1.Visible = False
button2.Visible = False
button3.Visible = False
End Sub
Maybe you can skip the DoEvents command, you should try
I would have thought that the error was explicit enough. Move the focus to some control that you are not trying to hide before you run your code. Also, consider Me : http://msdn.microsoft.com/en-us/library/aa223099(v=office.11).aspx