Solved

Error 3251 - Trying to edit SQL using VBA code and ADODB connection

Posted on 2013-01-17
3
791 Views
Last Modified: 2013-01-24
I'm trying to edit my SQL but can't find the right method. All I'm getting is an 3251 error. Hope for some help.

Here is the code I'm trying to use:

Sub TestWriteSQL()

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim ClientID As String

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.ConnectionString = "Provider=sqlncli; Data Source=MYSERVER; Initial Catalog=Test; Trusted_Connection=yes;"
cn.Open

ClientID = 7555

Set rs = cn.Execute("SELECT CustomFieldValue.Value, CustomRecordset.RecordId, CustomFieldValue.CustomFieldId FROM CustomFieldValue INNER JOIN CustomRecordset ON CustomFieldValue.CustomRecordsetId = CustomRecordset.Id WHERE CustomRecordset.RecordId = '" & ClientID & "' And (CustomFieldValue.CustomFieldId = 296)")

'rs.Edit
rs!Value = 22
rs.Update

Set rs = Nothing
cn.Close

End Sub

Open in new window

0
Comment
Question by:Zoodiaq
3 Comments
 
LVL 2

Accepted Solution

by:
MMTadmin earned 250 total points
ID: 38789710
cn.execute is read only recordset by design

http://msdn.microsoft.com/en-us/library/windows/desktop/ms675023%28v=vs.85%29.aspx

You need to declare a recordset and then use it

recordset.Open Source, ActiveConnection, CursorType, LockType, Options
0
 

Author Comment

by:Zoodiaq
ID: 38791977
Thx for the link.

I'm a beginner in this area. Could you provide me with an example where I change the CustomFieldValue.Value to 22? If I could get that to work I think I could move on from there.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 250 total points
ID: 38795279
Something like this should prove the most efficient way to change that value:
Sub TestWriteSQL()

Dim cn As ADODB.Connection
Dim ClientID As String

Set cn = New ADODB.Connection

cn.ConnectionString = "Provider=sqlncli; Data Source=MYSERVER; Initial Catalog=Test; Trusted_Connection=yes;"
cn.Open

ClientID = "7555"   ' ClientID is a String and therefore the value assigned should be in double quotes.

cn.Execute "UPDATE cfv SET Value = 22 FROM CustomFieldValue cfv INNER JOIN CustomRecordset cr ON cfv.CustomRecordsetId = cr.Id WHERE cr.RecordId = '" & ClientID & "' AND cfv.CustomFieldId = 296"

cn.Close

End Sub

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

808 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