2013-01-15 78 views
0

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

回答

0

嗯,我設法解決登錄問題,但我仍然堅持數據驗證位。當我點擊組合框中列表中的項目代碼時,出現運行時錯誤3421。

這是我從組合框中選擇項目代碼時想要發生的情況。我想從該項目代碼的查詢中獲得計算字段(該計算字段具有計算的項目剩餘庫存),如果它爲0,那麼我想顯示一個msg並阻止用戶輸入此記錄,所以我想重置組合框會做什麼?

如果股票是在一定的水平,我只是想顯示一個msgbox和這一切,通常讓用戶輸入記錄,否則,用戶只是正常輸入記錄。

我在此行得到一個運行時錯誤3421 「數據類型轉換錯誤」:

集RS = db.OpenRecordset(「SELECT [剩餘庫存] FROM [InventoryStatusSpecificItem] WHERE [項目代碼] = [表格]![發行]![ItemCBO]「,動態集)

Private Sub ItemCBO_AfterUpdate() 

    Dim db As DAO.Database 
    Dim rs As DAO.Recordset 

    Set db = CurrentDb() 


    Set rs = db.OpenRecordset("SELECT [Remaining Stock] FROM [InventoryStatusSpecificItem] WHERE [Item Code] = [Forms]![Issuances]![ItemCBO]", Dynaset) 



    If rs.Fields([Remaining Stock]) = "0" Then 
    MsgBox "This item is out of stock! Cannot Issue!", vbExclamation + vbOKOnly, "Attention!", vbExclamation 
    Me!ItemCBO = Null 

    ElseIf rs.Fields("Stock Status") = "Order Now!" Then 
    MsgBox "This item has reached its minimum quantity level. Only " + StrRemainingStock + " remains. Quantity Measured as " + StrMeasuredIn, vbExclamation + vbOKOnly, "Attention!" 

    Else 
    Me!ItemCBO.Requery 

    End If 

    'close recordset 
    rs.Close 
    Set db = Nothing 
+0

加油吧? – AsjadAmin