我编写了一个数据库查询程序,基本原理是先定义一个 adodb.recordset 实例,在打开数据库时用 sql 语句取出符合条件的记录,显示在一个窗体上.
但问题来了,因为数据源中的字段有些是数值型,有些是字符型,而我的查询条件是通过两个文本框输入的,数据查询时语句稍有不同:
1.数值型字段查询:
rs.open "select * from db where " & text1.text & "=" &_
text2.text,"dsn=db"2.字符型字段查询:
rs.open "select * from db where " & text1.text & "= '" &
text2.text & "'","dsn=db"两者的差别就在一对 '' 上,但是我又不想让程序变得更复杂,请教高人,可有好办法能两者
兼顾?
新手上路,问题多多,请不吝赐教!
但问题来了,因为数据源中的字段有些是数值型,有些是字符型,而我的查询条件是通过两个文本框输入的,数据查询时语句稍有不同:
1.数值型字段查询:
rs.open "select * from db where " & text1.text & "=" &_
text2.text,"dsn=db"2.字符型字段查询:
rs.open "select * from db where " & text1.text & "= '" &
text2.text & "'","dsn=db"两者的差别就在一对 '' 上,但是我又不想让程序变得更复杂,请教高人,可有好办法能两者
兼顾?
新手上路,问题多多,请不吝赐教!
sn.Open "select * from db where 条件", cn '随便写一个条件,目的是sn不要打开太多的记录,影响速度
Select Case sn(text1.text).Type
Case adChar, adBSTR
rs.open "select * from db where " & text1.text & "= '" &text2.text & "'","dsn=db"
Case adBigInt, adBoolean, adCurrency, adDecimal, adDouble, adInteger, adNumeric, adSingle, adSmallInt, adTinyInt
rs.open "select * from db where " & text1.text & "=" &text2.text,"dsn=db"
End Select
sn.close