Advertisement
Guest User

Untitled

a guest
May 5th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Form_Open(Cancel As Integer)
  5. ' Minimize the database window and initialize the form.
  6.  
  7. Dim dbs As Database
  8. Dim rst As Recordset
  9.  
  10. On Error GoTo Form_Open_Err
  11.  
  12. ' Minimize the database window.
  13. DoCmd.SelectObject acForm, "Switchboard", True
  14. DoCmd.Minimize
  15.  
  16. DoCmd.Hourglass False
  17. Set dbs = CurrentDb()
  18. Set rst = dbs.OpenRecordset("My Organization Information")
  19. If rst.RecordCount = 0 Then
  20. rst.AddNew
  21. rst![Address] = Null
  22. rst.UPDATE
  23. MsgBox "Before using this application, you need to enter your company name, address and related information."
  24. DoCmd.OpenForm "My Organization's Information", , , , , acDialog
  25. End If
  26. rst.Close
  27. dbs.Close
  28.  
  29. ' Move to the switchboard page that is marked as the default.
  30. Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
  31. Me.FilterOn = True
  32.  
  33. Form_Open_Exit:
  34. Exit Sub
  35.  
  36. Form_Open_Err:
  37. MsgBox Err.Description
  38. Resume Form_Open_Exit
  39.  
  40. End Sub
  41.  
  42. Private Sub Form_Current()
  43. ' Update the caption and fill in the list of options.
  44.  
  45. Me.Caption = Nz(Me![ItemText], "")
  46. FillOptions
  47.  
  48. End Sub
  49.  
  50. Private Sub FillOptions()
  51. ' Fill in the options for this switchboard page.
  52.  
  53. ' The number of buttons on the form.
  54. Const conNumButtons = 8
  55.  
  56. Dim dbs As Database
  57. Dim rst As Recordset
  58. Dim strSQL As String
  59. Dim intOption As Integer
  60.  
  61. ' Set the focus to the first button on the form,
  62. ' and then hide all of the buttons on the form
  63. ' but the first. You can't hide the field with the focus.
  64. Me![Option1].SetFocus
  65. For intOption = 2 To conNumButtons
  66. Me("Option" & intOption).Visible = False
  67. Me("OptionLabel" & intOption).Visible = False
  68. Next intOption
  69.  
  70. ' Open the table of Switchboard Items, and find
  71. ' the first item for this Switchboard Page.
  72. Set dbs = CurrentDb()
  73. strSQL = "SELECT * FROM [Switchboard Items]"
  74. strSQL = strSQL & " WHERE [ItemNumber] > 0 AND [SwitchboardID]=" & Me![SwitchboardID]
  75. strSQL = strSQL & " ORDER BY [ItemNumber];"
  76. Set rst = dbs.OpenRecordset(strSQL)
  77.  
  78. ' If there are no options for this Switchboard Page,
  79. ' display a message. Otherwise, fill the page with the items.
  80. If (rst.EOF) Then
  81. Me![OptionLabel1].Caption = "There are no items for this switchboard page"
  82. Else
  83. While (Not (rst.EOF))
  84. Me("Option" & rst![ItemNumber]).Visible = True
  85. Me("OptionLabel" & rst![ItemNumber]).Visible = True
  86. Me("OptionLabel" & rst![ItemNumber]).Caption = rst![ItemText]
  87. rst.MoveNext
  88. Wend
  89. End If
  90.  
  91. ' Close the recordset and the database.
  92. rst.Close
  93. dbs.Close
  94.  
  95. End Sub
  96.  
  97. Private Function HandleButtonClick(intBtn As Integer)
  98. ' This function is called when a button is clicked.
  99. ' intBtn indicates which button was clicked.
  100.  
  101. ' Constants for the commands that can be executed.
  102. Const conCmdGotoSwitchboard = 1
  103. Const conCmdOpenFormAdd = 2
  104. Const conCmdOpenFormBrowse = 3
  105. Const conCmdOpenReport = 4
  106. Const conCmdCustomizeSwitchboard = 5
  107. Const conCmdExitApplication = 6
  108. Const conCmdRunMacro = 7
  109. Const conCmdRunCode = 8
  110.  
  111. ' An error that is special cased.
  112. Const conErrDoCmdCancelled = 2501
  113.  
  114. Dim dbs As Database
  115. Dim rst As Recordset
  116.  
  117. On Error GoTo HandleButtonClick_Err
  118.  
  119. ' Find the item in the Switchboard Items table
  120. ' that corresponds to the button that was clicked.
  121. Set dbs = CurrentDb()
  122. Set rst = dbs.OpenRecordset("Switchboard Items", dbOpenDynaset)
  123. rst.FindFirst "[SwitchboardID]=" & Me![SwitchboardID] & " AND [ItemNumber]=" & intBtn
  124.  
  125. ' If no item matches, report the error and exit the function.
  126. If (rst.NoMatch) Then
  127. MsgBox "There was an error reading the Switchboard Items table."
  128. rst.Close
  129. dbs.Close
  130. Exit Function
  131. End If
  132.  
  133. Select Case rst![Command]
  134.  
  135. ' Go to another switchboard.
  136. Case conCmdGotoSwitchboard
  137. Me.Filter = "[ItemNumber] = 0 AND [SwitchboardID]=" & rst![Argument]
  138.  
  139. ' Open a form in Add mode.
  140. Case conCmdOpenFormAdd
  141. DoCmd.OpenForm rst![Argument], , , , acAdd
  142.  
  143. ' Open a form.
  144. Case conCmdOpenFormBrowse
  145. DoCmd.OpenForm rst![Argument]
  146.  
  147. ' Open a report.
  148. Case conCmdOpenReport
  149. DoCmd.OpenReport rst![Argument], acPreview
  150.  
  151. ' Customize the Switchboard.
  152. Case conCmdCustomizeSwitchboard
  153. ' Handle the case where the Switchboard Manager
  154. ' is not installed (e.g. Minimal Install).
  155. On Error Resume Next
  156. Application.Run "WZMAIN70.sbm_Entry"
  157. If (Err <> 0) Then MsgBox "Command not available."
  158. On Error GoTo 0
  159. ' Update the form.
  160. Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
  161. Me.Caption = Nz(Me![ItemText], "")
  162. FillOptions
  163.  
  164. ' Exit the application.
  165. Case conCmdExitApplication
  166. CloseCurrentDatabase
  167.  
  168. ' Run a macro.
  169. Case conCmdRunMacro
  170. DoCmd.RunMacro rst![Argument]
  171.  
  172. ' Run code.
  173. Case conCmdRunCode
  174. Application.Run rst![Argument]
  175.  
  176. ' Any other command is unrecognized.
  177. Case Else
  178. MsgBox "Unknown option."
  179.  
  180. End Select
  181.  
  182. ' Close the recordset and the database.
  183. rst.Close
  184. dbs.Close
  185.  
  186. HandleButtonClick_Exit:
  187. Exit Function
  188.  
  189. HandleButtonClick_Err:
  190. ' If the action was cancelled by the user for
  191. ' some reason, don't display an error message.
  192. ' Instead, resume on the next line.
  193. If (Err = conErrDoCmdCancelled) Then
  194. Resume Next
  195. Else
  196. MsgBox "There was an error executing the command.", vbCritical
  197. Resume HandleButtonClick_Exit
  198. End If
  199.  
  200. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement