设计一个程序,用户从界面选择查询条件,然后得出结果。如果用户不选任何条件,直接查询,则不做响应。
我写了一组sql语句:
strdb= "select * from mytable where"
然后根据用户的选择加上
strdb = strdb & " a = " & a1 & " and "
...
但是我做判断时,下面语句没有设想的结果
if Lcase(Right(strdb,5)) = "where" Then Exit Sub
或者
if Right(strdb,5) = "where" Then Exit Sub请各位帮我检查一下,该怎么写
我写了一组sql语句:
strdb= "select * from mytable where"
然后根据用户的选择加上
strdb = strdb & " a = " & a1 & " and "
...
但是我做判断时,下面语句没有设想的结果
if Lcase(Right(strdb,5)) = "where" Then Exit Sub
或者
if Right(strdb,5) = "where" Then Exit Sub请各位帮我检查一下,该怎么写
解决方案 »
- 这两句代码有什么不一样
- 关于多线程的一些问题。
- 检测文件是否被读取
- mshflexgrid当用代码转换到另一个格子时,如何触发它的entercell和leavecell两个事件
- 请问一下,我用datagrid控件,我想定义columns的caption,可是属性里的columns属性里为什么只有两个(columns1,column2),怎么再添加其
- 关于CreateObject地使用问题
- Internet Transfer 能使用http协议下载二进制文件吗?
- 请大家帮忙看一下,这段ASP代码哪里有问题.才是报错!
- VB怎样更改XML中的属性值
- ActiveReport
- 想知道火星文输入发的原理是什么?
- 请教DataGrid自动填零的问题?
dim where_temp1 as string
dim where_temp2 as string
dim strsql as stringif 条件变量1="" then
where_temp1=""
else
where_temp1=" and 字段1='"& 条件变量1 &"'"
end if
if 条件变量2="" then
where_temp2=""
else
where_temp2=" and 字段2='"& 条件变量2 &"'"
end if'构造SQL查询语句
strsql=" select * from 表名 where 1=1 " & where_temp1 & where_temp2
debug .print strdb
确认strdb为
select * from mytable where 但是我做判断时,下面语句没有设想的结果
if Lcase(Right(strdb,5)) = "where" Then Exit Sub
那么上面这句有什么不对吗
strdb = strdb & " a = '" & a1 & "' and "
關鍵是看你的a,是什麼類型
Dim lCount As Long
Dim sSql As String
Dim vFilters() As String
' 按照全部条件数来定义
ReDim vFilters(1 To max_conditions)
If 选择条件1 Then
lCount = lCount + 1
vFilters(lCount) = "字段1 = ..."
End If
If 选择条件2 Then
lCount = lCount + 1
vFilters(lCount) = "字段2 = ..."
End If
'...
If 选择条件n Then
lCount = lCount + 1
vFilters(lCount) = "字段n = ..."
End If
sSql = "select * from xxx "
If lCount > 0 Then
ReDim Preserve vFilters(1 To lCount)
sSql = sSql & " where " & Join(vFilters, " and ") ' 也可以用 Or
End If
MsgBox sSql
End Sub