473,414 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Operation is not allowed when the object is closed

Hi All

I'm trying to insert records using VBA in Access into a mysql database and I'm getting the above error. The code I'm using I have found on the web, and in all frankness don't understand it as I'm only a beginner. However the code does work if I'm using a select statement to retrieve records, but it does not work when I'm trying to do an insert statement.

Any pointers/adivce would be really appreciated! The error is thrown on statement. rs.Close

Thanks Paul

Code is
Expand|Select|Wrap|Line Numbers
  1. Private Sub InsertTrainers_Click()
  2. On Error GoTo Err_InsertTrainers_Click
  3.     Dim strDatabaseName As String
  4.     Dim strDBCursorType As String
  5.     Dim strDBLockType As String
  6.     Dim strDBOptions As String
  7.  
  8.     Dim rs As ADODB.Recordset
  9.     Dim cn As ADODB.Connection
  10.     Dim b As Long
  11.     Dim strSQL As String
  12.     Dim strMsg, Response As String
  13.  
  14.     strMsg = "Do you want to add this event to all the trainers schedule?"
  15.  
  16.     'Get confirmation of the insert into the trainers schedule.
  17.     Response = MsgBox(strMsg, vbOKCancel)
  18.  
  19.     If Response = vbOK Then
  20.         strDBCursorType = adOpenDynamic  'CursorType
  21.         strDBLockType = adLockOptimistic   'LockType
  22.         strDBOptions = adCmdText         'Options
  23.  
  24.         Set cn = New ADODB.Connection
  25.         cn.Open ConnectString()
  26.         With cn
  27.             .CommandTimeout = 0
  28.             .CursorLocation = adUseClient
  29.         End With
  30.  
  31.         Set rs = New ADODB.Recordset       'Creates record set
  32.         strSQL = "Insert Into TrainerSchedule (TrainerID,EventID,Roleid,Statusid,TrainerSOWid,TrainerTravelid, TrainerAccomid) " _
  33.                 & "Select TrainerID, EventID, 71, 21,76,80,84 From FindTrainerforEvent Where Eventid = " & [EventID]
  34.  
  35.         rs.Open strSQL, cn, strDBCursorType, strDBLockType, strDBOptions
  36.  
  37.         rs.Close
  38.         Set rs = Nothing
  39.         cn.Close
  40.         Set cn = Nothing
  41.  
  42.     End If
  43.  
  44. Exit_InsertTrainers_Click:
  45.     Exit Sub
  46.  
  47. Err_InsertTrainers_Click:
  48.     MsgBox Err.Description
  49.     Resume Exit_InsertTrainers_Click
  50.  
  51. End Sub
  52.  
  53. Private Function ConnectString() As String
  54. Dim strServerName As String
  55. Dim strDatabaseName As String
  56. Dim strUserName As String
  57. Dim strPassword As String
  58.  
  59.     'Change to IP Address if not on local machine
  60.     'Make sure that you give permission to log into the
  61.     'server from this address
  62.     'See Adding New User Accounts to MySQL
  63.     'Make sure that you d/l and install the MySQL Connector/ODBC 3.51 Driver
  64.  
  65. strServerName = "localhost"
  66. strDatabaseName = "EventPlanner"
  67. strUserName = "XXXXXX"
  68. strPassword = "XXXXXXX"
  69.  
  70. ConnectString = "DRIVER={MySQL ODBC 3.51 Driver};" & _
  71.                 "SERVER=" & strServerName & _
  72.                 ";DATABASE=" & strDatabaseName & ";" & _
  73.                 "USER=" & strUserName & _
  74.                 ";PASSWORD=" & strPassword & _
  75.                 ";OPTION=3;"
  76.  
  77. End Function
Sep 11 '08 #1
9 13433
Krandor
50
If it works when you do a select and not when you do an insert, then it may be a permissions issue.

Check the permissions for the login you are using and make sure that it has insert rights to the table.
Sep 11 '08 #2
If it works when you do a select and not when you do an insert, then it may be a permissions issue.

Check the permissions for the login you are using and make sure that it has insert rights to the table.
Unfortunately this is not the case - I have Select, Insert and Update privileges granted
Sep 11 '08 #3
FishVal
2,653 Expert 2GB
Try to use ADODB.Connection.Execute method instead of ADODB.Recordset.Open.

Regards,
Fish
Sep 11 '08 #4
Try to use ADODB.Connection.Execute method instead of ADODB.Recordset.Open.

Regards,
Fish
Works Perfectly! Thanks Fish

Paul
Sep 11 '08 #5
ADezii
8,834 Expert 8TB
If it works when you do a select and not when you do an insert, then it may be a permissions issue.

Check the permissions for the login you are using and make sure that it has insert rights to the table.
  1. To expand on FishVal's excellent point:
    Expand|Select|Wrap|Line Numbers
    1. It is not a good idea to use the Source argument of the Open method to perform an action query that doesn’t return records because there is no easy way to determine whether the call succeeded. The Recordset returned by such a query will be closed. Call the Execute method of a Command object or the Execute method of a Connection object instead to perform a query that, such as a SQL INSERT statement, that doesnt return records.
  2. To be perfectly honest, I feel as though a code revision is in order, since many Variable Declarations are incorrect (Cursor Type, Lock type, and the Options Parameter are not Strings), and the Return Value from the Msgbox Function is also not a String. Code syntax can also be improved on, as well as the overall application logic. I posted just a couple of suggestions for starters:
    Expand|Select|Wrap|Line Numbers
    1. Dim rs As ADODB.Recordset
    2. Dim strSQL As String
    3.  
    4. Dim intResponse As Integer
    5.  
    6. strMsg = "Do you want to add this event to all the trainers schedule?"
    7.  
    8. 'Get confirmation of the insert into the trainers schedule.
    9. intResponse = MsgBox(strMsg, vbOKCancel)
    10.  
    11. strSQL = "Blah, Blah, Blah..."
    12.  
    13. Set rs = New ADODB.Recordset
    14.  
    15. With rs
    16.   .Source = strSQL
    17.   .ActiveConnection = cn    'previously defined
    18.   .CursorType = adOpenDynamic
    19.   .LockType = adLockOptimistic
    20.     .Open
    21. End With
Sep 11 '08 #6
NeoPa
32,556 Expert Mod 16PB
Paul,
Please remember when posting code that you are expected to use the [ CODE ] tags provided.

This makes the code much easier to read and saves others the effort of adding them for you later.

NeoPa (Admin).

PS. I do understand you're new to the site.
Sep 11 '08 #7
  1. To expand on FishVal's excellent point:
    Expand|Select|Wrap|Line Numbers
    1. It is not a good idea to use the Source argument of the Open method to perform an action query that doesn’t return records because there is no easy way to determine whether the call succeeded. The Recordset returned by such a query will be closed. Call the Execute method of a Command object or the Execute method of a Connection object instead to perform a query that, such as a SQL INSERT statement, that doesnt return records.
  2. To be perfectly honest, I feel as though a code revision is in order, since many Variable Declarations are incorrect (Cursor Type, Lock type, and the Options Parameter are not Strings), and the Return Value from the Msgbox Function is also not a String. Code syntax can also be improved on, as well as the overall application logic. I posted just a couple of suggestions for starters:
    Expand|Select|Wrap|Line Numbers
    1. Dim rs As ADODB.Recordset
    2. Dim strSQL As String
    3.  
    4. Dim intResponse As Integer
    5.  
    6. strMsg = "Do you want to add this event to all the trainers schedule?"
    7.  
    8. 'Get confirmation of the insert into the trainers schedule.
    9. intResponse = MsgBox(strMsg, vbOKCancel)
    10.  
    11. strSQL = "Blah, Blah, Blah..."
    12.  
    13. Set rs = New ADODB.Recordset
    14.  
    15. With rs
    16.   .Source = strSQL
    17.   .ActiveConnection = cn    'previously defined
    18.   .CursorType = adOpenDynamic
    19.   .LockType = adLockOptimistic
    20.     .Open
    21. End With

Thanks for the advice ADezii - will update my code.

Cheers

Paul
Sep 12 '08 #8
Paul,
Please remember when posting code that you are expected to use the [ CODE ] tags provided.

This makes the code much easier to read and saves others the effort of adding them for you later.

NeoPa (Admin).

PS. I do understand you're new to the site.

Doh! Sorry stupid Newbie mistake.

Paul
Sep 12 '08 #9
NeoPa
32,556 Expert Mod 16PB
No biggie :)

You don't come across as a newbie regardless of your current post-count. I doubt we'll be seeing that again from you Paul.
Sep 12 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Matthew Louden | last post by:
The following ASP code yields the following error, but actually the new record is stored in database. The same error happens when the application deletes a record, such as sqlStmt ="delete from...
1
by: cliverama | last post by:
help! fried brains.... asp calling a sqlserver7 stored proc which dynamically builds a sqlstatement & passes it to sp_executesql asp page gives the operation not allowed when object is closed...
1
by: | last post by:
So, I am getting this really annoying error.. the SQL Statement executes perfectly in Query Analyzer.. all I am doing is basically duplicating a row (insert into select from) from one table back...
2
by: Arpan | last post by:
An ASP application updates a SQL Server database table after a Form is submitted after which the user is directed to another page that should display the updated records. If the code is ...
1
by: amitbadgi | last post by:
Hello guys I am getting this error while converting an asp applicayion to asp.net Exception Details: System.Runtime.InteropServices.COMException: Operation is not allowed when the object is...
1
by: Matt | last post by:
Got this problem that killing me...what am i doing wrong with this old dll. When i call the function from asp i get "Operation is not allowed when object is closed" any clue will be...
1
by: jigu | last post by:
<html> <head><title>testing connection asp to mdb</title></head> <body> <% dim sql_insert,sSQL src = "C:\websites1\yourdominname\mydatabase.mdb" sConnStr =...
5
by: Sonasang | last post by:
Hi , I am creating a web page with ASP and Javascript.We have shared the foldres containg the code and all our team members are accessing the code. There is no problem for me when i run the...
1
by: govind161986 | last post by:
Dear All, I have a stored procedure in which I have used a temporary table which selects the value from two tables and display the result. When I run the stored procedure from Sql's query analyzer...
1
by: sailoosha | last post by:
Hi, I am getting an error "operation not allowed when object is closed" when executing the following code. I am getting the runtime error at While Recordset.EOF <> True I am not sure where the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.