By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,682 Members | 1,999 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Hide/Display SubMenu Buttons

Denburt
Expert 100+
P: 1,356
This code is for a Toggle Button layout on a form, with this code you can set a number of toggle buttons visible and have multiple submenus that will stay hidden when not in use. My main menu is set up with this code and the way I set it up the first button is my search toggle button no submenu (this prevents a submenu from displaying when the form is first opened) TabIndex set to 0 (I like using the keyboard) the second toggle button is called Forms, the forms toggle button has submenu buttons for the forms I want displayed. The TabIndex can be a bit mundane at times trying to keep things in order but the nice thing is that in Design view of this form you can put the buttons any where you want and they will position themselves when they are called. I like having the tab index structured like so, the search button would be 0 Forms button would be 1 the first submenu button would be 2 the next submenu = 3 continue until all submenu buttons have their index then back to the menus next main button.

I placed the following in a module I keep for a majority of my form code. You may want to rename the bold statement to your TabIndex=0 control

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public myDetVal As Integer
  5. Public XVal As Integer
  6. Public myX As Integer
  7. Public myY As Integer
  8.  
  9. Public Sub FocusYN(frm As Form, Optional strCtlName As String)
  10. Dim ctl As Control
  11. Dim GrpInt As Integer, i As Integer, cnt As Integer, myInt As Integer, inInt As Integer
  12. Dim GrpNme() As Variant, GrpOrd() As Variant
  13. Dim FndIt As Boolean
  14. On Error GoTo FocusYNErr
  15. If strCtlName <> "Detail" Then
  16.     If frm(strCtlName).Tag <> "" Then
  17.         GrpInt = Right(frm(strCtlName).Tag, 1)
  18.         frm(strCtlName).Value = 1
  19.     Else
  20.         frm(strCtlName).Value = 0
  21.     End If
  22.     frm(strCtlName).SetFocus
  23.     myDetVal = 0
  24. Else
  25.     frm!Search.SetFocus
  26. End If
  27.  
  28. ReDim Preserve GrpOrd(myInt)
  29. For Each ctl In frm.Controls
  30.     If ctl.ControlType = 122 Then 'also known as ControlType acToggleButton
  31.         If strCtlName = "Detail" Then frm(ctl.Name).Value = 0
  32.         If GrpInt = Right(frm(ctl.Name).Tag, 1) And Left(frm(ctl.Name).Tag, 1) <> "V" Then
  33.             If ctl.Name <> strCtlName Then
  34.                 inInt = InStr(frm(ctl.Name).Tag, ";")
  35.                 If Left(frm(ctl.Name).Tag, inInt - 1) - 1 > myInt Then
  36.                     myInt = Left(frm(ctl.Name).Tag, inInt - 1) - 1
  37.                     ReDim Preserve GrpOrd(myInt)
  38.                 End If
  39.                 GrpOrd(Left(frm(ctl.Name).Tag, inInt - 1) - 1) = ctl.Name
  40.             End If
  41.         Else
  42.             If ctl.Name <> strCtlName And frm(ctl.Name).Value <> 0 Then
  43.                 frm(ctl.Name).Value = 0
  44.             End If
  45.             If frm(ctl.Name).Tag <> "" And Not frm(ctl.Name).Tag Like "V*" Then
  46.                 frm(ctl.Name).Visible = False
  47.             End If
  48.         End If
  49.     End If
  50. Next
  51.  
  52. For i = 0 To UBound(GrpOrd)
  53.     Set ctl = frm(GrpOrd(i))
  54.     If ctl.ControlType = 122 And strCtlName <> "DETAIL" Then 'also known as ControlType acToggleButton
  55.      Select Case i
  56.         Case 0
  57.             ctl.Top = frm(strCtlName).Top
  58.             ctl.Left = frm(strCtlName).Left + frm(strCtlName).Width
  59.         Case 1 To 6
  60.             ctl.Top = frm(GrpOrd(i - 1)).Top + frm(GrpOrd(i - 1)).Height
  61.             ctl.Left = frm(strCtlName).Left + frm(strCtlName).Width
  62.         Case 7
  63.             ctl.Top = frm(strCtlName).Top
  64.             ctl.Left = ((frm(strCtlName).Left + frm(strCtlName).Width) + frm(strCtlName).Width)
  65.         Case 8 To 13
  66.             ctl.Top = frm(GrpOrd(i - 1)).Top + frm(GrpOrd(i - 1)).Height
  67.             ctl.Left = ((frm(strCtlName).Left + frm(strCtlName).Width) + frm(strCtlName).Width)
  68.         Case 14
  69.             ctl.Top = frm(strCtlName).Top
  70.             ctl.Left = (((frm(strCtlName).Left + frm(strCtlName).Width) + frm(strCtlName).Width) + frm(strCtlName).Width)
  71.         Case 15 To 20
  72.             ctl.Top = frm(GrpOrd(i - 1)).Top + frm(GrpOrd(i - 1)).Height
  73.             ctl.Left = (((frm(strCtlName).Left + frm(strCtlName).Width) + frm(strCtlName).Width) + frm(strCtlName).Width)
  74.         Case Else
  75.             MsgBox "Check your controls or your code there appears to be more buttons than 3 rows of 7 in each!"
  76.     End Select
  77.     frm(GrpOrd(i)).Visible = True
  78.     End If
  79. SkipIt:
  80. Next
  81. Set ctl = Nothing
  82.  
  83. FocusYNExit:
  84. Exit Sub
  85. FocusYNErr:
  86. If Err.Number = 2100 Then
  87.     MsgBox "Your form is too small to show all of the buttons!"
  88.     Exit Sub
  89. ElseIf Err.Number <> 0 Then
  90.     MsgBox Err.Number & "   " & Err.Description
  91. End If
  92. End Sub
  93.  

To Effectively show & hide the buttons you will need to place a value in the tag properties of the main button that will control the buttons to be shown or hidden. For Example 1 main button on my menu form is called "Forms" it has a tag property = VGrp1 (V for always visible and I used the letters Grp to relay which group it belongs too). The submenu buttons of this Forms button should have a tag property starting with a number for the order you want them displayed starting at the top working your way down then to the right and separated by a semi colon they should be numbered starting with 1 in the order that you want them listed such as the “Add Invoice” button has a tag property = 1;Grp1 the next button following below has a tag property = 2;Grp1 (Button 2 of the same group)! The Menu next main button would have a tag of VGpr2 and the submenu buttons would be tagged with 1;Grp2,2;Grp2 etc. up to 21 buttons 3 rows of 7.

Now for the rest of the code in the forms module:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Form_Load()
  5. myDetVal = 0
  6. XVal = 1
  7. myX = 1
  8. Detail_MouseMove 1, 0, 1, 1
  9. End Sub
  10.  
  11. Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  12. If myDetVal = 0 Then
  13.     If XVal = 0 Then
  14.         myX = X
  15.         myY = Y
  16.         XVal = 1
  17.     Else
  18.         If myY > 0 Then
  19.             If myY - Y > 100 Or myY - Y < -100 Then
  20.                 FocusYN Me, "Detail"
  21.                 myDetVal = 1
  22.             End If
  23.         End If
  24.         If myX > 0 Then
  25.             If myX - X > 100 Or myX - X < -100 Then
  26.                 FocusYN Me, "Detail"
  27.                 myDetVal = 1
  28.             End If
  29.         End If
  30.     End If
  31. End If
  32. End Sub
  33. Private Sub Search_GotFocus()
  34.     Detail_MouseMove 1, 0, 1, 1
  35. End Sub
  36.  
  37. Private Sub Search_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  38. Detail_MouseMove 1, 0, 1, 1
  39. End Sub
  40. Private Sub Toggle1_GotFocus()
  41.     If Me!Toggle1 = 0 Then
  42.         FocusYN Me, "Toggle1"
  43.     End If
  44. End Sub
  45.  
  46. Private Sub Toggle1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  47. If Me!Toggle1 = 0 Then
  48.     Me!Toggle1.SetFocus
  49.     FocusYN Me, "Toggle1"
  50. End If
  51. End Sub
  52. Private Sub Toggle3_GotFocus()
  53.     If Me!Toggle3 = 0 Then
  54.         FocusYN Me, "Toggle3"
  55.     End If
  56. End Sub
  57.  
  58. Private Sub Toggle3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  59. If Me!Toggle3 = 0 Then
  60.     Me!Toggle3.SetFocus
  61.     FocusYN Me, "Toggle3"
  62. End If
  63. End Sub
  64.  
Mar 23 '07 #1
Share this Article
Share on Google+