Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Update data in resultset?

Status
Not open for further replies.

briansmithdanisco

IS-IT--Management
Jun 13, 2007
19
US
I have a module that does a select and returns a resultset with 1 to many records. I loop through the records and do some time calculations. I need to update each of the recrods in the resultset with the correct calculated time. I think I need to set the LockType to adLockOptimistic, but when I do, I still get the error that my resultset is read only.

Can anyone suggest where to put the LockType line so I am able to update the resultset data?

Here is an example of my SELECT. I have tried several combinations of SET commands, but nothing seems to be working.
Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Set cmd = New ADODB.Command

rst.LockType = adLockOptimistic

' Get all the on-stream records for this batch number.
With cmd
.ActiveConnection = CurrentProject.Connection
.CommandText = "Select * from tblOnStreamTime " & _
"Where '" & Me.MainBatchNumber.Value & "' = tblOnStreamTime.MainBatchNumber;"
.CommandType = adCmdText
.Prepared = True
Set rst = .Execute
End With

After I do some calculations I try to update the time value in the resultset with the calculated time:
rst!TotalTimeDetail = dteTimeCalc
rst.Update

Any suggestions would be appreciated.
Thanks.
 
Look at the following thread

thread705-1357831

I think it might help you
 
No need for the command object.

Code:
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
With rst
   .ActiveConnection = CurrentProject.Connection
   .CursrorLocation = adUseClient
   .CursorType = adOpenStatic
   .LockType = adLockOptimistic
   .Source = "SELECT * " & _
             "FROM tblOnStreamTime " & _
             "WHERE MainBatchNumber='" & Me.MainBatchNumber.Value & "'"
   .Open
End With

'do some calculations 

With rst
   .Fields("TotalTimeDetail")= dteTimeCalc
   .Update
   .Close 
End With
Set rst = Nothing

If MainBatchNumber is NOT of text data type field then your WHERE clause should be
"WHERE MainBatchNumber=" & Me.MainBatchNumber.Value
 
Thanks JerryKlmns!! It worked perfectly! I am self tought with VBA so sometimes even though something works, I am not 100% sure why.

If I am working in the same database with selects on tables, I don't need to create a new command object each time? I have always used command and result set objecst in my selects, updates, deletes...

Thanks again for your help.
 
< I am self tought with VBA
That makes us two [cheer]

<If I am working in the same database with selects on tables, I don't need to create a new command object each time?

Command object is used with parameter saved queries, to pass the parameter(s) value(s)

For action queries use the Execute method of a connection object (CurrentProject.Connection is one), along with the option adExecuteNoRecords=128. If you execute an sql statement created on the fly use adCmdText=1. Those two dogether sum up to the value 129

CurrentProject.Connection.Execute strSQL, , 129
 
< I am self tought with VBA
That makes us two [cheerS]

<If I am working in the same database with selects on tables, I don't need to create a new command object each time?

Command object is used with parameter saved queries, to pass the parameter(s) value(s)

For action queries use the Execute method of a connection object (CurrentProject.Connection is one), along with the option adExecuteNoRecords=128. If you execute an sql statement created on the fly use adCmdText=1. Those two dogether sum up to the value 129

CurrentProject.Connection.Execute strSQL, , 129
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top