Solved

Accessing SQL server tables through VBA

Posted on 2004-09-29
11
710 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 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 12179404
try

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

Expert Comment

by:Rey Obrero
ID: 12179417
too slow
0
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now