编程环境是win2000,vb6,access2000
有这四个vb中的变量:bhText , xmText ,dh1Text ,dh2Text 
代码如下:
if trim(bhText)="" then bhText="%"
if trim(xmText)="" then xmText="%"
if trim(dh1Text)="" then dh1Text="%"
if trim(dh2Text)="" then dh2Text="%"adodc1.recordsource="select * from clientinfo where clientid like '" & bhText & "' and clientname like '" & xmText & "' and phone like '" & dh1Text & "' and phone2 like '" & dh2Text & "'"但是,如果clientinfo中的某字段,比如phone2有一些记录是空串的,查询结果就会出错,搞了几天也搞不明白为什么,也想不到更好的办法
请各位高手帮帮忙吧,100分相送

解决方案 »

  1.   


    Access中的通配符用“*”号,虽然你的问题与此无关。
    if trim(bhText)="" then bhText="*"
    if trim(xmText)="" then xmText="*"
    if trim(dh1Text)="" then dh1Text="*"
    if trim(dh2Text)="" then dh2Text="*"
    如果字段中有空串的,也要选出来:
    adodc1.recordsource="select * from clientinfo where (clientid is null or clientid like '" & bhText & "') and (clientname is null or clientname like '" & xmText & "') and (phone is null or phone like '" & dh1Text & "') and (phone2 is null or phone2 like '" & dh2Text & "')"
      

  2.   

    试试这样行不行:dim strtj as stringif trim(bhText)>"" then strtj = "where clientid like " & trim(bhText)if trim(xmText)>"" then strtj = iif(strtj=""," where "," and ") & "clientname like " & trim(xmText)if trim(dh1Text)>"" then strtj = iif(strtj=""," where "," and ") & "phone like " & trim(dh1Text)if trim(dh2Text)>"" then strtj = iif(strtj=""," where "," and ") & "phone2 like '" & dh2Text & "'"adodc1.recordsource="select * from clientinfo" & strtj另外,可以预填一些信息,消除空串:cn.execute "update clientinfo set phone2 = 'N/A' where phone2 = "" or isnull(phone2)"
      

  3.   

    对了,access的统配符正如楼上所言是 "*",我也有过类似错误,费了好大劲才改过来。并不是因为空串的原因吧。难道有空串sql 语句就不能运行了吗?不和逻辑。
      

  4.   

    同意 of123()   这个办法肯定可以用 最好改为 if trim(dh2Text)<>"" then   是 〈〉 而 不是 > cn.execute "update clientinfo set phone2 = 'N/A' where phone2 = "" or isnull(phone2)"这句话改为cn.execute "update clientinfo set phone2 = 'N/A' where isnull(phone2,'')=''"
      

  5.   

    但个人建议不要有
    cn.execute "update clientinfo set phone2 = 'N/A' where isnull(phone2,'')=''"因为多加任何值都有可能会有冲突,所以建议在查询时多加过滤条件就行了~
      

  6.   

    sql = "select * from clientinfo where 1=1"
    If Trim(bhtext) <> "" Then sql = sql & " and clientid like '" & bhtext & "*'"
    If Trim(xmText) <> "" Then sql = sql & " and clientname like '" & Replace(xmText, "'", "''") & "*'"
    If Trim(dh1Text) <> "" Then sql = sql & " and phone like '" & dh1Text & "*'"
    If Trim(dh2Text) <> "" Then sql = sql & " and phone2 like '" & dh2Text & "*'"
    adodc1.RecordSource = sql