Help!怎样使多次参数化SQL查询结果同时显示在datagrid控件呢?vb6.0 新手
请论坛中的高手帮帮我这个vb新手吧,这困扰我很久了啊,不甚感激啊!
[size=10px]自己写的源代码:
Option Explicit
Dim Rs As New ADODB.Recordset
Dim Rs_new As New ADODB.Recordset
Private Sub Label1_Click() ’单击label1就执行一次查询,查询关键字是A1.IPL编号='" & Lbl.Caption & "'
If Rs.State = adStateOpen Then
Rs.Close
End If
Label1.BackColor = vbRed
Call OPer_Label(Label1, DataGrid1, Adodc1, True, 1)
End Sub
Private Sub Label2_Click() ’和label1的功能是一样,只是caption不同
If Rs.State = adStateOpen Then
Rs.Close
End If
Label2.BackColor = vbRed
Call OPer_Label(Label2, DataGrid1, Adodc1, True, 2)
End Sub
Public Sub Lbl_selection(Lbl As Label) ’该sub是连接access数据库,执行参数化查询,得到recordset
Dim Cn As New ADODB.Connection
Dim StrCnn As String
StrCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\航材管理应用程序\数据库\FirstDataBase.mdb"
Cn.CursorLocation = adUseClient
Cn.Open StrCnn
Dim Cmd As New ADODB.Command
Dim Prm As New ADODB.Parameter
Cmd.CommandText = "SELECT A1.IPL编号, A1.件号, A1.名称, A1.需求量, A1.库存量, A2.批号, A2.价格, A2.到货时间 From A1, A2 WHERE A1.件号=A2.件号 and A1.IPL编号='" & Lbl.Caption & "'" ’参数化查询sql命令
Cmd.ActiveConnection = Cn
Rs.CursorType = adOpenStatic
Rs.LockType = adLockBatchOptimistic
Rs.Open Cmd, , , , adCmdText
End Sub
Public Sub OPer_Label(Lbl As Label, Datagd As DataGrid, AdoData As Adodc, DTell As Boolean, indxAsInteger)
Call Lbl_selection(Lbl)
If indx = 1 Then
Set Rs_new = Rs.Clone
Set Datagd.DataSource = Rs_new
Else
If indx > 1 Then
Dim i As Integer
Set Rs_new = Rs.Clone
Rs_new.AddNew
For i = 0 To 7
Rs_new.Fields(i).Value = Rs.Fields(i).Value
Next i
Set AdoData.Recordset = Rs_new
Set Datagd.DataSource = AdoData
End If
End If
End sub
OPer_Label过程是按网上说的做的:建立一个每次参数化查询后得到的Rs,和绑定并显示在datagrid的另一个Rs_new,把每次查询后的Rs逐个添加到Rs_new以使多次查询能够同时在datagrid中显示出来。
哈哈,说了这么久都没有说自己碰到的问题是什么啊!
这个OPer_Label过程代码有些问题,不能把多次查询的结果同时显示在datagrid上啊!会覆盖前一次查询的结果!
有谁知道怎么写的话,请告诉我吧,谢谢啦。
请大家帮帮忙咯,感激不尽啊!![/size]
[解决办法]
思路:
将每次查询的数据集(rs)循环添加的另一数据集(rs_new),datagrid和rs_new绑定即可
[解决办法]
Set AdoData.Recordset = Rs_new '为什么要这样
Set Datagd.DataSource = AdoData '为什么要这样
直接
Set Datagd.DataSource = Rs_new
Datagd.Refresh
[解决办法]
同意3#的说法;
设置一个数据集rs,把每次查询参数都附到rs中
dim rs as new adodb.recordset;
if rs nothing then
把数据集附给rs
else
rs.addnew
~~~
~~~
set datagrade.datasource=rs
不知道这样写看的懂么?
[解决办法]
..ElseIf indx > 1 Then '执行第二次查询(单击label2),把查询结果添加到datagrid中Dim i As Integerif rs_new.RecordCount = 0 then'无记录就复制 Set Rs_new = Rs.Clone'不判断,直接复制是覆盖的原因。elseRs_new.AddNewFor i = 0 To 7Rs_new.Fields(i).Value = Rs.Fields(i).Value ‘往datagrid中添加第二次查询的recordsetNext iSet AdoData.Recordset = Rs_newSet Datagd.DataSource = AdoDataend ifEnd IfEnd If
------解决方案--------------------
Dim Rs_new As ADODB.Recordset '不需要自动创建'Public Sub OPer_Label(Lbl As Label, Datagd As DataGrid, AdoData As Adodc, DTell As Boolean, indx As Integer) Dim i As Integer Call Lbl_selection(Lbl) If indx = 1 Then Set Rs_new = Rs.Clone Set Rs_new.ActiveConnection = Nothing '断开连接后添加的记录不会影响数据库' Set Datagd.DataSource = Rs_new Else While Not Rs.EOF Rs_new.AddNew For i = 0 To 7 Rs_new.Fields(i).Value = Rs.Fields(i).Value Next i Rs_new.Update Rs.MoveNext Wend Datagd.Refresh End IfEnd Sub