问题已开启
(普通问题)
Access2010如何在菜单栏中添加自定义的菜单和子菜单
怎么才能在菜单栏的加载项中添加你的菜单和子菜单呢,也就是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
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
提问者: beichen 提问时间: 2013-01-22
• 切换成功之后出现TAU suCCEsss是什么意思? 2018-11-26
• SCTP_MGW CoproCESsor falled to allocate memory 2018-02-28
• CMServicerequest后并未出现CMserviceaCCEpt而是直接接收到Systeminformationtype1导致起呼失 2017-04-27
• detach aCCEpt后,华为mifi多长时间进入no device mESSage?还是什么条件下 2016-03-15
• 您好!大神我想请问下。ACCEss VLAN模式是什么模式啊?和普通VLAN模式有什么区别啊。谢谢!还有就是可不可以留个联系方式我加个好友好交流下。谢谢了!! 2016-01-20
• 非周期上报中的{CSIproCESs,CSIsubframeset}-pair(s)的理解 2015-10-27
• 关于CSIproCESs 2015-10-21
• LTE下载FTPdownloadsuCCEss后出现Handoverfailure是什么原因? 2015-09-09
• SCTP_MGW CoproCESsor falled to allocate memory 2018-02-28
• CMServicerequest后并未出现CMserviceaCCEpt而是直接接收到Systeminformationtype1导致起呼失 2017-04-27
• detach aCCEpt后,华为mifi多长时间进入no device mESSage?还是什么条件下 2016-03-15
• 您好!大神我想请问下。ACCEss VLAN模式是什么模式啊?和普通VLAN模式有什么区别啊。谢谢!还有就是可不可以留个联系方式我加个好友好交流下。谢谢了!! 2016-01-20
• 非周期上报中的{CSIproCESs,CSIsubframeset}-pair(s)的理解 2015-10-27
• 关于CSIproCESs 2015-10-21
• LTE下载FTPdownloadsuCCEss后出现Handoverfailure是什么原因? 2015-09-09
问题答案
( 0 )
联系我们 - 问通信专家 | Powered by MSCBSC 移动通信网 © 2006 - |