Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

895 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

17 Experts available now in Live!

Get 1:1 Help Now