以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  关于多字段查询  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=104911)

--  作者:peibaomin
--  发布时间:2017/8/8 13:26:00
--  关于多字段查询
当多字段所储存的数据都一样时(如“电话一”、"电话二"、“电话三”、"固定电话"字段)查询查询时,怎样实现在窗体中有一个文本框输入查询条件,来同时对四个字段或更多的字段进行精确查询和模糊查询?
--  作者:有点甜
--  发布时间:2017/8/8 14:21:00
--  

参考代码

 

"电话1 = \'123\' or 电话2 = \'123\' or 电话3 = \'123\'"

 

"电话1 like \'%123%\' or 电话2 like \'%123%\' or 电话3 like \'%123%\'"


--  作者:peibaomin
--  发布时间:2017/8/8 14:50:00
--  

Dim tbx As WinForm.TextBox=e.Form.controls("anshoujihaoma")
Dim mh As WinForm.CheckBox=e.Form.Controls("shoujimohuchazhao")
Dim str As String=tbx.value
If str =  Nothing Then
    MessageBox.Show("手机号不为空!", "提示")
    tbx.Select
    Return
End If
Dim filter As String = ""

If mh.Checked Then
    filter="客户主手机号 like \'% str % \' Or 第二手机号 like \'% str %\' Or  第三手机号 Like \'% str %\'"  \'模糊查找      
Else
    filter="客户主手机号 = \'str\' Or 第二手机号= \'str \' Or 第三手机号 = \'str \'" \'精确查找
 End If
Tables("客户资料").filter = filter

 

 

运行时如果是精确查找明明有数据单击查找后结果是无数据;如果模糊查找将弹出一个错误对话框:

 


图片点击可在新窗口打开查看此主题相关图片如下:123.png
图片点击可在新窗口打开查看

--  作者:有点甜
--  发布时间:2017/8/8 14:53:00
--  
If mh.Checked Then
    filter="客户主手机号 like \'%" & str & "% \' Or 第二手机号 like \'%" & str & "%\' Or  第三手机号 Like \'%" & str & "%\'"  \'模糊查找      
Else
    filter="客户主手机号 = \'" & str & "\' Or 第二手机号= \'" & str & "\' Or 第三手机号 = \'" & str & "\'" \'精确查找
 End If

--  作者:peibaomin
--  发布时间:2017/8/8 15:02:00
--  

模糊查找还是错误

 


图片点击可在新窗口打开查看此主题相关图片如下:1234.png
图片点击可在新窗口打开查看

--  作者:有点甜
--  发布时间:2017/8/8 15:03:00
--  
If mh.Checked Then
    filter="客户主手机号 like \'%" & str & "%\' Or 第二手机号 like \'%" & str & "%\' Or  第三手机号 Like \'%" & str & "%\'"  \'模糊查找      
Else
    filter="客户主手机号 = \'" & str & "\' Or 第二手机号= \'" & str & "\' Or 第三手机号 = \'" & str & "\'" \'精确查找
 End If

--  作者:peibaomin
--  发布时间:2017/8/9 9:00:00
--  

 

[此贴子已经被作者于2017/8/9 10:38:00编辑过]

--  作者:peibaomin
--  发布时间:2017/8/9 10:38:00
--  

Dim Filter As String
Dim shmcmhcz As WinForm.CheckBox = e.Form.Controls("shanghumohuchazhaoCheckBox")
With e.Form.Controls("shanghumingchengTextBox")
    If .Value IsNot Nothing Then
        If shmcmhcz.Checked Then
            Filter = "商户名称 like \'%" & .Value  & "%\'"
        Else
            Filter = "商户名称= \'" & .Value & "\'"
        End If
    End If
End With

Dim frxmmhcz As WinForm.CheckBox = e.Form.Controls("farenmohuchazhaoCheckBox")
With e.Form.Controls("farenxingmingTextBox")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        If frxmmhcz.Checked Then
            Filter = "法人姓名 like \'%" & .Value  & "%\'"
        Else
            Filter = "法人姓名= \'" & .Value & "\'"
        End If
    End If
End With

Dim jjtmmhcz As WinForm.CheckBox = e.Form.Controls("jijumohuchazhaoCheckBox")
With e.Form.Controls("jijutiaomaTextBox")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        If jjtmmhcz.Checked Then
            Filter = "机具条码 like \'%" & .Value  & "%\'"
        Else
            Filter = "机具条码= \'" & .Value & "\'"
        End If
    End If
End With
Dim llkmhcz As WinForm.CheckBox = e.Form.Controls("liuliangkamohuchazhaoCheckBox")
With e.Form.Controls("liuliangkahaoTextBox")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        If llkmhcz.Checked Then
            Filter = "流量卡号 like \'%" & .Value  & "%\'"
        Else
            Filter = "流量卡号= \'" & .Value & "\'"
        End If
    End If
End With
With e.Form.Controls("tongdaomingchengComboBox")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        Filter = "通道名称= \'" & .Value & "\'"
    End If
End With
Dim shbhmhcz As WinForm.CheckBox = e.Form.Controls("shanghubhmohuchazhaoCheckBox")
With e.Form.Controls("shanghubianhaoTextBox")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        If shbhmhcz.Checked Then
            Filter = "商户编号 like \'%" & .Value  & "%\'"
        Else
            Filter = "商户编号= \'" & .Value & "\'"
        End If
    End If
End With
Dim wdbhmhcz As WinForm.CheckBox = e.Form.Controls("wangdianbhmohuchazhaoCheckBox")
With e.Form.Controls("wangdianbianhaoTextBox")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        If wdbhmhcz.Checked Then
            Filter = "网点编号 like \'%" & .Value  & "%\'"
        Else
            Filter = "网点编号= \'" & .Value & "\'"
        End If
    End If
End With
Dim wdmcmhcz As WinForm.CheckBox = e.Form.Controls("wangdianmcmohuchazhaoCheckBox")
With e.Form.Controls("wangdianmingchengTextBox")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        If wdmcmhcz.Checked Then
            Filter = "网点名称 like \'%" & .Value  & "%\'"
        Else
            Filter = "网点名称= \'" & .Value & "\'"
        End If
    End If
End With
Dim wddzmhcz As WinForm.CheckBox = e.Form.Controls("wangdiandzmohuchazhaoCheckBox")
With e.Form.Controls("wangdiandizhiTextBox")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        If wddzmhcz.Checked Then
            Filter = "网点地址 like \'%" & .Value  & "%\'"
        Else
            Filter = "网点地址= \'" & .Value & "\'"
        End If
    End If
End With
With e.Form.Controls("shanghuzhuangtaiComboBox")
    If .Value IsNot Nothing Then
        If Filter > "" Then
            Filter = Filter & " And "
        End If
        Filter = "商户状态= \'" & .Value & "\'"
    End If
End With
If Filter > "" Then
    Tables("商户资料表").Filter = Filter
End If
Forms("商户资料查询窗体").Close

 

老师这段代码那错了,怎么运行后查询的结果和实际不一样呀?

单项查可以,联合查就把所有的数据都查出来了


--  作者:有点甜
--  发布时间:2017/8/9 11:12:00
--  

msgbox(filter) 看看,弹出什么?

 

截图,或者做例子说明,如何不正确。


--  作者:裴保民
--  发布时间:2017/8/9 11:19:00
--  
如果查询条件是两项或以上模糊查询会把所有的数据全查出来了