Solved

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

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

777 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