Solved

Do I have to ReQuery after an UpdateBatch in ADO?

Posted on 2004-09-14
2
598 Views
Last Modified: 2013-12-25
I'm using Visual Basic 6 and ADO to open MS Access tables. Here is a sample of my code to create a connection and open a table:

Dim cn As ADODB.Connection, rsFg As ADODB.Recordset
Set cn = New ADODB.Connection
With cn
.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & gsAccessPath & "\Americraft.mdb"
.CursorLocation = adUseClient
.Open
End With

Set rsFg = New ADODB.Recordset
With rsFg
Set .ActiveConnection = cn
.CursorLocation = adUseClient .CursorType = adOpenDynamic
.LockType = adLockBatchOptimistic
.Source = "SELECT KEY,CUSTNO,ITEM,CUSTPO,ONHAND,CTNSONHAND,MATERIAL,DLABOR,VAR_OH,FIXED_OH FROM FG ORDER BY ITEM, CUSTPO"
.Open
End With


My table has an autonumber field named KEY as its primary key. Below is a sample of the code I use to update the table:

With rsFg
.Filter = "ITEM='" & txtFields(0) & "' AND CUSTPO='" & txtFields(1) & "'"
!onhand = !onhand - Val(txtFields(3))
!ctnsonhand = !ctnsonhand - Val(txtFields(3))
.UpdateBatch
.Filter = ""
.Requery
 End With

Is it important to include the primary key in the fields I get in my 'SELECT' statement, i.e. will the primary key field help ADO to find the right record to update? Do I have to issue a Requery after each UpdateBatch in order to make sure that ADO will find the right record to update the next time I do an UpdateBatch? Can I just Update instead of UpdateBatch?
0
Comment
Question by:BruceHSanford
2 Comments
 
LVL 13

Accepted Solution

by:
Michael_D earned 125 total points
ID: 12060389
You should use .UpdateBatch only if you changing several records without updating each one separatly.
You don't have to "help" ADO doing its job :)
This mean that you don't have to include "KEY" field into Select if you don't need it .
.Requery method ensure that you have "last updated" data - usefull only if your data might be changed from other application or workstation. All changes that YOU made already in your recordset.
0
 
LVL 10

Expert Comment

by:anv
ID: 12062469
yes u can use update instead of updatebatch in ur case..

since ur Recordset's cursortype is adOpenDynamic so the changes will be reflected to u instatntly... as and when data changes in that table...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now