• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Can't get ADO Recordset to work

I'm trying to get the following to work.  I'm not to good with ADO yet.
Dim rstTimeEst As New ADODB.Record
  Dim strQuery As String
  Dim dbBat1 As New Command
 
  strQuery = "DELETE * FROM Commitment_Time_Est_Data"
  CurrentDb.Execute strQuery
  dbBat1.ActiveConnection = CurrentProject.Connection
  dbBat1.CommandText = "SELECT * FROM Commitment_Time_Est_Data"
  rstTimeEst.Open dbBat1, , adOpenForwardOnly, adLockOptimistic
  rstTimeEst!Commitment_ID = !Commitment_ID
  rstTimeEst.Update

Open in new window

0
schmir1
Asked:
schmir1
  • 3
  • 2
1 Solution
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You need to open a Recordset:

Dim rstTimeEst As ADODB.Recordset

CurrentProject.Execute "DELETE * FROM Commitment_Time_Est_Data"

Set rstTimeEst = New ADODB.Recordset  
rstTimeEst.Open  "SELECT * FROM Commitment_Time_Est_Data",  CurrentProject.Connection, adOpenKeyset, adLockOptimistic

rstTimeEst!Commitment_ID = <some value here>
rstTimeEst.Update

rstTimeEst.Close
Set rstTimeEst = Nothing
0
 
schmir1Author Commented:
I got the following errors.  See the errors list in the comments:
Dim rstTimeEst As ADODB.Recordset

'CurrentProject.Execute "DELETE * FROM Commitment_Time_Est_Data"  'Run-time error '438':
                                                                  'Object doesn 't support this property or method

Set rstTimeEst = New ADODB.Recordset
rstTimeEst.Open "SELECT * FROM Commitment_Time_Est_Data", CurrentProject.Connection, adOpenKeyset, adLockOptimistic

rstTimeEst!Commitment_ID = 1 'Run-time error '3021': Either BOF or EOF is True, or the current record has been deleted.
                             'Requested operation requires a current record.

rstTimeEst.Update

rstTimeEst.Close
Set rstTimeEst = Nothing
0
 
schmir1Author Commented:
I had to give up on ADO code and go back to something I know, DAO.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
This one:

CurrentProject.Execute "DELETE * FROM Commitment_Time_Est_Data"  

Should be:

CurrentProject.CONNECTION.Execute "blah blah"

sorry about that ...

as to the other ... you should always check to see if your REcordset has returned a value before attempting to update it:

If Not(rstTimeEst.EOF and rstTimeEST.BOF) Then
  '/do your update here
End If

However, since you are (apparently) deleting every record in the Commitment_Time_Est_Data table, you cannot use a Update query for this, you'd need to run an INSERT query. You can achieve the same thing like this:

CurrentProject.Connection.Execute "DELETE * FROM Commitment_Time_Est_Data"  
CurrentProject.Connection.Execute "INSERT INTO Commitment_Time_Est_Data(Commitment_ID) VALUES(1)"

Assuming the datatype of Commitment_ID is Numeric, this is all you'd need to do.
0
 
schmir1Author Commented:
That makes sense.  The data is generated every time the user presses the button so I delete the old stuff.  Thanks for your help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now