关于Adodc+Datagrid+Access的问题,实在解决不了了!求助!
1、我在查询时用 Adodc连接到Access数据库的一张表,想用几个查询条件约束进行组合查询,然后在Datagrid中显示结果,因此将查询条件设为变量,然而实现不了。
2、我不用变量,单写where查序号可查,比如我表里有序号为1、2、3、4四条数据,我在序号栏输入1、2、3、4都分别可查到这四条,输入5或6、7、……时我用MSgbox写的“未找到……”即发挥作用弹了出来,可是当我回头输入1、2、3、4时,也弹出来了,竟然开始能查现在也查不了了,这是为什么?各位高手,谢谢了!
代码如下:
Option Explicit
Dim cx As String
Dim cxt1 As String, cxt2 As String, cxt3 As String
Dim cxt4 As String, cxt5 As String, cxt6 As String
Private Sub Command1_Click()
MsgBox cx
If cx = "" Then
MsgBox "请输入查询条件!", vbExclamation, "错误"
Exit Sub
DataGrid1.ClearFields
End If
With Adodc1
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.CommandType = adCmdText
.RecordSource = "Select * from 资料总表 where" + cx
.Refresh
End With
Set DataGrid1.DataSource = Adodc1
Adodc1.Refresh
cx = ""
If Text4 = "" Then
MsgBox "没有找到符合条件的记录!", vbExclamation
End If
cx = ""
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
Text3 = ""
Combo1.Text = ""
Combo2.Text = ""
Combo3.Text = ""
Combo4.Text = ""
Combo5.Text = ""
Combo6.Text = ""
Combo7.Text = ""
Combo8.Text = ""
End Sub
Private Sub Text2_Change()
cxt2 = " 标题 like " + "'" + Text2 + "%" + "'"
If cx <> "" Then
cx = cx + " and " + cxt2
Else
cx = cxt2
End If
End Sub
Private Sub Text3_Change()
cxt1 = " 序号=" + "'" + Text3 + "'"
If cx <> "" Then
cx = cx + " and" + cxt1
Else
cx = cxt1
End If
End Sub
问题点数:0、回复次数:4Top
1 楼yoki(小马哥--鬓微霜,又何妨)回复于 2003-11-03 18:50:15 得分 0
Option Explicit
Dim cx As String
Dim cxt1 As String, cxt2 As String, cxt3 As String
Dim cxt4 As String, cxt5 As String, cxt6 As String
Private Sub Command1_Click()
cx=" where 1=1"
cx=iif(trim(text3.text)<>"",cx+ " and 序号=" + "'" + Text3 + "'",cx)
cx=iif(trim(text2.text)<>"",cx+ " and 标题 like " + "'" + Text2 + "%" + "'",cx)
MsgBox cx
If cx = "" Then
MsgBox "请输入查询条件!", vbExclamation, "错误"
Exit Sub
DataGrid1.ClearFields
End If
With Adodc1
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.CommandType = adCmdText
.RecordSource = "Select * from 资料总表" + cx
.Refresh
End With
Set DataGrid1.DataSource = Adodc1
Adodc1.Refresh
cx = ""
If Text4 = "" Then
MsgBox "没有找到符合条件的记录!", vbExclamation
End If
cx = ""
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
Text3 = ""
Combo1.Text = ""
Combo2.Text = ""
Combo3.Text = ""
Combo4.Text = ""
Combo5.Text = ""
Combo6.Text = ""
Combo7.Text = ""
Combo8.Text = ""
End Sub
Top
2 楼yuanchaoj(菜鸟)回复于 2003-11-05 07:54:53 得分 0
这个方法还是不行,用msgbox cx一看,查询条件全乱了,小马哥,你还有没有更好的方法啊,小弟在此拜谢了!Top
3 楼li_ping(学习WinFX("Avalon"))回复于 2003-11-05 09:00:27 得分 0
Private Sub Command1_Click()
cx=""
cx=iif(trim(text3.text)<>"",cx+ "序号=" + "'" + Text3 + "'",cx)
cx=iif(trim(text2.text)<>"",cx+ " and 标题 like " + "'" + Text2 + "%" + "'",cx)
MsgBox cx
If cx = "" Then
MsgBox "请输入查询条件!", vbExclamation, "错误"
Exit Sub
DataGrid1.ClearFields
End If
With Adodc1
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.CommandType = adCmdText
.RecordSource = "Select * from 资料总表" + cx
.Refresh
End With
Set DataGrid1.DataSource = Adodc1
Adodc1.Refresh
cx = ""
If Text4 = "" Then
MsgBox "没有找到符合条件的记录!", vbExclamation
End If
cx = ""
End Sub
Top
4 楼li_ping(学习WinFX("Avalon"))回复于 2003-11-05 09:10:04 得分 0
错了,应该是这样:
Private Sub Command1_Click()
cx=""
cxt3=iif(trim(text3.text)<>"","序号=" + "'" + Text3 + "'","")
cxt2=iif(trim(text2.text)<>"","标题 like " + "'" + Text2 + "%" + "'","")
cx=iif(cxt3<>"",cxt3+" and ",cxt3)+cxt2
MsgBox cx
If cx = "" Then
MsgBox "请输入查询条件!", vbExclamation, "错误"
Exit Sub
DataGrid1.ClearFields
End If
With Adodc1
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.CommandType = adCmdText
.RecordSource = "Select * from 资料总表 where " + cx
.Refresh
End With
Set DataGrid1.DataSource = Adodc1
Adodc1.Refresh
cx = ""
If Text4 = "" Then
MsgBox "没有找到符合条件的记录!", vbExclamation
End If
cx = ""
End Sub
Top




