Solved

Accessing SQL server tables through VBA

Posted on 2004-09-29
11
741 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

756 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