473,397 Members | 1,949 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,397 software developers and data experts.

Using Recordsets

102 64KB
Background:
Front end: Access 2010
Back end: SQL Server 2008



I use the following code to write to a table and it works fine. I change the table name from "tbl_XX02003" to "dbo_XX02033" (line 16). The later is a back end table on SQL Server 2008. After I do this I get the following error: Run time error '3219": Invalid Operation. All I did was change the table name. Do I need something else since "dbo_XX02003" is a back end table?

Expand|Select|Wrap|Line Numbers
  1. Dim supplierID As Integer
  2. Dim key As String
  3. Dim typex As String
  4. Dim ratingperiod As String
  5. Dim ratingdate As Date
  6. Dim applevel As String
  7.  
  8. Dim rst As DAO.Recordset
  9.  
  10. supplierID = 11
  11. key = "129FY2Q"
  12. ratingperiod = "FY292Q"
  13. ratingdate = Date
  14. applevel = "Lock"
  15.  
  16. Set rst = CurrentDb.OpenRecordset("tbl_XX02003", dbOpenTable)
  17. rst.AddNew
  18. rst!supplierID = supplierID
  19. rst!key = key
  20. rst!Type = typex
  21. rst!ratingperiod = ratingperiod
  22. rst!ratingdate = ratingdate
  23. rst!applevel = applevel
  24.  
  25. rst.Update
  26. rst.Close
  27. Set rst = Nothing
  28.  
  29. MsgBox "Insert was successful.", 48, ""
Aug 21 '14 #1

✓ answered by jforbes

Hey sc5502,
When opening a Recordset on a SQL table versus an Access table things can get hairy. My guess as to the problem you are running in to has to do with the dbSeeChanges option of the recordset. Maybe try these changes and see if it works?
Expand|Select|Wrap|Line Numbers
  1. 16. Set rst = CurrentDb.OpenRecordset("dbo_XX02003", dbOpenTable, dbSeeChanges)
  2.  
Or
Expand|Select|Wrap|Line Numbers
  1. 16. Set rst = CurrentDb.OpenRecordset("SELECT * FROM dbo_XX02003", dbOpenDynaset, dbSeeChanges)
  2.  
Another thing that might help is switching to using an Execute Statement when you can. You could replace line 16-27 with the following and it should run pretty fast:
Expand|Select|Wrap|Line Numbers
  1. Dim sSQL As String
  2. sSQL = ""
  3. sSQL = sSQL & "INSERT INTO dbo_XX02003 ("
  4. sSQL = sSQL & " supplierID"
  5. sSQL = sSQL & ", key"
  6. sSQL = sSQL & ", Type"
  7. sSQL = sSQL & ", ratingperiod"
  8. sSQL = sSQL & ", ratingdate"
  9. sSQL = sSQL & ", applevel"
  10. sSQL = sSQL & ") VALUES ("
  11. sSQL = sSQL & "  " & supplierID & ""
  12. sSQL = sSQL & ", '" & key & "'"
  13. sSQL = sSQL & ", '" & typex & "'"
  14. sSQL = sSQL & ", '" & ratingperiod & "'"
  15. sSQL = sSQL & ", '" & ratingdate & "'"
  16. sSQL = sSQL & ", '" & applevel & "'"
  17. sSQL = sSQL & ")"
  18. DoCmd.SetWarnings False
  19. CurrentDB.Execute sSQL, dbFailOnError + dbSeeChanges
  20. DoCmd.SetWarnings True
  21.  

5 2044
twinnyfo
3,653 Expert Mod 2GB
sc5502,

I do not know the cause, but I can only assume that it is because of it being a SQL Server table. There may require certain connnections to the DB, which, I must admit, I am not an expert on--however, I am posting on this thread, as I woul dlike to learn and understand more about SQL Server backends, as we may some day move in that direction.

I hope the other experts here have some ideas on solving your problem.
Aug 21 '14 #2
Seth Schrock
2,965 Expert 2GB
With the method you are using, you can only open tables that are linked within Access and you must use the name as it appears in Access. I also use the dbOpenDynaset and dbSeeChanges options when opening a recordset.

There are ways to view data in a table that isn't linked, but none of the ways that I know of allow you to add or edit records.
Aug 21 '14 #3
jforbes
1,107 Expert 1GB
Hey sc5502,
When opening a Recordset on a SQL table versus an Access table things can get hairy. My guess as to the problem you are running in to has to do with the dbSeeChanges option of the recordset. Maybe try these changes and see if it works?
Expand|Select|Wrap|Line Numbers
  1. 16. Set rst = CurrentDb.OpenRecordset("dbo_XX02003", dbOpenTable, dbSeeChanges)
  2.  
Or
Expand|Select|Wrap|Line Numbers
  1. 16. Set rst = CurrentDb.OpenRecordset("SELECT * FROM dbo_XX02003", dbOpenDynaset, dbSeeChanges)
  2.  
Another thing that might help is switching to using an Execute Statement when you can. You could replace line 16-27 with the following and it should run pretty fast:
Expand|Select|Wrap|Line Numbers
  1. Dim sSQL As String
  2. sSQL = ""
  3. sSQL = sSQL & "INSERT INTO dbo_XX02003 ("
  4. sSQL = sSQL & " supplierID"
  5. sSQL = sSQL & ", key"
  6. sSQL = sSQL & ", Type"
  7. sSQL = sSQL & ", ratingperiod"
  8. sSQL = sSQL & ", ratingdate"
  9. sSQL = sSQL & ", applevel"
  10. sSQL = sSQL & ") VALUES ("
  11. sSQL = sSQL & "  " & supplierID & ""
  12. sSQL = sSQL & ", '" & key & "'"
  13. sSQL = sSQL & ", '" & typex & "'"
  14. sSQL = sSQL & ", '" & ratingperiod & "'"
  15. sSQL = sSQL & ", '" & ratingdate & "'"
  16. sSQL = sSQL & ", '" & applevel & "'"
  17. sSQL = sSQL & ")"
  18. DoCmd.SetWarnings False
  19. CurrentDB.Execute sSQL, dbFailOnError + dbSeeChanges
  20. DoCmd.SetWarnings True
  21.  
Aug 21 '14 #4
zmbd
5,501 Expert Mod 4TB
That last block of code could be cleaned up in VBA:

Expand|Select|Wrap|Line Numbers
  1. '<air code>
  2. '
  3. Dim supplierID As Integer 
  4. Dim key As String 
  5. Dim typex As String 
  6. Dim ratingperiod As String 
  7. Dim ratingdate As Date 
  8. Dim applevel As String
  9. '
  10. Dim sSQL As String
  11. '
  12. 'something needs to set the values for key, typex, etc...
  13. '
  14. sSQL = "INSERT INTO dbo_XX02003 (" & _
  15.     "supplierID" & _
  16.     ", key" & _
  17.     ", Type" & _
  18.     ", ratingperiod" & _
  19.     ", ratingdate" & _
  20.     ", applevel)" & _
  21.     " VALUES (" & _
  22.       supplierid & _
  23.     ", '" & Key & "'" & _
  24.     ", '" & typex & "'" & _
  25.     ", '" & ratingperiod & "'" & _
  26.     ", '" & ratingdate & "'" & _
  27.     ", '" & applevel & "')"
  28. '
  29. 'CurrentDb.Execute sSQL, dbFailOnError + dbSeeChanges
  30.  
Removed all of the string adding steps and this could be done with fewer lines; however, IMHO, Jforbes has the correct breaking for easy reading.

Also, there is no need to turn off the warnings when useing an execute statement, and indeed the ability to trap the error of the execute method is an advantage.
Aug 21 '14 #5
sc5502
102 64KB
This one worked
Expand|Select|Wrap|Line Numbers
  1. Set rst = CurrentDb.OpenRecordset("SELECT * FROM dbo_XX02003", dbOpenDynaset, dbSeeChanges)
.
Aug 22 '14 #6

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

Similar topics

19
by: bdt513 | last post by:
I am trying to extract the values from a query using VBA. Specifically, I want to concatenate all the values of the "rosEmail" field from query "qselRosterEmailList" into one string (strEmails). I...
1
by: lakshmi | last post by:
Hi all, I recently rewrote a data intensive C++ program in C#. The C++ program was traversing 3 recordsets that were all open at the same time. I replaced those 3 recordsets with 3 .NET data...
16
by: Randy Harris | last post by:
I was inspired by the recent discussion of returning multiple recordsets to ADO from a stored procedure. (Amazed is probably more accurate). I asked about how to accomplish same with Oracle and...
24
by: Donald Grove | last post by:
I want to populate an array with values from an ado recordset (multiple rows) I use the absolute position of the cursor in the recordset to define the row of my array to be populated. I have a...
10
by: Roger Withnell | last post by:
I'm using ASP, VBScript and SQL Server. I'm also using UTF-8 character set and so my codepage is 65001 and SQL Server datatype nvarchar. I can insert unicode characters correctly into the...
1
by: Atul | last post by:
Hi Friends, I want to call a function on a button click . In that function , I will be dealing with the recordsets(for inserting and deleting data from the tables). But when I tried...
4
by: psuaudi | last post by:
I am trying to use a recordset for the first time with a query as it’s source. I am having trouble getting it to work. I've been quite sucessful with other VB scripting. My motivation for doing...
14
gcoaster
by: gcoaster | last post by:
Hello, * Access 2007 * Little Experience with VBA I will try and make this as clear as I can. ISSUE I have a database that has information for Clients and their Computers.
7
by: MLH | last post by:
If I'm using the following in a procedure... DoCmd.GoToRecord acDataForm, "FormName", acNext, 4 .... how can I recognize the EOF condition? Using GoToRecord, I find myself lost when trying to...
26
by: enigma19 | last post by:
Hi , I need help to solve this problem Problem definition: I have a table 'tblItemOrdQty' with item name and its order quantities (Lot1,Lot2,Lot3.... etc). Each items are ordered in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.