令人头疼的问题!!!
我想实现数据集的添加,实现多次查询出的数据在datagrid中显示,我试过各种方法,总是出现各种错误,无法实现,请各位大侠多多指点!
代码如下:
Private Sub Command2_Click()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.CursorLocation = adUseClient
'cnn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=XSCJ"
'设置连接字符串ConnectionString属性
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=XSCJ"
'打开到数据库的连接
cnn.Open
Set rst = cnn.Execute("select * from yuanliaobiao where ylnumber='" & Trim(Text1.Text) & "'or ylname='" & Trim(Text2.Text) & "'or pynumber='" & Trim(Text3.Text) & "'")
rst.ActiveConnection = Nothing
cnn.Close
Set DataGrid1.DataSource = rst
Dim rst1 As New ADODB.Recordset
rst1.ActiveConnection = Nothing
rst1.CursorLocation = adUseClient
rst1.CursorType = adOpenStatic
rst1.LockType = adLockOptimistic
Set rst1 = rst.Clone '我在这复制前一个记录集
Set Frmcpglbj.DataGrid1.DataSource = rst1 '在这观查rst1的记录与rst一样
Dim i As Integer
' If rst1.RecordCount = 0 Then
Set rst1 = rst.Clone
' Else
rst1.AddNew
For i = 0 To 7
rst1.Fields(i).Value = rst.Fields(i).Value
Next i
rst1.Update
rst.MoveNext
Set Adodc2.Recordset = rst1
Set Frmcpglbj.DataGrid1.DataSource = Adodc2
' End If
End Sub
[解决办法]
Set rst1 = rst.Clone 并没有复制数据,只当你rst重新加载数据之后两个才会不一样