Access2010如何在菜单栏中添加自定义的菜单和子菜单

问题已开启 普通问题 提问者:beichen|提问时间:2013-01-22
怎么才能在菜单栏的加载项中添加你的菜单和子菜单呢,也就是ACCESS中的查询都在自定义的菜单栏中用下拉菜单显示出来呢?
下面这些命令都复制在模块中了,要用什么命令才能把菜单和子菜单自动载入到菜单中呢--如附件图?谢谢!

复制来的代码:

Option Compare Database
Sub 设置工具栏()
Dim newBar As CommandBar
Dim newButton As CommandBarButton
On Error Resume Next
Set newBar = Application.CommandBars.Add("xtgj", msoBarTop)
If Err.Number > 0 Then
Application.CommandBars("xtgj").Delete '删除
Set newBar = Application.CommandBars.Add("xtgj", msoBarTop)
End If
newBar.Visible = True
Set db = CurrentDb
sql1 = "SELECT  * FROM 工具栏  ORDER BY 序号 "
Set rs = db.OpenRecordset(sql1, 2, 512)
If Not rs.EOF Then rs.MoveFirst
While Not rs.EOF
If rs("类型") = "命令按钮" Then
  Set newButton = newBar.Controls.Add(msoControlButton, rs("ID"))
Else
  Set newButton = newBar.Controls.Add(msoControlComboBox, rs("ID"))
End If
If Not rs.EOF Then rs.MoveNext
Wend
rs.Close
Set db = Nothing
Set rs = Nothing
End Sub
Public Sub 动态菜单()
Const bm As String = "主菜单"
Const bm1 As String = "子菜单"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Dim sql1 As String
Dim bar As CommandBar
Dim mybar As CommandBarControl
On Error Resume Next
Set bar = Application.CommandBars.Add("xtcd", msoBarTop, True, True)
If Err.Number > 0 Then
Application.CommandBars("xtcd").Delete
Set bar = Application.CommandBars.Add("xtcd", msoBarTop, True, True)
rr.Number = 0
End If
With bar
.Protection = msoBarNoMove
.Visible = True
End With
Set db = CurrentDb
sql1 = "SELECT  * FROM " & bm & " ORDER BY ID DESC"
Set rs = db.OpenRecordset(sql1)
If Not rs.EOF Then rs.MoveFirst
While Not rs.EOF
Set mybar = bar.Controls.Add(Type:=msoControlPopup, Before:=1)
With mybar
.Caption = rs("主菜单")
End With
sql1 = "SELECT  * FROM " & bm1 & " where ID=" & rs("ID") & " ORDER BY ZID DESC"
Set rs1 = db.OpenRecordset(sql1)
If Not rs1.EOF Then rs1.MoveFirst
While Not rs1.EOF
  Set mybar1 = mybar.Controls.Add(Type:=msoControlButton, Before:=1)
  With mybar1
   .Caption = rs1("子菜单")
   .Visible = True
   .OnAction = "菜单接口"   '将激活的过程名称
   .Tag = rs1("ZID")
  End With
  If Not rs1.EOF Then rs1.MoveNext
Wend
rs1.Close
If Not rs.EOF Then rs.MoveNext
Wend
rs.Close
Set db = Nothing
Set rs = Nothing
Set rs1 = Nothing
End Sub
Sub 菜单接口()
Const bm1 As String = "子菜单"
Dim db As DAO.Database
Dim rs As DAO.Recordset
On Error GoTo err1  '如果有错误,转错误处理
Set db = CurrentDb
sql1 = "SELECT  * FROM " & bm1 & " where ZID=" & CommandBars.ActionControl.Tag
Set rs = db.OpenRecordset(sql1)
Select Case rs("打开类别")  '根据所选的“打开类别”进行相应的操作
Case 1  '类别为窗体
DoCmd.OpenForm rs("名称"), rs("视图类型")  '根据视图类型打开窗体
Case 2  '类别为查询
If rs("视图类型") > 2 Then  ''视图类型不符合要求时,自动按打印预览视图处理
  DoCmd.OpenQuery rs("名称"), 2
Else
  DoCmd.OpenQuery rs("名称"), rs("视图类型")  '根据视图类型打开查询
End If
Case 3  '类别为报表
If rs("视图类型") > 2 And rs("视图类型") < 5 Then  '视图类型不符合要求时,自动按打印预览视图处理
  DoCmd.OpenReport rs("名称"), 2
Else
  DoCmd.OpenReport rs("名称"), rs("视图类型") '根据视图类型打开报表
End If
Case 4  '类别为表
DoCmd.OpenTable rs("名称"), rs("视图类型"), acReadOnly  '根据视图类型以只读方式打开表
Case 5  '类别为代码
DoCmd.RunMacro rs("名称")
Case 6  '类别为话统
dsp_result (rs("名称"))
Case 7
Call input_sta
Case 8  '类别为代码
Call 动态菜单
Case Else  '错误的打开类别处理
MsgBox ("打开类别输入错误")
  
End Select
Exit Sub  '退出
err1:  '错误处理
MsgBox ("还没有定义这个功能," & "不能打开这个“" & rs("名称") & "”")
End Sub
Sub 删除菜单()
On Error GoTo err1
Application.CommandBars("xtcd").Delete
err1:
End Sub
Sub 菜单有效设置(gn) '所带参数(gn=(1--菜单有效,其他--无效))
On Error GoTo err1
If gn = 1 Then
Application.CommandBars("xtcd").Enabled = True '有效
Else
Application.CommandBars("xtcd").Enabled = False '无效
End If
err1:
End Sub
Sub 菜单可见设置(gn) '所带参数(gn=(1--菜单可见,其他--不可见))
On Error GoTo err1
If gn = 1 Then
Application.CommandBars("xtcd").Visible = True '可见
Else
Application.CommandBars("xtcd").Visible = False '不可见
End If
err1:
End Sub
Sub 主菜单有效设置(zxh, gn) '所带参数(zxh=主菜单序号,gn=(1--菜单有效,其他--无效))
Dim js
On Error GoTo err1
js = Application.CommandBars("xtcd").Controls.Count
If zxh > js And zxh < js Then
MsgBox ("参数错误,主菜单序号不能大于或小于主菜单表中的记录数")
Exit Sub
End If
If gn = 1 Then
Application.CommandBars("xtcd").Controls(zxh).Enabled = True '有效
Else
Application.CommandBars("xtcd").Controls(zxh).Enabled = False '无效
End If
err1:
End Sub
Sub 主菜单可见设置(zxh, gn) '所带参数(zxh=主菜单序号,gn=(1--菜单可见,其他--隐藏))
Dim js
On Error GoTo err1
js = Application.CommandBars("xtcd").Controls.Count
If zxh > js And zxh < js Then
MsgBox ("参数错误,主菜单序号不能大于或小于主菜单表中的记录数")
Exit Sub
End If
If gn = 1 Then
Application.CommandBars("xtcd").Controls(zxh).Visible = True  '可见
Else
Application.CommandBars("xtcd").Controls(zxh).Visible = False '隐藏
End If
err1:
End Sub
Sub 子菜单有效设置(zxh, gn) '所带参数(zxh=主菜单序号,gn=(1--菜单有效,其他--无效))
Dim i As Integer
Dim js
On Error GoTo err1
js = Application.CommandBars("xtcd").Controls.Count
If zxh > js And zxh < js Then
MsgBox ("参数错误,主菜单序号不能大于或小于主菜单表中的记录数")
Exit Sub
End If
For i = 1 To Application.CommandBars("xtcd").Controls(zxh).Controls.Count
If gn = 1 Then
  Application.CommandBars("xtcd").Controls(zxh).CommandBar.Controls(i).Enabled = True '有效
Else
  Application.CommandBars("xtcd").Controls(zxh).CommandBar.Controls(i).Enabled = False '无效
End If
Next i
err1:
End Sub
Sub 子菜单可见设置(zxh, gn) '所带参数(zxh=主菜单序号,gn=(1--菜单有效,其他--无效))
Dim i As Integer
Dim js
On Error GoTo err1
js = Application.CommandBars("xtcd").Controls.Count
If zxh > js And zxh < js Then
MsgBox ("参数错误,主菜单序号不能大于或小于主菜单表中的记录数")
Exit Sub
End If
For i = 1 To Application.CommandBars("xtcd").Controls(zxh).Controls.Count
If gn = 1 Then
  Application.CommandBars("xtcd").Controls(zxh).CommandBar.Controls(i).Visible = True '可见
Else
  Application.CommandBars("xtcd").Controls(zxh).CommandBar.Controls(i).Visible = False '隐藏
End If
Next i
err1:
End Sub
Sub 单个子菜单有效设置(zxh, z_xh, gn) '所带参数(zxh=主菜单序号,z_xh=子菜单序号,gn=(1--菜单有效,其他--无效))
Dim js
On Error GoTo err1
js = Application.CommandBars("xtcd").Controls.Count
If zxh > js And zxh < js Then
MsgBox ("参数错误,主菜单序号不能大于或小于主菜单表中的记录数")
Exit Sub
End If
js = Application.CommandBars("xtcd").Controls(zxh).Controls.Count
If zxh > js And zxh < js Then
MsgBox ("参数错误,子菜单序号不能大于或小于该主菜单下的子菜单")
Exit Sub
End If
If gn = 1 Then
Application.CommandBars("xtcd").Controls(zxh).CommandBar.Controls(z_xh).Enabled = True '有效
Else
Application.CommandBars("xtcd").Controls(zxh).CommandBar.Controls(z_xh).Enabled = False '无效
End If
err1:
End Sub

Sub 单子菜单可见设置(zxh, z_xh, gn) '所带参数(zxh=主菜单序号,z_xh=子菜单序号,gn=(1--菜单可见,其他--隐藏))
Dim js
On Error GoTo err1
js = Application.CommandBars("xtcd").Controls.Count
If zxh > js And zxh < js Then
MsgBox ("参数错误,主菜单序号不能大于或小于主菜单表中的记录数")
Exit Sub
End If
js = Application.CommandBars("xtcd").Controls(zxh).Controls.Count
If zxh > js And zxh < js Then
MsgBox ("参数错误,子菜单序号不能大于或小于该主菜单下的子菜单")
Exit Sub
End If
If gn = 1 Then
Application.CommandBars("xtcd").Controls(zxh).CommandBar.Controls(z_xh).Visible = True '可见
Else
Application.CommandBars("xtcd").Controls(zxh).CommandBar.Controls(z_xh).Visible = False '隐藏
End If
err1:
End Sub


我要回答

请先 登录 或 注册 再回答问题

问题答案 (0)

这个问题还没有回答,期待你的解答。

最新紧急招聘通信职位

杭州东信网络技术有限公司 聘:云南华为项目中级工程师
需求人数:1 人 地点:迪庆藏族自治州
南京华苏科技有限公司 聘:4/5G系统分析高级高级工程师
需求人数:4 人 地点:郑州市
成都旗讯通信技术有限公司 聘:重庆/西安/渭南招室分测试优化
需求人数:9 人 地点:重庆市,西安市,渭南市,宝鸡市,咸阳市
山东省邮电工程有限公司 聘:竣工资料结算员
需求人数:5 人 地点:福州市
郑州旭端信息科技有限公司 聘:AI大数据工程师
需求人数:20 人 地点:北京市,河南省,广东省,上海市,浙江省
成都睿晟嘉好劳务有限公司 聘:诺基亚后台网优高级工程师
需求人数:3 人 地点:泸州市,宜宾市
珠海世纪鼎利科技股份有限公司 聘:浙江45G室分测试人员 (外场)
需求人数:5 人 地点:温州市,台州市,衢州市,丽水市,金华市
杭州华星创业通信技术股份有限公司 聘:中兴中级后台-温州
需求人数:1 人 地点:温州市
嘉环科技股份有限公司 聘:网优高级工程师
需求人数:2 人 地点:长沙市

热点问题

联系我们 - 问通信专家 Powered by MSCBSC 移动通信网  © 2006 -