Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

如何用组合框进行模糊查询?

殷小宝  发表于:2010-11-16 02:40:52  
复制

比如:我在组合框中输入“厂”字,子窗体与“厂”有关的记录全部显示出来。怎么能够实现呢?

 

Top
netguestcn 发表于:2010-11-16 05:10:52
Private Sub Command2_Click()
On Error GoTo Err_Command2_Click
    Dim strWhere As String
    strWhere = ""
    If Not IsNull(Me.工厂) Then
    strWhere = strWhere & "([工厂] like '*" & Me.工厂 & "*') AND "
    End If
   
    If Len(strWhere) > 0 Then
        strWhere = Left(strWhere, Len(strWhere) - 5)
    End If
    Me.子窗体.Form.Filter = strWhere
    Me.子窗体.Form.FilterOn = True
Exit_Command2_Click:
    Exit Sub
Err_Command2_Click:
    MsgBox Err.Description
    Resume Exit_Command2_Click
End Sub

netguestcn 发表于:2010-11-16 05:16:12

上面的为按钮单击事件

如需在组合框中输入“厂”字,子窗体与“厂”有关的记录全部显示出来,则在组合框更新后事件中加入如下代码:

    Dim strWhere As String
    strWhere = ""
    If Not IsNull(Me.工厂) Then
    strWhere = strWhere & "([工厂] like '*" & Me.工厂 & "*') AND "
    End If
   
    If Len(strWhere) > 0 Then
        strWhere = Left(strWhere, Len(strWhere) - 5)
    End If
    Me.子窗体.Form.Filter = strWhere
    Me.子窗体.Form.FilterOn = True



殷小宝  发表于:2010-11-16 16:54:04
netguestcn 老师:这句代码 strWhere = strWhere & "([工厂] like '*" & Me.工厂 & "*') AND " 中的" AND"是什么意思? strWhere = Left(strWhere, Len(strWhere) - 5)这句是不是代表"Me.工厂",我怎么用你代码用不起来呢?烦请老师再细答.拜托了.



煮江品茶 发表于:2010-11-16 19:11:48

这个AND没什么意义,因为在后面一句又把它删除了。因此正确的写法应该为:

Dim strWhere As String
    strWhere = "True"
    If IsNull(Me.工厂)=False Then
        strWhere = strWhere & " AND ([工厂] like '*" & Me.工厂 & "*')"
    End If
    Me.子窗体.Form.Filter = strWhere
    Me.子窗体.Form.FilterOn = True



殷小宝  发表于:2010-11-16 23:20:15

煮江品茶老师:难道只使用过滤器筛选就行了,不需要Requery了吗?上段代码我用了一下,不行。其它文本框都行,就是组合框不行。我按您的方法用:

Private Sub 关键字4_AfterUpdate()
Dim strWhere As String
    strWhere = "True"
    If IsNull(Me.关键字4) = False Then
        strWhere = strWhere & " AND ([关键字4] like '*" & Me.关键字4 & "*')"
    End If
    Me.子窗体.Form.Filter = strWhere
    Me.子窗体.Form.FilterOn = True
End Sub

如图所示:

 



殷小宝  发表于:2010-11-16 23:22:01
上面的子窗体为“关键字查询3”我写错了,改过来了还是这样。

殷小宝  发表于:2010-11-16 23:23:36
总记录:7篇  页次:1/1 9 1 :