请耐心等待,我是 VBA 的菜鸟,我无意编写代码,但我最终还是这样做了,因为我无法看到没有它我将如何实现我想要的东西。我一直在互联网上搜索 3 天,每天 10 小时,但运气不佳。我有一个要保护的 Access 数据库。让我解释一下我首先要实现的目标:
1)我有一个表单中的组合框,可以在表格中查找值,即项目的项目代码。这基本上是一个库存的事情。我有一个查询,其中有一个计算字段,该字段返回每个项目的剩余库存。我有另一个查询,它将项目代码作为参数并返回该特定项目代码的剩余库存。
我想要做的是检查剩余库存是否为 0,如果是,则在从组合框列表中选择此项目代码时,让 msgbox 显示一条消息,指出该项目没有库存,因此无法发出,并重置组合框(即好像没有进行选择)
这是我的代码;组合框的 afterUpdate 事件:
Private Sub Item_Code_AfterUpdate()
Dim dbMyDatabase As DAO.Database
Dim rsMyRecords As DAO.Recordset
Dim strQuery As String
strQuery = "SELECT [Store_Items].[Item Code], Store_Items.[Opening Stock], IIf((Nz([Opening Stock],0)+Nz([Quantity Purchased],0)-Nz([Quantity Issued],0)<0),0,(Nz([Opening Stock],0)+Nz([Quantity Purchased],0)-Nz([Quantity Issued],0))) AS [Remaining Stock] FROM (Purchases RIGHT JOIN Store_Items ON Purchases.[Item Code] = Store_Items.[Item Code]) LEFT JOIN Issuances ON Store_Items.[Item Code] = Issuances.[Item Code] WHERE (((Store_Items.[Item Code])=[Forms]![Issuances]![Item Code]));"
Set dbMyDatabase = CurrentDb
Set rsMyRecords = dbMyDatabase.OpenRecordset(strQuery)
Dim Msg, Style, Title, Response
Msg = "This Item is out of Stock! You Cannot Issue this item!"
Style = vbOK
Title = "Warning!"
If rsMyRecords![Item Code] <= 0 Then
Response = MsgBox(Msg, Style, Title)
If Response = vbOK Then Me![Item Code].Requery
End Sub
2) 我制作了 2 个版本的导航面板。其中一位可以访问所有表格,而另一位则具有有限的访问权限。这由登录类型决定。现在,唯一有效的是第一个,我不明白为什么 ElseIfs 不起作用。当我尝试使用其他类型登录时,单击登录按钮,没有任何反应。
我还想隐藏导航窗格并访问允许数据库设计编辑以及根据登录类型在设计视图中查看表和表单的默认菜单,但我不知道如何实现这一点。
Private Sub LoginBUtton_Click()
'Check to see if data is entered into the UserName combo box
If IsNull(Me.CBOLogin) Or Me.CBOLogin = "" Then
MsgBox "You must select a user type.", vbOKOnly, "No User name"
Me.CBOLogin.SetFocus
Exit Sub
End If
'Check to see if data is entered into the password box
If IsNull(Me.TextPass) Or Me.TextPass = "" Then
MsgBox "No Password Entered, Enter a password.", vbOKOnly, "No Password"
Me.TextPass.SetFocus
Exit Sub
End If
'Check value of password in Users to see if this
'matches value chosen in combo box
If Me.TextPass.Value = DLookup("Password", "Users", _
"[UserID]=" & Me.CBOLogin.Value) Then
ElseIf (Me.CBOLogin.Value = "Developer") Then
DoCmd.Close acForm, "LoginForm", acSaveNo
DoCmd.OpenForm "FullAccessNav"
ElseIf (Me.CBOLogin.Value = "Office") Then
DoCmd.Close acForm, "LoginForm", acSaveNo
DoCmd.OpenForm "LimitedAccessNav"
ElseIf (Me.CBOLogin.Value = "Store") Then
DoCmd.Close acForm, "loginForm", acSaveNo
DoCmd.OpenForm "LimitedAccessNav"
Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, _
"Invalid Entry!"
Me.TextPass.SetFocus
End If
'If User Enters incorrect password 3 times database will shutdown
intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database.Please contact admin.", _
vbCritical, "Restricted Access!"
Application.Quit
End If
End Sub
请帮忙?:S