Solved

Accessing SQL server tables through VBA

Posted on 2004-09-29
11
725 Views
Last Modified: 2011-10-03
I am trying to update a SQL Server table through VBA coding which keeps erroring.  Ihave created a test example of coding that is being used:

    Dim DB As Database, stSQL As String, RS As Recordset
    Set DB = CurrentDb()
    stSQL = "Select * From tblName;"
    Set RS = DB.OpenRecordset(stSQL)
   
    RS.AddNew
        RS("[Name1]") = Me![Text1]
        RS("[Name2]") = Me![Text3]
        RS("[Name3]") = Me![Text5]
    RS.Update
I receive the error
"You must use the dbSeeChanges option with OpenRecorset when accessing a SQL Server table that has a IDENTITY column."
Thanks
Dtrain

0
Comment
Question by:DTRAIN
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 250 total points
ID: 12179387
try this change:

from:

    Dim DB As Database, stSQL As String, RS As Recordset
    Set DB = CurrentDb()
    stSQL = "Select * From tblName;"
    Set RS = DB.OpenRecordset(stSQL)
   
    RS.AddNew
        RS("[Name1]") = Me![Text1]
        RS("[Name2]") = Me![Text3]
        RS("[Name3]") = Me![Text5]
    RS.Update


to:

    Dim DB As Database, stSQL As String, RS As Recordset
    Set DB = CurrentDb()
    stSQL = "Select * From tblName;"
    Set RS = DB.OpenRecordset(stSQL,,dbSeeChanges )'  the [Options] parameter is 'optional', and is the SECOND optional parameter, hence the ,, to skip the first of the optional parameters
   
    RS.AddNew
        RS("[Name1]") = Me![Text1]
        RS("[Name2]") = Me![Text3]
        RS("[Name3]") = Me![Text5]
    RS.Update


you can also change the line:

 Set RS = DB.OpenRecordset(stSQL)

like this:

 Set RS = DB.OpenRecordset(stSQL, options:=dbSeeChanges )

both lines do EXACTLY the same thing, your choice.

AW
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 12179404
try

Set rs = db.OpenRecordset(stSQL, dbOpenDynaset, dbSeeChanges)
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 12179417
too slow
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 8

Expert Comment

by:JonoBB
ID: 12179432
I suspect that this is becasuse sqlserver is trying to incremeent the ID field, but cant becuase its being updated through a recordset.

Just to be sure, try this code (which uses ADO) via a linked table in access

Dim strSQL As String
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset

stSQL = "Select * From tblName;"

'Create connection.
    Set cnn = CurrentProject.Connection
               
'Open recordset
    With rs
        Set .ActiveConnection = cnn
        .Source = strSQL
        .LockType = adLockOptimistic
        .CursorType = adOpenForwardOnly
        .Open
        .AddNew
        ![Name1] = =me.Text1
        ![Name2] = =me.Text2
        ![Name3] = =me.Text3
        .Update
    End With

    rs.Close
    Set rs = Nothing
    cnn.Close
    Set cnn = Nothing
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 12179637

Private Sub SomeSub()

Dim stgrSQL as String
strSQL = "INSERT INTO TableName ( Name1, Name2, Name3 ) Values('" & Me![Text1] & "','" & Me![Text2] & "','" & Me![Text3] &"')"

Docmd.SetWarnings False
DoCmd.RunSQL strSQL
Docmd.SetWarnings True

Exit Sub

Alan
0
 

Author Comment

by:DTRAIN
ID: 12179730
capricorn1
Solution works great and is not slow.  This will be accepted.

JonoBB  This errored as User- Defined type not defined.

Thanks Dtrain
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12186461
The comment from capricorn1 about 'too slow' was because his answer FOLLOWED mine (which was ALMOST identical), and he was saying the he was 'too slow' in submitting his answer, since it was almost a duplicate of mine.

AW
0
 

Author Comment

by:DTRAIN
ID: 12188942
My appologies.  I did not see Arthur_Wood's comment.  Please do reopen this question so that the point can be awarded correctly.  Prior to reading capricorn1's comment I had answered my own question but since the answer came in while I was working on the problem I still awarded the points.  Since both answers are correct point can be devided among both individuals.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12205888
Thank you both.  Those were just the points I needed to go out and buy my new Jag XKE (LOL---Don't I wish)

Thanks again, for the recognition.

AW
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question