Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code that seems to work, but something is awry ... If I execute the following VBA code it DOES update the Database, but them fails on the CLOSE statement saying Error 3704, the file is already Closed. So, the obvious thing is to just delete the CLOSE statement, but I have read that I don't need to OPEN the Table to do the Update, so should I be using a different option on the line that executes the SQL statement (I tried UPDATE but that didn't work) ?!?

VB
Sub UpdateDatabaseLeague(temp)

On Error GoTo 0

SQLstr = "UPDATE League SET SessionNo = " & myEmailSessionNo & " WHERE League.[ID] = " & temp

Set KA_RS_League = New ADODB.Recordset

If KA_RS_League.State = adStateOpen Then KA_RS_League.Close

KA_RS_League.Open SQLstr, KA_DB, adOpenDynamic, adLockOptimistic

KA_RS_League.Close

End Sub


What I have tried:

I tried using UPDATE instead of OPEN and I researched on line to find out that I don't need to Open the Table ...

VB
KA_RS_League.Update SQLstr, KA_DB, adOpenDynamic, adLockOptimistic
Posted
Updated 7-Mar-17 10:21am

If you are doing something that doesn't need records as a result (ie update, insert or delete) then you can use the ADODB.Connection object instead and use its Execute method to execute your SQL.

Google "adodb.connection" or "adodb.connection execute" for sample code.
 
Share this answer
 
Comments
Gary Heath 7-Mar-17 16:22pm    
Thank you
You have to use ADODB.Command[^] instead of ADODB.Recordset.
 
Share this answer
 
Comments
Gary Heath 7-Mar-17 16:22pm    
Thank you
Maciej Los 8-Mar-17 13:26pm    
You're very welcome.
To help anybody else like myself who codes but doesn't understand all the intricacies and terminology, this code works for me :-)

VB
Option Explicit
Option Base 1
Option Compare Text

Sub UpdateDatabaseLeague(temp)

On Error GoTo 0

SQLstr = "UPDATE League SET SessionNo = " & myEmailSessionNo & " WHERE League.[ID] = " & temp

Set KA_RS_League = New ADODB.Recordset

If KA_RS_League.State = adStateOpen Then KA_RS_League.Close

Set KA_RS_League = KA_DB.Execute(SQLstr)

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900