我想实现点击按钮后,在表1里添加一条记录(Combo1.Text)
,然后把它取出来放到表2里,可是程序执行完毕只看到表1添加的新纪录,而表2没有看到,部分代码如下,我没有提交吗?
Recordset.AddNew
Recordset.Fields("cjdh").Value = Trim(Combo1.Text)
Recordset.UpdateConnection.Execute "insert into 表2 (cjdh) select cjdh from 表1 where cjdh='" & Trim(Combo1.Text) & "'"
,然后把它取出来放到表2里,可是程序执行完毕只看到表1添加的新纪录,而表2没有看到,部分代码如下,我没有提交吗?
Recordset.AddNew
Recordset.Fields("cjdh").Value = Trim(Combo1.Text)
Recordset.UpdateConnection.Execute "insert into 表2 (cjdh) select cjdh from 表1 where cjdh='" & Trim(Combo1.Text) & "'"
是不是更新的很慢?在没有更新完毕就访问数据库就取不到数据了?
這樣就行了,還那么複雜
Recordset.Fields("cjdh").Value = Trim(Combo1.Text)
Recordset.Update
Recordset.requery Connection.Execute "insert into 表2 (cjdh) select cjdh from 表1 where cjdh='" & Trim(Combo1.Text) & "'"
Recordset.requery 什么意思啊?
通过重新执行对象所基于的查询来更新 Recordset 对象中的数据。语法recordset.Requery Options参数Options 可选。位掩码,包含影响本操作的 ExecuteOptionEnum 值。如果将此参数设置为 adAsyncExecute,本操作将异步执行,操作完成时将发布一个 RecordsetChangeComplete 事件。说明用 Requery 方法通过重新发布原始命令和再次检索数据来刷新数据源中 Recordset 对象的所有内容。调用此方法等价于依次连续调用 Close 和 Open 方法。如果正在编辑当前记录或正在添加新记录,将发生错误。当 Recordset 对象打开时,定义游标性质(CursorType、LockType、MaxRecords 等)的属性为只读。因此,Requery 方法只能刷新当前游标。要更改任何一个游标属性并查看结果,必须使用 Close 方法以使这些属性重新变为读/写。然后可以更改属性设置并调用 Open 方法重新打开游标。
本范例演示从 Command 和 Connection 对象运行 Execute 方法。范例中还使用 Requery 方法检索 Recordset 中的当前数据,并用 Clear 方法清除 Errors 集合的内容。(通过 Recordset 的 ActiveConnection 属性的 Connection 对象来访问 Errors 集合。)运行此过程需要 ExecuteCommand 和 PrintOutput 过程。Public Sub ExecuteX() Dim strSQLChange As String
Dim strSQLRestore As String
Dim strCnn As String
Dim cnn1 As ADODB.Connection
Dim cmdChange As ADODB.Command
Dim rstTitles As ADODB.Recordset
Dim errLoop As ADODB.Error ' Define two SQL statements to execute as command text.
strSQLChange = "UPDATE Titles SET Type = " & _
"'self_help' WHERE Type = 'psychology'"
strSQLRestore = "UPDATE Titles SET Type = " & _
"'psychology' WHERE Type = 'self_help'" ' Open connection.
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=Pubs;User Id=sa;Password=; "
Set cnn1 = New ADODB.Connection
cnn1.Open strCnn ' Create command object.
Set cmdChange = New ADODB.Command
Set cmdChange.ActiveConnection = cnn1
cmdChange.CommandText = strSQLChange
' Open titles table.
Set rstTitles = New ADODB.Recordset
rstTitles.Open "titles", cnn1, , , adCmdTable ' Print report of original data.
Debug.Print _
"Data in Titles table before executing the query"
PrintOutput rstTitles ' Clear extraneous errors from the Errors collection.
cnn1.Errors.Clear ' Call the ExecuteCommand subroutine to execute cmdChange command.
ExecuteCommand cmdChange, rstTitles
' Print report of new data.
Debug.Print _
"Data in Titles table after executing the query"
PrintOutput rstTitles ' Use the Connection object's execute method to
' execute SQL statement to restore data. Trap for
' errors, checking the Errors collection if necessary.
On Error GoTo Err_Execute
cnn1.Execute strSQLRestore, , adExecuteNoRecords
On Error GoTo 0 ' Retrieve the current data by requerying the recordset.
rstTitles.Requery ' Print report of restored data.
Debug.Print "Data after executing the query " & _
"to restore the original information"
PrintOutput rstTitles rstTitles.Close
cnn1.Close
Exit Sub
Err_Execute: ' Notify user of any errors that result from
' executing the query.
If rstTitles.ActiveConnection.Errors.Count >= 0 Then
For Each errLoop In rstTitles.ActiveConnection.Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume NextEnd SubPublic Sub ExecuteCommand(cmdTemp As ADODB.Command, _
rstTemp As ADODB.Recordset) Dim errLoop As Error
' Run the specified Command object. Trap for
' errors, checking the Errors collection if necessary.
On Error GoTo Err_Execute
cmdTemp.Execute
On Error GoTo 0 ' Retrieve the current data by requerying the recordset.
rstTemp.Requery
Exit SubErr_Execute: ' Notify user of any errors that result from
' executing the query.
If rstTemp.ActiveConnection.Errors.Count > 0 Then
For Each errLoop In Errors
MsgBox "Error number: " & errLoop.Number & vbCr & _
errLoop.Description
Next errLoop
End If
Resume NextEnd SubPublic Sub PrintOutput(rstTemp As ADODB.Recordset) ' Enumerate Recordset.
Do While Not rstTemp.EOF
Debug.Print " " & rstTemp!Title & _
", " & rstTemp!Type
rstTemp.MoveNext
LoopEnd Sub