过滤特定列的数据

时间:2014-05-13 20:46:32

标签: ms-access access-vba filtering

Hello Everyone我处于两难境地我试图找到一种方法来过滤掉列字段中的某些数据我知道如何做到这一点但我不知道要使用的正确语法。首先是表格,这里是我想写的代码结构。

for i=1 to length of column First Match
    for j=1 to length of column Second Match

    If ((value of the data in column First Match = 15) OR (value of the data in column FirstMatch = 1)) AND
       ((value of the data in column Second Match = 15) OR (value of the data in column Second Match = 1)) 
    Then
        Filter the data and append so the filtered datas are saved for both First Match and Second Match
    end if
    next
next

我正在尝试过滤掉15和1的数据,以便只显示值为0,2,3,4,5,6 ... 14的数据,例如john和steve将不会显示,因为第一个和第二个匹配字段都有1或15但其余数据将显示我的表单是拆分表单设置。

我的方法是否正确?


First Name    Last Name    First Match    Second Match
James         Matheson         0               2
Monroe        Labonson         4               3
Barack        Obama            2               5
Frederick     Douglas          3               4
Steve         MCGowan          1               1
John          Seals            15              15
Mike          Omalley          14              15



Set rs = CurrentDb.OpenRecordset("Table1")

Do While Not rs.EOF
If rs!Fields("First Match") > 1 And rs!Fields("First Match") < 15 And rs!Fields("Second Match") > 1 And rs!Fields("Second Match") < 15 Then

End If
Loop

2 个答案:

答案 0 :(得分:0)

通过更好的理解(我希望),我已经在Access中重现了您的数据,并构建了一个执行您似乎要求的查询。查看此内容的最佳方法是创建新查询,而不是添加表,然后直接转到SQL视图。粘贴以下SQL语句(引号中的语句)并将matchFilter替换为您的表名。

在您的事件代码中,您可以根据SQL创建记录集:

Dim sSQL as string, rs as Recordset

sSQL = "SELECT matchFilter.[First Name], matchFilter.[Last Name], matchFilter.[First Match], matchFilter.[Second Match] " & _
"FROM matchFilter " & _
"WHERE ((([First Match]<>1 And [First Match]<>15 And [Second Match]<>1 And [Second Match]<>15)=True))"
 Set rs = CurrentDB.OpenRecordset(sSQL)
 ' now do what you want

下面的答案错误!

Dim rs as Recordset
Set rs = CurrentDB.OpenRecordset("yourTableName")
Do While Not rs.EOF
    If rs!Fields("col1") > 1 AND rs!Fields("col1") < 15 AND rs!Fields("col2") > 1 AND rs!Fields("col2") Then
        'do what you have to do with filtered out records
    End If
Loop

答案 1 :(得分:0)

我发现解决方案显然只是一个误解,因为我第一次尝试这段代码时我认为这是错误的,但这是因为我的字段名称在我第一次编写它们之间没有空格。

Me.Filter = ""
Me.Filter = "[First Nam]<>'Jamie' AND [Last Nam]<>'Cartman'"
Me.FilterOn = True