Solved

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

Posted on 2013-01-17
3
792 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
[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 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

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