注册 登录
编程论坛 SQL Server论坛

无法查询

xywnhy 发布于 2010-04-15 00:11, 690 次点击
Private Sub command1_click()
  condstr = ""
   If Trim(Text1.Text) <> "" Then
     If condstr = "" Then
       condstr = "xinghao  LIKE '" + Trim(Text1.Text) + "& '"
     End If
   End If
   
   If Trim(Text2.Text) <> "" Then
     If condstr = "" Then
       condstr = "gongneng LIKE '" + Trim(Text2.Text) + "& '"
     Else
       condstr = condstr + "and gongneng LIKE '" + Trim(Text2.Text) + "& '"
     End If
   End If
   
   
   If Trim(Text3.Text) <> "" Then
     If condstr = "" Then
       condstr = "fengzhuang LIKE '" + Trim(Text3.Text) + "& '"
     Else
       condstr = condstr + "and fengzhuang LIKE '" + Trim(Text3.Text) + "& '"
     End If
   End If
   
   If Trim(Text4.Text) <> "" Then
     If condstr = " " Then
       condstr = "leibie LIKE  '" + Trim(Text4.Text) + "& ' "
     Else
       condstr = condstr + "and leibie LIKE '" + Trim(Text4.Text) + "& '"
     End If
   End If


  If condstr <> "" Then                    '已经写入条件
     Adodc1.RecordSource = "select * from erjiguan where " + condstr
     Adodc1.Refresh
     
  Else                                     '没有写入条件,返回原记录
     Adodc1.RecordSource = "select * from erjiguan "
     Adodc1.Refresh
   
  End If
  
  recs = Adodc1.Recordset.RecordCount
  
  If recs = 0 Then
    MsgBox "没有任何满足条件的器件", vbOKOnly, "信息提示"
  End If
  Call encomm

End Sub


表中有个元件型号为:1
我在xinghao的text1里面输入 1  之后点确定
却显示"没有任何满足条件的器件", secs还是为0
这是为什么呢?
8 回复
#2
cnfarer2010-04-15 07:04

Like 的表达式中应该包含通配符(比如:%  _等)
#3
xywnhy2010-04-15 09:46
那应该怎么写呢,小弟写不来啊
大哥,把like那句写一下啊~~~~~~~~~~
不胜感激
#4
happynight2010-04-15 10:28
condstr = "xinghao  LIKE '" & Trim(Text1.Text) & "% '"

第一 连接符最好使用"&"不容易出歧义或出错
第二 模糊查询是使用"%"作为通配符(好象是这么叫的吧 )
#5
xywnhy2010-04-15 10:43
Private Sub command1_click()
  condstr = ""
   If Trim(Text1.Text) <> "" Then
     If condstr = "" Then
       condstr = "xinghao  LIKE '" & Trim(Text1.Text) & "% '"
     End If
   End If
   
   If Trim(Text2.Text) <> "" Then
     If condstr = "" Then
       condstr = "gongneng LIKE '" & Trim(Text2.Text) & "% '"
     Else
       condstr = condstr & "and gongneng LIKE '" & Trim(Text2.Text) & "% '"
     End If
   End If
   
   
   If Trim(Text3.Text) <> "" Then
     If condstr = "" Then
       condstr = "fengzhuang LIKE '" & Trim(Text3.Text) & "% '"
     Else
       condstr = condstr & "and fengzhuang LIKE '" & Trim(Text3.Text) & "% '"
     End If
   End If
   
   If Trim(Text4.Text) <> "" Then
     If condstr = " " Then
       condstr = "leibie LIKE  '" & Trim(Text4.Text) & "% ' "
     Else
       condstr = condstr & "and leibie LIKE '" & Trim(Text4.Text) & "% '"
     End If
   End If

现在是这样了,还是显示没有原件,recs还是0

[ 本帖最后由 xywnhy 于 2010-4-15 10:47 编辑 ]
#6
happynight2010-04-15 12:00
你调试的时候分两步调试
第一 调试SQL语句看看SQL语句是否有正常返回数据
第二 调试VB代码,ADO RECORDSET的属性设置也有讲究,若设置不当 即使返回了数据 RECORDCOUNT也会显示为"0"
     建议 recs = Adodc1.Recordset.RecordCount  
          If recs = 0 Then
             MsgBox "没有任何满足条件的器件", vbOKOnly, "信息提示"
          End If
    修改成
          IF NOT(Adodc1.Recordset.EOF AND Adodc1.Recordset.BOF) THEN  MsgBox "没有任何满足条件的器件", vbOKOnly, "信息提示"

           
#7
xywnhy2010-04-15 12:47
谢谢版主,这次是不管查出东西来没 都会提示“没有器件”了


[ 本帖最后由 xywnhy 于 2010-4-15 13:05 编辑 ]
#8
xywnhy2010-04-15 13:11
谢谢版主 已经调试成功了
#9
happynight2010-04-15 14:11
以下是引用xywnhy在2010-4-15 12:47:17的发言:

谢谢版主,这次是不管查出东西来没 都会提示“没有器件”了
SORRY  应该改成
IF (Adodc1.Recordset.EOF AND Adodc1.Recordset.BOF) THEN  MsgBox "没有任何满足条件的器件", vbOKOnly, "信息提示"
1