Update Error on VBA .Execute on ADODB Recordset - sql

I am trying to update a SQL DB table through the input from my textbox. The issue I am having can be seen with the generic VBA Sub seen below:
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Dim strSql As String
Dim strADOCon As String
strADOConTechSub = "PROVIDER=SQLOLEDB;Data Source=______;Initial Catalog=______;User ID = _______; Password = ______;Trusted_Connection=Yes"
conn.Open strADOCon
strSql = "SELECT this " & _
"FROM there " & _
"WHERE that <> ''"
Set rst = conn.Execute(strSql)
rst!this.Value = Me.Textbox1
conn.Close
I am receiving the error message;
Run-Time error '3251':
Current Recordset does not support updating. This may be a limitation
of the provider, or of the selected locktype.
Any help would be greatly appreciated.

Related

VBA to export MS Access table into Oracle table

I am trying to insert ms access table into Oracle table using the below code. I get an error "table or view does not exist" at cn.Execute strSql line. I think the code is unable to identify the access table name. Any help is greatly appreciated. Thanks!
Function test()
Dim cn As New ADODB.Connection
Dim rs As DAO.Recordset
Dim strSql As String
Dim db As DAO.Database
Dim ssqlFields As String
Dim i As Integer
Dim ssql As String
Set db = CurrentDb
Set cn = New ADODB.Connection
Set rs = db.OpenRecordset("SELECT * FROM T_556_557_ORCL", dbOpenDynaset)
cn.Open ( _
"User ID = XXXX" & _
";Password=YYYY" & _
";Data Source=ABCD" & _
";Provider=OraOLEDB.Oracle")
cn.CommandTimeout = 300
strSql = "INSERT INTO TDMTRDOP.TEST SELECT Ent_Ex_Summ,QTY FROM T_556_557_ORCL"
cn.Execute strSql
Set rs = Nothing
Set cn = Nothing
End Function

SQL Query Returns empty Recordset

I'm trying to learn how to connect to a SQL Server DB from Excel DB. I've tried to reduce the code to dead simple to begin with. I've looked at several answers to related questions, however, I cannot figure out why this doesn't work. It executes all the way through. (The code shown here is somewhat anonymized.)
The query finds the database, because if the table name is invalid it throws an error. However it always returns record count = -1. I can eyeball the table in MSSMS and it has data. Same result for other tables in the DB.
Public Sub ADOtest1()
Dim Conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConnString As String
strConnString = "Provider='SQLOLEDB'" & ";" & _
"Data Source='XXX-XPS\SQLEXPRESS'" & ";" & _
"Initial Catalog='XXXXX'" & ";" & _
"Integrated Security='SSPI'"
Set Conn = New ADODB.Connection
Conn.Open strConnString
' the query finds the DB, because if the table name is incorrect, it throws an error
strSQLString = "SELECT * from t300_XXXX"
Set rs = Conn.Execute(strSQLString)
wrkRecordCount = rs.RecordCount
'--- just some test breakpoints
If wrkRecordCount = -1 Then
a = "" '--- code keeps arriving here
Else
a = ""
End If
rs.Close
Conn.Close
End Sub
Answer from Srinika below worked:
Set rs = Conn.Execute(strSQLString)
rs.Close
rs.CursorLocation = adUseClient
rs.Open
I'll post two examples, so please refer.
First Example
Sub ExampleSQL()
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String
Set cnn = New ADODB.Connection
'Set the provider property to the OLE DB Provider for ODBC.
'cnn.Provider = "MSDASQL"
'cnn.Provider = "Microsoft.ACE.OLEDB.12.0"
'cnn.Provider = "MSOLAP"
'cnn.Provider = "SQLOLEDB.1"
' Open a connection using an ODBC DSN.
cnn.ConnectionString = "driver={SQL Server};" & _
"server=severname;uid=sa;pwd=password;database=test"
Set rs = New ADODB.Recordset
strSQL = "SELECT * FROM [your Table] "
rs.Open strSQL, cnn.ConnectionString, adOpenForwardOnly, adLockReadOnly, adCmdText
cnn.Open
If cnn.State = adStateOpen Then
Else
MsgBox "Sever is not connected!! "
Exit Sub
End If
If Not rs.EOF Then
With Ws
.Range("a4").CurrentRegion.ClearContents
For i = 0 To rs.Fields.Count - 1
.Cells(4, i + 1).Value = rs.Fields(i).Name
Next
.Range("a5").CopyFromRecordset rs
.Columns.AutoFit
End With
Else
MsgBox "No Data!!", vbCritical
End If
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
End Sub
Second Example
Sub getDataFromServer()
Dim con As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim i As Integer
con.ConnectionString = "Provider=SQLOLEDB.1;" _
& "Server=(local);" _
& "Database=TEST;" _
& "Integrated Security=SSPI;" _
& "DataTypeCompatibility=80;"
con.Open
Set cmd.ActiveConnection = con
cmd.CommandText = "SELECT * FROM [your Table]"
Set rs = cmd.Execute
Range("A1").CopyFromRecordset rs
con.Close
Set con = Nothing
End Sub

Trouble to display a recordset

Public Function `RecordsetSybase(sqlstr As String) As ADODB.Recordset
If TestSybaseConnection() = False Then
setLogin
End If
Dim commandObject As ADODB.Command
Set commandObject = New ADODB.Command
Dim data As New ADODB.Recordset
With commandObject
.CommandText = sqlstr
.ActiveConnection = SybaseConnection(getUID, getPASS)
.CommandTimeout = 350
End With
data.Open commandObject.CommandText, commandObject.ActiveConnection
'Do Until data.EOF = True
'MsgBox data(0)
'data.MoveNext
'Loop
commandObject.ActiveConnection.Close
Set RecordsetSybase = data
Set commandObject = Nothing
End Function
Sub classific()
Dim conn As WorkbookConnection
Dim strSQL As String
Dim rs As New ADODB.Recordset
'Query
strSQL = "SELECT DISTINCT name_short_orig, cl_rating " & _
"FROM pbsm_hist.dbo.pbsm_auths_hist_adjusted " & _
"WHERE name_short_orig IS NOT Null " & _
"AND cl_rating NOT IN ('F+','F','Z') "
Set rs = RecordsetSybase(strSQL)
MsgBox rs
End Sub
I can display the recordset in the function (see comments).
But I cannot do it in the sub, I get the following error message:
Run-time error '13':
Type mismatch
You are completely bypassing the command object, currently its just sitting there storing the command text and connection.
To actually use the command object:
set data = commandObject.execute()
As for the error, MsgBox rs is not valid - what would you expect to see as the string representation of the rs object?
If you wanted the 1st value:
if not rs.eof then msgbox rs.collect(0)

SQL query performed in VBA

I'm trying to connect Teradata Sql assistant to Excel through VBA. I'd like to write a query in VBA and print the result in a Sheet. I have gone through all the previous explanations but I still can't figure out why it's not working.
Dim strConn As String
strConn = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=TERADATA"
Dim Query As String
Query = "select * FROM P_ZC074_TMIS.FACT_TMX_PL_NII_TP_FX where CNT_ORG ='5872196'"
Dim rs As New ADODB.Recordset
rs.Open Query, strConn
Sheet1.Range("A1").CopyFromRecordset rs
I have activated the Microsoft ActiveX Data Objects 6.1 Library but
I receive a "Query Timeout Expired" error; I guess I have to use a CommandTimeout to fix this issue (assuming that it's just that) but I have no idea how to write that in VBA code.
Thanks in advance.
You need to open a connection to the database first and pass that to the recordset that you're trying to open similar to this:
Public Sub GetData()
Dim oDB As ADODB.Connection: Set oDB = New ADODB.Connection
Dim oCM As ADODB.Command: Set oCM = New ADODB.Command
Dim oRS As ADODB.Recordset: Set oRS = New ADODB.Recordset
Dim strConn As String, strQuery As String
On Error GoTo Err:
strConn = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=TERADATA"
strQuery = "select * FROM P_ZC074_TMIS.FACT_TMX_PL_NII_TP_FX where CNT_ORG ='5872196'"
oDB.Open strConn
With oCM
.ActiveConnection = oDB
.CommandType = adCmdText
Set oRS = .Execute
End With
If Not oRS.BOF And Not oRS.EOF Then
ThisWorkbook.Worksheets("Sheet1").Range("A1").CopyFromRecordset oRS
End If
Err:
On Error Resume Next
oRS.Close
Set oRS = Nothing
oDB.Close
Set oDB = Nothing
MsgBox ("An error occurred!" & vbNewLine & "Error Number: " & Err.Number _
& vbNewLine & "Error Message: " & Err.Description), vbCritical, "Error!"
End Sub

Copying ADO recordset into excel worksheet

I'm trying to open a CSV file and query it and return the results into column A of the second worksheet of "ThisWorkbook".
I'm not getting any errors so I do not see why it is not copying the record set into excel.
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim currentDataFilePath As String
Dim currentDataFileName As String
Dim nextRow As Integer
currentDataFilePath = "C:\Users\M\folder\"
currentDataFileName = "csv-file"
con.Open "Provider=Microsoft.JET.OLEDB.4.0;" & _
"Data Source=" & currentDataFilePath & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited;IMEX=1"""
'rs.ActiveConnection = con
rs.Open "SELECT Name FROM [" & currentDataFileName & ".csv] WHERE Datatype ='TYPE3'",
con
ThisWorkbook.Worksheets("Sheet2").Range("A:A").CopyFromRecordset rs
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
End Sub
You might refer to the CopyFromRecordset() method.
Based on your code above, after the rs.Open command you would add something like this:
ActiveWorksheet.Range("A1").CopyFromRecordset rs
See more here: http://msdn.microsoft.com/en-us/library/office/ff839240%28v=office.15%29.aspx