Access中的DoCMD.OpenForm - 使用WHERE打开具有有限记录集的表单

时间:2013-08-05 17:00:34

标签: sql ms-access-2007 access-vba

我在MS Access工作。我试图根据用户的登录信息(由某些vba代码确定)打开一个不向用户显示某些记录集的表单。

使用以下DoCmd.OpenForm,表单已打开,但我希望隐藏用户的记录集(其中G_Var = YES)仅被过滤掉。因此,用户可以只删除已过滤的数据表Form并查看行(我不想禁用过滤器)。我做错了什么?

DoCmd.OpenForm "Metrics", acFormDS, , "G_Var = 'NO'"  

我也在想也许我可以编写某种查询来检索这些信息。也许是这样的事情:

rs.Open "Select * FROM Metrics WHERE G_Var = 'NO';", CurrentProject.Connection

我只是不明白我将如何获取此查询信息并将其输出到表单中。通过定义记录源,我能够以这种方式完成任务:

Private Sub btnMetrics_From_Click()

  DoCmd.OpenForm "Metrics", acFormDS
  Forms.Item("Start Form").Visible = False

End Sub

Private Sub Form_Activate()
  Dim rs As New ADODB.Recordset
  Dim sql As String
  Dim Boo As Boolean

    g_User = UserInfo(ap_GetUserName())
    Boo = False

rs.Open "SELECT * FROM tbl_User_Info WHERE G_Var_Access = TRUE;", CurrentProject.Connection

Do Until rs.EOF = False
    If rs.Fields("User_Name").Value = g_User Then
        Boo = True

        Exit Do
    End If
Loop

rs.Close

If Boo = True Then

Else
    Me.RecordSource = "SELECT * FROM Metrics WHERE G_Var = 'NO';"
End If

    Me.Requery

End Sub

由于

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的主要RecordSource SQL ...

SELECT M.*
FROM Metrics AS M,tbl_User_Info AS U
WHERE U.User_Name=CurrentUser()
AND M.G_Var LIKE IIF(U.G_Var_Access=TRUE,'*','NO')