2013-01-15 45 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