Office中國(guó)論壇/Access中國(guó)論壇

 找回密碼
 注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

返回列表 發(fā)新帖
查看: 2570|回復(fù): 5
打印 上一主題 下一主題

[Access本身] 批量更新

[復(fù)制鏈接]

點(diǎn)擊這里給我發(fā)消息

跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2015-6-8 06:06:45 來(lái)自手機(jī) | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
http://www.devsuperpage.com/search/Articles.aspx?G=8&ArtID=93090
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享分享 分享淘帖 訂閱訂閱

點(diǎn)擊這里給我發(fā)消息

2#
 樓主| 發(fā)表于 2015-6-8 06:08:51 來(lái)自手機(jī) | 只看該作者
well-known "pubs" sample database in SQL Server 2000.  The following simple procedure (in Access nodule) works perfectly, exactly as expected:  Public Sub Test()  Dim conn As ADODB.Connection: Set conn = CurrentProject.AccessConnection Dim rs As ADODB.Recordset: Set rs = New ADODB.Recordset  With rs  ' Open recordset .Source = "SELECT * FROM jobs" .LockType = adLockBatchOptimistic .CursorType = adOpenStatic .CursorLocation = adUseClient Set .ActiveConnection = conn .Open  ' Disconnect recordset Set .ActiveConnection = Nothing  ' Move around and change some records .MoveNext .Fields("job_desc") = .Fields("job_desc") + " xxx" .Update  .MoveNext .Fields("job_desc") = .Fields("job_desc") + " xxx" .Update  ' Re-connect recordset Set .ActiveConnection = conn  ' Save changes .UpdateBatch  End With  End Sub  The "job_desc" fields of the second and third record are changed (and those changes become "visible" to other DB clients only after the recordset is re-connected and batch-updated).  Now, when I try to use the same technique with a recordset bound to a form (i.e. when I try to let a user to change some records in a continuous form and then commit those changes all at once by pressing "Save" button), it does not work. Here is the code behind the continuous form which is bound to the "jobs" table at design time:  Option Compare Database Option Explicit  Private m_conn As ADODB.Connection Private m_rs As ADODB.Recordset  Private Sub Form_Open(Cancel As Integer)  Set m_conn = CurrentProject.AccessConnection Set m_rs = New ADODB.Recordset  Me.RecordSource = ""  With m_rs  ' Open recordset .Source = "SELECT * FROM jobs"

點(diǎn)擊這里給我發(fā)消息

3#
 樓主| 發(fā)表于 2015-6-8 06:09:46 來(lái)自手機(jī) | 只看該作者
With m_rs  ' Open recordset .Source = "SELECT * FROM jobs" .LockType = adLockBatchOptimistic .CursorType = adOpenStatic .CursorLocation = adUseClient Set .ActiveConnection = m_conn .Open  ' Disconnect recordset Set .ActiveConnection = Nothing  End With  ' "Give" recordset to the form ' so that user can move around and change some records. Set Me.Recordset = m_rs  End Sub  Private Sub cmdSave_Click()  With m_rs  ' Re-connect recordset Set .ActiveConnection = m_conn  ' Save changes Call .UpdateBatch(adAffectAll)  End With  Me.SetFocus DoCmd.Close  End Sub  None of the changes made by the user are applied to the database. :-(  Interestingly enough, when I inspect the form's recordset just before reconnecting it, all the records touched by the user through the form's controls do have new values in their fields. But, because of some reasons - reasons that I do not understand yet - those changes are not respected by UpdateBatch method (or by the adFilterPendingRecords filter).  If connected-ness of ADO recordsets was meant to be transparent to Access 2003 forms (and I really want to believe so), then it feels like I am missing some small but important details here. Any help would be greatly appreciated!  Thank you, Yarik.  Re: Cannot update a disconnected (and then

點(diǎn)擊這里給我發(fā)消息

4#
 樓主| 發(fā)表于 2015-6-8 06:10:43 來(lái)自手機(jī) | 只看該作者
綁定到窗體就無(wú)法批量更新了?

點(diǎn)擊這里給我發(fā)消息

5#
 樓主| 發(fā)表于 2015-6-9 04:43:34 來(lái)自手機(jī) | 只看該作者
實(shí)驗(yàn)了-一下,的確如此,Acc的窗體可能無(wú)法實(shí)現(xiàn),好遺憾

點(diǎn)擊這里給我發(fā)消息

6#
發(fā)表于 2021-1-15 10:16:44 | 只看該作者
學(xué)習(xí)
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則

QQ|站長(zhǎng)郵箱|小黑屋|手機(jī)版|Office中國(guó)/Access中國(guó) ( 粵ICP備10043721號(hào)-1 )  

GMT+8, 2024-10-23 08:39 , Processed in 0.119120 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復(fù) 返回頂部 返回列表