简单问题 * 高分求解 高人帮我看看这段代码哪里错了 先出100不够在加

hailong326 2004-09-06 06:13:29
private Sub Command7_Click()
Dim str As String
str = ""
If Trim(Text1.Text) <> "" Then
If str = "" Then
str = "图书编号'" + Trim(Text1.Text) + "'"
Else
str = str + "and 图书编号='" + Trim(Text1.Text) + "'"
End If
End If

Adodc1.RecordSource = "select * from 图书信息 where" + str
Adodc1.Refresh
End Sub

谢谢大家了!
...全文
133 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2004-09-07
  • 打赏
  • 举报
回复
这样的程序实用上还是有问题。用户在 Text1 中可能输入任何东西。会出错的。

最好是用下拉列表方式的 Combo 让用户选字段名、比较符、可选值。字段名和可选值从数据库表中提取,用代码填充。
ryuginka 2004-09-07
  • 打赏
  • 举报
回复
其实楼主要求的是个动态SQL语句,但是他考虑的太简单了,连用户如果不输入的情况都没有考虑,其实最好的办法就是用LIKE语句,但是效率底了点。
of123 2004-09-07
  • 打赏
  • 举报
回复
这样的程序实用上还是有问题。用户在 Text1 中可能输入任何东西。会出错的。

最好是用下拉列表方式的 Combo 让用户选字段名、比较符、可选值。字段名和可选值从数据库表中提取,用代码填充。
ryuginka 2004-09-07
  • 打赏
  • 举报
回复
private Sub Command7_Click()
Dim str As String
str = ""
If Trim(Text1.Text) <> "" Then
If str = "" Then
str = " 图书编号'" + Trim(Text1.Text) + "'"
Else
str = str + " and 图书编号='" + Trim(Text1.Text) + "'"
End If
else '加上text1.text为空的时候,否则你的查询SQL语句有错误

End If

Adodc1.RecordSource = "select * from 图书信息 where" + str
Adodc1.Refresh
End Sub
ryuginka 2004-09-07
  • 打赏
  • 举报
回复
private Sub Command7_Click()
Dim str As String
str = ""
If Trim(Text1.Text) <> "" Then
If str = "" Then
str = " 图书编号'" + Trim(Text1.Text) + "'" 图书编号前面加空格

Else
str = str + " and 图书编号='" + Trim(Text1.Text) + "'" 'and前面加空格

End If
End If

Adodc1.RecordSource = "select * from 图书信息 where " + str 'where后面加空格
Adodc1.Refresh
End Sub
zdmaster 2004-09-07
  • 打赏
  • 举报
回复
private Sub Command7_Click()
Dim str As String
str = ""
If Trim(Text1.Text) <> "" Then
If str = "" Then
str = "图书编号'" + Trim(Text1.Text) + "'"
Else
str = str + " and 图书编号='" + Trim(Text1.Text) + "'" '此处的and 前应该有个空格
End If
End If

Adodc1.RecordSource = "select * from 图书信息 where 1=1 " + str
Adodc1.Refresh
End Sub
试试
langziwujia007 2004-09-07
  • 打赏
  • 举报
回复
private Sub Command7_Click()
Dim str As String
str = ""
If Trim(Text1.Text) <> "" Then
If str = "" Then
str = "图书编号'" + Trim(Text1.Text) + "'"
Else
str = str + " and 图书编号='" + Trim(Text1.Text) + "'" '此处的and 前应该有个空格
End If
End If

Adodc1.RecordSource = "select * from 图书信息 where" + str
Adodc1.Refresh
End Sub
hailong326 2004-09-06
  • 打赏
  • 举报
回复
代码还有refresh 的方法Iadodc失败,怎么回事啊?
congjl2002 2004-09-06
  • 打赏
  • 举报
回复
这个还有其它的错误的可能吗?比如连接之类......
congjl2002 2004-09-06
  • 打赏
  • 举报
回复
是啊,这个代码还有refresh 的方法Iadodc失败,怎么回事啊?
online 2004-09-06
  • 打赏
  • 举报
回复
str = ""
放到其他地方,如果在private Sub Command7_Click()中, str = str + "and 图书编号='" + Trim(Text1.Text) + "'",这句就不会执行了

在其他地方赋值,搂住可能作的是多条件查询?

starsoulxp 2004-09-06
  • 打赏
  • 举报
回复
Adodc1.RecordSource = "select * from 图书信息 where" + str少了个空格,应为:
Adodc1.RecordSource = "select * from 图书信息 where " + str


另外,
str = ""
If Trim(Text1.Text) <> "" Then
If str = "" Then
str = "图书编号'" + Trim(Text1.Text) + "'"
Else
str = str + "and 图书编号='" + Trim(Text1.Text) + "'"
End If
既然str已经赋为""了,那下面的If str = "" ,else还有什么用呢?

iiboy 2004-09-06
  • 打赏
  • 举报
回复
当然这名也错了:
str = "图书编号'" + Trim(Text1.Text) + "'"
要改为:
str = "图书编号='" + Trim(Text1.Text) + "'"
iiboy 2004-09-06
  • 打赏
  • 举报
回复
在and前加个空格,以及在where后加上一个空格,更改后的语句如下:
......
str = str + " and 图书编号='" + Trim(Text1.Text) + "'"
......
Adodc1.RecordSource = "select * from 图书信息 where " + str
.....
maxim 2004-09-06
  • 打赏
  • 举报
回复
的确,根据SQL语法,and前面,where后面应有个空格。
但是,前面不是有 str = "" 吗?根据楼主的代码Else后面的那一条str = str + "and 图书编号='" + Trim(Text1.Text) + "'"是错误,并且是无意义的。应该去掉,除非在此之前还有其他查询条件。完整的代码应该是

private Sub Command7_Click()
Dim str As String
Dim SQL As String
str = ""
If Trim(Text1.Text) <> "" Then
If str = "" Then
str = "图书编号'" + Trim(Text1.Text) + "'"
End If
End If

SQL= "select * from 图书信息 where" + str
Debug.Print SQL
Adodc1.RecordSource =Adodc1.RefreshSQL
End Sub
hailong326 2004-09-06
  • 打赏
  • 举报
回复
谢谢大家!

我试一下,如果可以马上结贴.

maxim 2004-09-06
  • 打赏
  • 举报
回复
问题出现在: str = str + "and 图书编号='" + Trim(Text1.Text) + "'"
前面不是有 str = "" 吗?
这样一来 Adodc1.RecordSource = "select * from 图书信息 where" + str
不就成了 Adodc1.RecordSource = "select * from 图书信息 where and 图书编号='" + Trim(Text1.Text) + "'"吗?
是语法错误,建议改为:
If Trim(Text1.Text) <> "" Then
str = str + " 图书编号='" + Trim(Text1.Text) + "'"
End If

另外可以使用 Debug.Print "select * from 图书信息 where" + str 进行验证调试,就不会出现这种情况了。

online 2004-09-06
  • 打赏
  • 举报
回复
str = str + " and 图书编号='" + Trim(Text1.Text) + "'"
Adodc1.RecordSource = "select * from 图书信息 where " + str

and前面,where后面有个空格
跟踪看一下str的值
G10 2004-09-06
  • 打赏
  • 举报
回复
关注中
netwan 2004-09-06
  • 打赏
  • 举报
回复
private Sub Command7_Click()
Dim str As String
str = ""
If Trim(Text1.Text) <> "" Then

str = str + " 图书编号='" + Trim(Text1.Text) + "'"

End If

Adodc1.RecordSource = "select * from 图书信息 where" + str
Adodc1.Refresh
End Sub

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧