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? - Dim supplierID As Integer
-
Dim key As String
-
Dim typex As String
-
Dim ratingperiod As String
-
Dim ratingdate As Date
-
Dim applevel As String
-
-
Dim rst As DAO.Recordset
-
-
supplierID = 11
-
key = "129FY2Q"
-
ratingperiod = "FY292Q"
-
ratingdate = Date
-
applevel = "Lock"
-
-
Set rst = CurrentDb.OpenRecordset("tbl_XX02003", dbOpenTable)
-
rst.AddNew
-
rst!supplierID = supplierID
-
rst!key = key
-
rst!Type = typex
-
rst!ratingperiod = ratingperiod
-
rst!ratingdate = ratingdate
-
rst!applevel = applevel
-
-
rst.Update
-
rst.Close
-
Set rst = Nothing
-
-
MsgBox "Insert was successful.", 48, ""
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? - 16. Set rst = CurrentDb.OpenRecordset("dbo_XX02003", dbOpenTable, dbSeeChanges)
-
Or - 16. Set rst = CurrentDb.OpenRecordset("SELECT * FROM dbo_XX02003", dbOpenDynaset, dbSeeChanges)
-
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: - Dim sSQL As String
-
sSQL = ""
-
sSQL = sSQL & "INSERT INTO dbo_XX02003 ("
-
sSQL = sSQL & " supplierID"
-
sSQL = sSQL & ", key"
-
sSQL = sSQL & ", Type"
-
sSQL = sSQL & ", ratingperiod"
-
sSQL = sSQL & ", ratingdate"
-
sSQL = sSQL & ", applevel"
-
sSQL = sSQL & ") VALUES ("
-
sSQL = sSQL & " " & supplierID & ""
-
sSQL = sSQL & ", '" & key & "'"
-
sSQL = sSQL & ", '" & typex & "'"
-
sSQL = sSQL & ", '" & ratingperiod & "'"
-
sSQL = sSQL & ", '" & ratingdate & "'"
-
sSQL = sSQL & ", '" & applevel & "'"
-
sSQL = sSQL & ")"
-
DoCmd.SetWarnings False
-
CurrentDB.Execute sSQL, dbFailOnError + dbSeeChanges
-
DoCmd.SetWarnings True
-
5 2044
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.
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.
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? - 16. Set rst = CurrentDb.OpenRecordset("dbo_XX02003", dbOpenTable, dbSeeChanges)
-
Or - 16. Set rst = CurrentDb.OpenRecordset("SELECT * FROM dbo_XX02003", dbOpenDynaset, dbSeeChanges)
-
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: - Dim sSQL As String
-
sSQL = ""
-
sSQL = sSQL & "INSERT INTO dbo_XX02003 ("
-
sSQL = sSQL & " supplierID"
-
sSQL = sSQL & ", key"
-
sSQL = sSQL & ", Type"
-
sSQL = sSQL & ", ratingperiod"
-
sSQL = sSQL & ", ratingdate"
-
sSQL = sSQL & ", applevel"
-
sSQL = sSQL & ") VALUES ("
-
sSQL = sSQL & " " & supplierID & ""
-
sSQL = sSQL & ", '" & key & "'"
-
sSQL = sSQL & ", '" & typex & "'"
-
sSQL = sSQL & ", '" & ratingperiod & "'"
-
sSQL = sSQL & ", '" & ratingdate & "'"
-
sSQL = sSQL & ", '" & applevel & "'"
-
sSQL = sSQL & ")"
-
DoCmd.SetWarnings False
-
CurrentDB.Execute sSQL, dbFailOnError + dbSeeChanges
-
DoCmd.SetWarnings True
-
zmbd 5,501
Expert Mod 4TB
That last block of code could be cleaned up in VBA: - '<air code>
-
'
-
Dim supplierID As Integer
-
Dim key As String
-
Dim typex As String
-
Dim ratingperiod As String
-
Dim ratingdate As Date
-
Dim applevel As String
-
'
-
Dim sSQL As String
-
'
-
'something needs to set the values for key, typex, etc...
-
'
-
sSQL = "INSERT INTO dbo_XX02003 (" & _
-
"supplierID" & _
-
", key" & _
-
", Type" & _
-
", ratingperiod" & _
-
", ratingdate" & _
-
", applevel)" & _
-
" VALUES (" & _
-
supplierid & _
-
", '" & Key & "'" & _
-
", '" & typex & "'" & _
-
", '" & ratingperiod & "'" & _
-
", '" & ratingdate & "'" & _
-
", '" & applevel & "')"
-
'
-
'CurrentDb.Execute sSQL, dbFailOnError + dbSeeChanges
-
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.
This one worked - Set rst = CurrentDb.OpenRecordset("SELECT * FROM dbo_XX02003", dbOpenDynaset, dbSeeChanges)
.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |