请耐心等待我是VBA的完整noob,我无意编写代码,但无论如何我最终还是这样做了,因为我看不到如何实现没有它,我正在寻找。我一直在互联网上搜索3天,每天10个小时,但没有多少运气。我有一个我想要保护的Access数据库。只是让我解释我想要实现的第一件事:VBA和Access 2007数据验证,登录问题
1)我有一个表单查找表中的值,一个项目的项目代码的组合框。这基本上是一个库存的事情。我有一个查询有一个计算字段,它返回每个项目的剩余库存。我有另一个查询,它将项目代码作为参数,并返回该特定项目代码的剩余库存。
我想要做的是检查剩余库存是否为0,如果是,则从组合框列表中选择此物料代码,然后有一个msgbox显示一条消息,指出该物料没有库存,因此它不能发布,并重置组合框(即如果没有选择)
这是我的代码; combobox的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
Help Please? :S
加油吧? – AsjadAmin