請耐心等待我是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