Solved

Accessing SQL server tables through VBA

Posted on 2004-09-29
11
748 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

724 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