How to hide button control in VBA - ms-access

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

Related

MS-Access: Populate modal form from custom type

I am trying to display an error form, populated with text from a custom type object, as a MS-Access version of the fantastic answer on this Excel question.
The problem I run into is that I want to wait for a user response/confirmation on this error form, so it must be (as far as I am aware) a modal form and therefore I cannot just open the form and immediately populate it.
I have tried to find a way of doing in Access what was done in Excel; load the form, populate the form then display the form, but this doesn't seem possible since Access' event order is Open->Load->...
I have also tried looking for a way to open as a normal form, populate and then 'modalise' the form but could not find a way to do this.
Does anyone know of a way to achieve this function? Or is there an alternative to modal forms to pause execution awaiting user input?
A modal form is VERY different from a dialog form.
Modal forms do NOT cause the calling code to halt, but a dialog form does.
(do not confuse the two types of forms).
To “get back” a user response from a dialog form, simply set the form visible = false in place of a close form command. This will KICK the dialog form out of dialog mode, and your calling code now continues and the calling code is “free” to examine any values the user typed in or say choose from combo boxes, or check boxes, or whatever.
So your code block will look like this:
Private Sub Command0_Click()
Dim f As String
Dim strT As String
f = "formB"
DoCmd.OpenForm f, , , , , acDialog
If CurrentProject.AllForms(f).IsLoaded Then
' grab any values entered by user, or whatever buttons hit
strT = Forms(f).Text1
' etc. etc. etc. - grab values from that form
' don't forget to close the form
DoCmd.Close acForm, f
Else
' user canceled the form
' code goes here for user hitting your cancel button or "x"
End If
End Sub
And in your dialog form, the “ok” button, or “close” button simply goes:
Me.visible = False
If the user hits your cancel button, that code would be:
Docmd.Close acForm
So if the user hits “cancel” or even the “X” in the upper right corner, you consider and assume the user “bailed out”. When they do this, the form will be closed.
So the code after the dialog part simply checks if the form is STILL open (because your “ok” button does a visible = false).
And since the form is STILL open, then you are free to grab the values of ANY control – say user text typed into a text box, values from a combo box, check box, or whatever.
When user is done the calling code is free to examine or grab any value(s) from that form.
Edit - 2nd solution to allow "setting" of values.
This code will work:
Private Sub Command0_Click()
Dim f As String
Dim strT As String
f = "formB"
DoCmd.OpenForm f
Forms(f).Text1 = "Hello" ' set values on form
' now WAIT for user
Do While CurrentProject.AllForms(f).IsLoaded
If Forms(f).ok = True Then Exit Do
DoEvents
Loop
If CurrentProject.AllForms(f).IsLoaded Then
If Forms(f).ok = True Then
MsgBox "value return = " & Forms(f).Text1
Else
MsgBox "user cancel"
End If
DoCmd.Close acForm, f
Else
' cancel code goes here
MsgBox "user cancel"
End If
End Sub
The code for the OK button on the form B is now:
Me.Visible = False
Me.ok = True
And you need a public var "ok" in form B.
eg this:
Option Compare Database
Option Explicit
Public ok As Boolean
So the cancel button in form B can simply close the form. (you can close the form - but don't set ok = True)

Access Form Controls Flicker on control being setFocus from Class

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?

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!

Greying out textboxes

Hey I have a checkbox that I want a textbox linked to that turns grey if the checkbox is not clicked. I have the code under the on click event on the checkbox and this works if the box is clicked the first time..... It allows the textbox to become enabled. But I want it to if the used accidently clicks the checkbox they can click off the checkbox and the textbox goes back grey again. Im not sure if I have the code on the wrong event for the checkbox or not. Thanks.
Private Sub FlightScheduleAffected_Click()
If IsNull(Me.FlightScheduleAffected.Value) Then
Me.TimeMOCCNotified.Enabled = False
Else
Me.TimeMOCCNotified.Enabled = True
End If
End Sub
The value of the checkbox should be true or false, it will never be null. You want your if statement to read
If Me.FlightScheduleAffected.Value Then
Me.TimeMOCCNotified.Enabled = True
Else
Me.TimeMOCCNotified.Enabled = False
End If

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