Solved

Accessing SQL server tables through VBA

Posted on 2004-09-29
11
745 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

734 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