VBA - Reorder Access 2010 Navigation Form Tab Index - ms-access

Is there a way to dynamically reorder the navigation buttons on the Access 2010 Navigation Form?
I would like to hide certain buttons depending on user type; however simply hiding them leaves a gap between buttons if the order cannot be modified.
I have tried similar to the following with no luck.
Me.NavigationButton1.TabIndex = 1
Me.NavigationButton2.TabIndex = 0
Me.Requery
Me.Refresh
Thank you in advance.

NavigationButtons work a bit differently. They are managed in a NavigationControl container that does not resize automatically if underlying buttons are hidden.
However, you can try this, it works for me:
' Always remember the normal size of the button '
Static bt1Witdh as Long
if bt1Width = 0 then bt1Width = Me.NavigationButton1.Width
' Now hide the button '
Me.NavigationButton1.Width = 0
Me.NavigationButton1.Visible = False
' Unhide the button '
Me.NavigationButton1.Visible = true
Me.NavigationButton1.Width = bt1Width
You can make this a lot more generic and a bit 'hacky' by using the control's Tag property as a temporary storage for keeping track of their normal width:
Public Sub HideNavigationButton(bt As NavigationButton)
' Store the width if we haven't already done so '
If bt.Tag = vbNullString Then bt.Tag = CStr(bt.Width)
bt.Width = 0
bt.Visible = False
End Sub
Public Sub ShowNavigationButton(bt As NavigationButton)
If bt.Visible Or bt.Width > 0 Then Exit Sub
bt.Visible = True
bt.Width = CInt(bt.Tag)
End Sub
To use it:
' Hide '
HideNavigationButton Me.NavigationButton1
' Show '
ShowNavigationButton Me.NavigationButton1

Related

Access VBA Set Focus in Column

good afternoon,
I have an access with a subform (sub_frm_robo2) that has a specific column (CD_SENHA).
In the Form Load event I put the code: (Me.sub_frm_robo2.Form! SENHA.InputMask = "Password")
and I am trying to create a condition that at the moment this Column
(CD_SENHA) receives focus (Me.sub_frm_robo2.Form!SENHA.SetFocus),
the data mask be removed (Me.sub_frm_robo2.Form!SENHA.InputMask = "")
and when the focus changes to the next column return the data mask to the initial format (Me.sub_frm_robo2.Form! PASSWORD = "Password")
Below some images to better exemplify
Before Focus
With Focus
After Focus
I think the code would look something like this
Do While Me.sub_frm_robo2.SetFocus = True
If Me.sub_frm_robo2.Form!SENHA.SetFocus Then
Me.sub_frm_robo2.Form!SENHA.InputMask = ""
End If
Next
Can you help me?
Well, I did not have any help here but I managed to resolve after a few attempts and errors, just to code a code in the event of each column of the subformaluario to make it work. Good luck to everyone
Private Sub CNPJ_AF_GotFocus()
Me.Form!SENHA.InputMask = "Password"
End Sub
Private Sub SENHA_GotFocus()
Me.Form!SENHA.InputMask = ""
End Sub

How to short the code to change control properties on command buttion click in ms-access vba

I have a userform with a group of command buttons with similiar properties and fuctionality. I am using these buttons to let the user keep track about his activity. So e.g. whenever user will click the "production" button the application label will say "production" mode, when the user presses "Break" button the label will change from "production" mode to "Break" mode, Similiarly I have 7-8 buttons altogether on same form. Other conditions are whichever buttons gets pressed that button should get disabled and all other buttons should get enabled, followed by next button, if pressed it should get disabled and it should enable the button which was pressed previously.
The button which has been pressed whould also change its color and should also change the previous button's color back to normal.
I understand it would be difficult to understand the scenario, please check the code below for one of my buttons and its working well.
Private Sub btn1()
Me.Label78.Caption = Me.btn1.Caption
Me.btn1.BackColor = RGB(250, 100, 100)
Me.btn1.Gradient = 12
Me.btn1.Enabled = False
Me.btn2.Enabled = True
Me.btn2.BackColor = RGB(100, 250, 100)
Me.btn2.Gradient = 12
Me.btn3.Enabled = True
Me.btn3.BackColor = RGB(100, 250, 100)
Me.btn3.Gradient = 12
End Sub
the above code works perfeclty and changes properties to 3 buttons(its sample code not the code for all 7 buttons), but I have total 7-8 button and i may add more buttons in future SO if i keep on writing this kind of code in one button for all 7-8 buttons, then it will take a lot of time and efforct.
So what is the best possible way to short this code ?
I know this is little complicated hence please ask if any more information required.
Thanks in advance !!
You can have a helper subfunction:
Private Sub SetButton(ByVal ButtonId As Long, ByVal Enabled As Boolean)
Dim BackColor As Long
If Enabled Then
BackColor = RGB(100, 250, 100)
Else
BackColor = RGB(250, 100, 100)
End If
With Me("btn" & Cstr(Id))
.Enabled = Enabled
.Gradient = 12
.BackColor = BackColor
End With
End Sub
Then call this in the OnClick event like:
For Id = 1 To 8
Select Case Id
Case 1, 2, 4, 7
SetButton Id, True
Case Else
SetButton Id, False
End Select
Next

MS Access Toggle Button background

Is it possible to totally remove the background color and the border of a toggle button in Access? As shown in the Imgur picture, I'd like to remove the grey-ish background and the border of the button totally so I'd keep the white icon (that list icon) only. Would that even be possible?
I am sure this is related to your other question at MS Access ToggleButton Picture change.
Use an Image control with embedded image and this code (note declaration of public variable in header):
Option Compare Database
Option Explicit
Public booMenu
Sub timeout(duration_ms As Double)
Dim Start_Time As Double
Start_Time = Timer
Do
DoEvents
Loop Until (Timer - Start_Time) >= duration_ms
End Sub
Private Sub Image5_Click()
Dim x As Integer
booMenu = Not booMenu
Me.Menu.Visible = True
Do
DoEvents
Me.Menu.Width = Me.Menu.Width + IIf(booMenu, 200, -200)
Me.Menu.Left = Me.Menu.Left - IIf(booMenu, 200, -200)
Me.Image5.Left = Me.Image5.Left - IIf(booMenu, 200, -200)
timeout (0.01)
x = x + 1
Loop Until x = 10
Me.Menu.Visible = booMenu
End Sub
I managed to find (kind of) a solution to this. The solution was to change the following parameters:
Use Theme = No;
BackColor = Form Color;
Pressed Back Color = Form Color
Eventually, it provides an effect very similar to the one that I was trying to achieve.

Why won't the ForeColor of this label visibly change when altered in a loop?

I am making a form that has groups of controls that I want to visibly enable/disable with a related toggle button (let's call them Group Toggles). Each group has a different variety of control types, so I made a common procedure to handle the toggling:
'constants for control ForeColors
Public Enum LabelForeColor
Default = 8355711
Off = 14277081
End Enum
Public Enum ListForeColor
Default = 4210752
Off = 12566463
End Enum
Public Sub EnableControl(Ctrl As Control, Enabled As Boolean)
With Ctrl
Select Case Ctrl.ControlType
Case acLabel
If Enabled Then .ForeColor = LabelForeColor.Default Else .ForeColor = LabelForeColor.Off
Debug.Print "LABEL", .ForeColor
Case acListBox
If Enabled Then .ForeColor = ListForeColor.Default Else .ForeColor = ListForeColor.Off
.Enabled = Enabled
Debug.Print "LIST", .ForeColor
Case acCommandButton
.Enabled = Enabled
Debug.Print "BUTTON", "NA"
Case acCheckBox
.Enabled = Enabled
Debug.Print "CHECK", "NA"
Case Else
Debug.Print "Control [" & .Name & "] is not of a type that EnableControl can handle."
End Select
End With
End Sub
Each group of controls is represented by a collection. When the form is loaded, every control with a particular tag property is added to the corresponding collection. The Group Toggles are not added to any collection and instead have event procedures that look like this:
Private Sub ToggleGroup1_AfterUpdate()
Dim State As Boolean
'a public function that converts the toggle button's value to a boolean
State = FormCommon.ToggleButtonState(ToggleGroup1.Value)
Dim iCtrl As Control
For Each iCtrl In Controls_ByPlant
FormCommon.EnableControl iCtrl, State
Next iCtrl
End Sub
When I click on a GroupToggle, all of the controls in the corresponding group visibly change appropriately, except for the labels. After an hour of troubleshooting, here's what I know:
The ForeColor property of the label does change, but not visibly.
When I call EnableControl on a label outside of a loop, the label visibly changes.
It doesn't matter if I pass the label object specifically to the subroutine or if I pass it from its group collection; the change is visible in both cases
If I toggle-disabled a label as part of the Group Toggle event and then call EnableControl specifically on that label to try to disable it again, there is no visible change (probably because the ForeColor property is already set to the "off" color)
Turning screen updating off with Application.Echo while the Group Toggle event runs and then turning it back on at the end of the event does not make a difference.
Making the Group Toggle event run with a For i = 1 to .Count instead of a For Each does not make a difference.
This problem also occurs when changing a different visual property instead, such as ForeTint.
(Per comments) Repaint does not make a difference
(Per comments) DoEvents does not make a difference
Why is this happening?
(First ever question, so apologies if I messed something up in the post)
This was interesting, but somewhat anticlimactic.
Your code does work for the labels, but what happens is this:
All labels are associated with input controls (as it is usual)
When you deactivate a group, you disable the input controls (.Enabled = Enabled)
This automatically sets the associated labels to a (system defined) light gray text color which cannot be changed.
This "disabled label" color is very similar to your LabelForeColor.Default color, so it is hard to see the change when toggling. But it does change.
Change your color constants to make the effect more visible:
Public Enum LabelForeColor
Default = vbRed ' 8355711
' the "Off" color is never visible, unless you add an un-associated label to a group
Off = vbBlue ' 14277081
End Enum
Edit: your test code FormCommon.EnableControl iCtrl, False works, because it only affects the label, but doesn't disable its associated list box.

How to make global changes to Access forms?

I've inherited a database which has a tabbed form, which in turn has tabbed subforms. Many of the subforms have a label that has to be corrected. If this were a Word document or Excel workbook, I'd use Find-and-Replace-All. You can't do that across controls on forms, can you?
Is there a way to do so using VBA? I think it would look something like:
Set aForm = Me
For each aPage in aForm.Pages
For each aControl in aPage.Controls
if aControl.type = "Label" then
if aControl.Value = "OldText" then aControl.Value = "NewText"
end if
Next aControl
Next aPage
EDIT -------------
I tried this code:
For Each aForm In Application.CurrentProject.AllForms
For Each aPage In aForm.Pages '<---- Error here
For Each aControl In aPage.Controls
If aControl.Type = "Label" Then
If aControl.Value = "OldText" Then aControl.Value = "NewText"
End If
Next aControl
Next aPage
Next aForm
But I get an error where I put that arrow. It says "Object doesn't support this property or method", which means it doesn't like "aForm.Pages". What's the right way to cycle through tabbed pages?