設為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊

QQ登錄

只需一步,快速開始

access的一個如何建工具欄(toolbar)的例子,中文注解。

2002-5-8 02:08| 發(fā)布者: admin| 查看: 827| 評論: 27|原作者: MsAccess|來自: m.mzhfr.cn

摘要: 文件下載
'****************************************************************
' 本過程建立一新工具欄
'****************************************************************
Sub AddNewCB()
   Dim CBar As CommandBar, CBarCtl As CommandBarControl
   On Error GoTo AddNewCB_Err

   ' 新建一浮動工具欄,并且可見.
   Set CBar = CommandBars.Add(Name:="Sample Toolbar", Position:= _
      msoBarFloating)
   CBar.Visible = True

   ' 建立一具有文字的按鈕并設置其屬性.
   Set CBarCtl = CBar.Controls.Add(Type:=msoControlButton)
   With CBarCtl
      .Caption = "按鈕"
      .Style = msoButtonCaption
      .TooltipText = "按鈕顯示信息框"
      .OnAction = "=MsgBox(""你按了工具欄一按鈕!"")"
   End With

   ' 建立一具有圖標的按鈕并設置其屬性.
   Set CBarCtl = CBar.Controls.Add(Type:=msoControlButton)
   With CBarCtl
      .FaceId = 1000
      .Caption = "切換按鈕"
      .TooltipText = "切換第一個按鈕(可見/隱藏)"
      .OnAction = "=ToggleButton()"
   End With

   ' 建立組合框控件并設置相應屬性.
   Set CBarCtl = CBar.Controls.Add(msoControlComboBox)
   With CBarCtl
      .Caption = "下拉菜單"
      .Width = 100
      .AddItem "新建按鈕", 1
      .AddItem "移去按鈕", 2
      .DropDownWidth = 100
      .OnAction = "=AddRemoveButton()"
   End With
   Exit Sub
   
AddNewCB_Err:
   If Err.Number = 5 Then  '存在則刪除
      For Each CBar In Application.CommandBars
         If CBar.Name = "Sample Toolbar" Then
           CBar.Delete
           Resume
         End If
      Next
      Exit Sub
   End If
   MsgBox "Error " & Err.Number & vbCr & Err.Description
   Exit Sub
End Sub

'****************************************************************
' 本過程為工具欄上的按鈕調用.
' 它使另一按鈕在可見和隱藏間切換.
'****************************************************************
Function ToggleButton()
   Dim CBButton As CommandBarControl
   On Error GoTo ToggleButton_Err
   Set CBButton = CommandBars("Sample Toolbar").Controls(1)
   CBButton.Visible = Not CBButton.Visible
   Exit Function
   
ToggleButton_Err:
   MsgBox "Error " & Err.Number & vbCr & Err.Description
   Exit Function
End Function

'****************************************************************
'本過程為工具欄上的組合框所調用
'它用于添加和刪除按鈕
'****************************************************************
Function AddRemoveButton()
   Dim CBar As CommandBar, CBCombo As CommandBarComboBox
   Dim CBNewButton As CommandBarButton
   On Error GoTo AddRemoveButton_Err
   Set CBar = CommandBars("Sample Toolbar")
   Set CBCombo = CBar.Controls(3)
   Select Case CBCombo.ListIndex
      '如果按了新建按鈕, 則建立一按鈕
      Case 1
         Set CBNewButton = CBar.Controls.Add(Type:=msoControlButton)
         With CBNewButton
            .Caption = "新按鈕"
            .Style = msoButtonCaption
            .BeginGroup = True
            .Tag = "新建的按鈕"
            .OnAction = "=MsgBox(""這可是新鮮出爐的按鈕!"")"
         End With
      ' 移去 如果有新按鈕,找到并移去.
      Case 2
         Set CBNewButton = CBar.FindControl(Tag:="新建的按鈕")
         CBNewButton.Delete
      Case Else
      ' 如果用戶自行輸入(不在列表)
         MsgBox "你輸入的內容為:" & CBCombo.Text, vbInformation, "Ms Access"
   End Select
   Exit Function
   
AddRemoveButton_Err:
   ' 如果按鈕不存在.
   If Err.Number = 91 Then
      MsgBox "無法移去不存在的按鈕!"
      Exit Function
   Else
     MsgBox "錯誤 " & Err.Number & vbCr & Err.Description
     Exit Function
   End If
End Function



示例文件下載


發(fā)表評論

最新評論

引用 tmtony 2002-5-8 03:22
這是個很好例子,我給鞋廠的系統(tǒng)中已經(jīng)偷了它的部分源碼
引用 yuanhai 2002-5-29 00:16
引用 sampson 2002-7-23 23:35
運行時出錯呀
引用 tomzy 2002-7-25 16:58
運行出錯....
引用 MsAccess 2002-7-26 23:14
只不過由于2000同XP的版本不同罷了,看看哪些引用有問題,選擇類似的(版本)引用即可。
引用 2609526 2006-9-2 05:53
TKS TIGONG
引用 mqmelon 2006-10-7 18:27
謝謝啦
引用 shenlan 2007-1-7 09:06
需要研究一下,謝謝分享
引用 actthree 2007-1-31 19:50
好東西
引用 actthree 2007-1-31 19:50
謝謝樓主
引用 心如蒸餾水 2007-4-6 21:00
頂一個,好東西,謝啦
引用 ok003 2007-4-21 04:58
這么好的東東 怎么用呢。什么場合可用到這個?
引用 hgh1600 2007-5-3 07:01
不能用[em01]
引用 sblisb 2007-8-19 15:38
哪?沒看到下載東東的地址呀?
引用 hosam 2007-8-20 21:48
謝謝分享.。。。。!
引用 qsdys 2007-8-27 18:08
路過下來看看!謝謝分享!
引用 qsdys 2008-1-6 16:12
下來看看,謝謝分享
引用 whjtw 2008-1-9 18:34
下下來看看,謝謝分享
引用 tzt0625 2009-2-9 00:17
下載 來 看看 多謝。。

查看全部評論(27)

QQ|站長郵箱|小黑屋|手機版|Office中國/Access中國 ( 粵ICP備10043721號-1 )  

GMT+8, 2025-7-13 08:19 , Processed in 0.109813 second(s), 23 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回頂部