I am having a hard time filling a recordset with data from an MSSQL 2012 server using ADODB.
Below is the code that is not working, along with the error I am receiving.
I have used this exact same setup to query data from an MSSQL 2008r2 server without issue.
I know that my SELECT statement is accurate and correct. If I take my select statement and paste it into a new query window in sql server management studio, it returns data.
Dim mConnection
Set mConnection = CreateObject("ADODB.Connection")
mConnection.Open "Provider=SQLOLEDB.1;Data Source=rwdwstage","SA","PASSWORDREDACTED"
Set Tech_RS = CreateObject("ADODB.Recordset")
SQLstr = "SELECT QDSTCD, QDAREA, QDBANK FROM [RWDATA].[dbo].[BRDETAIL] where QDSDT = '20151215' and QDSTCD = 'WI'"
Tech_RS.open SQLstr, mConnection
Do while not Tech_RS.EOF
wscript.echo Tech_RS.Fields("QDSTCD") & " - " & Tech_RS.Fields("QDAREA") & " - " & Tech_RS.Fields("QDBANK")
Tech_RS.MoveNext
Loop
Error Returned
cscript ..\HIST_Transfer\test2.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
..\HIST_Transfer\test2.vbs(11, 1) Microsoft OLE DB Provider for SQL Server: Incorrect syntax near the keyword 'FROM'.
Any help that could be provided would be extremely appreciated!
You can add Initial Catalog to connection string:
mConnection.Open "Provider=SQLOLEDB.1;Data Source=rwdwstage;Initial Catalog=RWDATA","SA","PASSWORDREDACTED"
and change query to:
SQLstr = "SELECT QDSTCD, QDAREA, QDBANK FROM [dbo].[BRDETAIL] where QDSDT = '20151215' and QDSTCD = 'WI'"
I guess that it worked before because the default database, for login you use for connecting, was RWDATA and with newer version it points to another DB (you can try to change default DB for that login and check if it works, be aware that changing it may broke other application that relies on default DB)
Related
Note that both databases are MS SQL Server.
The SELECT works fine and the code doesn't break until it gets to ADODB.Recordset.Update. The SQL account has all of the necessary permissions. The table [NASMSPAINT].[Ignition].[dbo].[booth_Styles] is a linked server. The User account I am using has enough permissions because I am able to UPDATE the table using Python. This is on a secure isolated network so security is of very little concern, this just needs to work using VB6 with ADO. Long story short, this code is part of a large application still using VB6 and rewriting the code in Visual Studio is not an option.
Using ADODB.Recordset.OPEN using adLockOptimistic option, the following error occurs on the ".Update" line of the code:
SQL server error message 16964 - for the optimistic cursor, timestamp columns are required if the update or delete targets are remote.
Using ADODB.Recordset.OPEN using adLockPessimistic option, the following error occurs on the ".Update" line of the code:
SQL Server Error Msg 16963 – You cannot specify scroll locking on a cursor that contains a remote table.
I have found very little information on the internet concerning these errors. I have set the following server option properties on the linked server on the database:
Collation Compatible: TRUE
Data Access:TRUE
RPC:TRUE
RPC Out:TRUE
Use Remote Collation:FALSE
Collation Name:
Connection Timeout:0
Query Timeout:0
Distributor:FALSE
Publisher:FALSE
Subscriber:FALSE
Lazy Schema Validation:FALSE
Enable Promotion of Distributed Transaction:TRUE
VB6 code:
sDBName = "PROVIDER=SQLOLEDB.1;Data Source=192.168.2.70;User ID=xxxx;Password=xxxx;Persist Security Info=False"
Dim Conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set Conn = New ADODB.Connection
Conn.Open sDBName
Set rs = New ADODB.Recordset
With rs
.Open "SELECT * FROM [NASMSPAINT].[Ignition].[dbo].[booth_Styles] WHERE [Booth] = 'AdPro' ORDER BY [StyleID]", Conn, adOpenDynamic, adLockOptimistic
.MoveFirst
nThisStyle = 1
Do Until .EOF
![Plant_Number] = Style_Data(nThisStyle).PlantStyle
![Style_Number] = Style_Data(nThisStyle).FanucStyle
![Descript] = Style_Data(nThisStyle).StyleDesc
![Robots_Required] = Style_Data(nThisStyle).StyleRobotsReq
.Update
.MoveNext
nThisStyle = nThisStyle + 1
Loop
End With
The code breaks on the .Update line.
My current project is troubleshooting our program and server performance and I got stuck on this:
Whenever I run a query in Access I get a massive amount of these errors in my log file:
Error: 18456, Severity: 14, State: 5.
Error message:
Login failed for user 'Jakob'. Reason: Could not find a login matching the name provided. [CLIENT: <named pipe>]
Weirdly enough the query resolves normally (although maybe a little slowly) and I get the expected results But my goal remains to fix this issue for the sake of deepening my understanding of the SQL Server environment and to increase performance.
Here are all the information I have gathered:
A lot of these errors occur, when opening a query and I get 1 to 2 of these errors when opening the 3 integrated tables in rapid succession. (Not when I open just 1 of them)
I have integrated 3 tables from the SQL Server on my local machine via ODBC with the following code:
Public Function ConnectTable(strTableName As String, strDateiname As String, Optional SQLSvrBo As Boolean = False, Optional strPreFix = "", Optional impBo As Boolean = False) As Boolean
On Error Resume Next
Dim DB As Database
Dim tdf As TableDef
Set DB = CurrentDb
DoCmd.DeleteObject acTable, strPreFix & strTableName
On Error GoTo Err_ConnectTable
If SQLSvrBo = False Then 'Connect to Access-Database'
Set tdf = DB.CreateTableDef(strPreFix & strTableName)
tdf.SourceTableName = strTableName
tdf.Connect = ";DATABASE=" & strDateiname
DB.TableDefs.Append tdf
ConnectTable = True
Else 'Connect to SQL-Server'
Set tdf = DB.CreateTableDef(strPreFix & strTableName)
tdf.SourceTableName = "dbo." & strTableName
tdf.Connect = "ODBC;DRIVER={" & SQLODBCDriver() & "};SERVER=" & SQLServerName() & ";DATABASE=" & SQLDatabase() & ";UID=" & SQLUID() & ";PWD=" & SQLPassword() & ";APP=Microsoft® Access;[LOGINTIMEOUT=25;]"
DB.TableDefs.Append tdf
ConnectTable = True
End If
Exit Function
'Error Handler'
End Function
On the "Frontend" we have "Query 0" which joins a local Access table with "Query 1". "Query 1" joins two of the integrated tables. The third integrated table is manipulated via a RecordSet, which writes data into "Query 0" by using a function. This function gets called 3 times per row and loops (max 3 times) if there are more than 1 entry for the corresponding record. I have triple checked the Extended Events and there are exactly an equal number of SQL-Statements and Error Messages.
I am running the newest versions of both MS Access and MS SQL-Server 2017 (EXPRESS) on my local machine, which has newest Windows 11 Updates installed.
What I have tried:
I have disabled the Named Pipes with the SQL Server Configuration Manager to no success. The Error message still mentions [Client: <named pipe>]!
"Managed" (: to replicate it on another system. But I didn't change anything from my setup, the same errors just happened to also occur there.
My guess is, that it has something to do with Access trying to connect to SQL Server via both SQL Server Authentication (which is obviously working, thanks to my flawless Copy-Paste-Skills) and Windows Authentication.
I'm executing a query using ADO:
Dim connLocal As ADODB.Connection
Set connLocal = CurrentProject.Connection
strSQL = "INSERT INTO dbo_tbl_ErrorLog SELECT tbl_ErrorLog.* FROM tbl_ErrorLog;"
On Error GoTo ErrorSQL
connLocal.Execute strSQL
.....
ErrorSQL:
lErrNo = Err.Number
strErrDesc = Err.Description
If InStr(1, strErrDesc, "ODBC") Then
Dim i As Long
Dim strErr As String
For i = 0 To connLocal.Errors.Count - 1
strErrDesc = strErrDesc & vbCrLf & connRemote.Errors(i).Number & " - " & connLocal.Errors(i).Description
Next i
End If
.....
In the query the table dbo_tbl_ErrorLog is linked MS SQL table, tbl_ErrorLog - linked MS Access table.
In case of SQL query error I receive an error:
-2147467259 - ODBC--call failed.
And I when I'm trying to get error details, the collection connLocal.Errors contains just one item "ODBC--call failed". Is it possible to retrieve full error details in such case of mixed query?
Through ADO, unfortunately, there's not as far as I know.
The Access database engine doesn't propagate errors from ODBC data sources to ADO.
Your ADO connection is one to the Access Database Engine, and only receives the errors it raises.
If you need to trace this specific error, you can do so by enabling ODBC tracing for the ODBC connection you're using to connect Access to SQL server. The trace log should include any errors thrown by SQL Server, and also the queries Access uses to move the data to SQL server.
If you have sufficient permissions, you can also query the error log with sp_readerrorlog after the error gets raised, either using a pass-through query or separate ADO connection to SQL Server.
I am using a VB6 application. Which was using the MSAccess database. Now i am changing the database access to sql server. to getting/updating the data from MSAccess the application was using the DAO object. So now i am also trying to connect SQL Server with DAO method. Now i am able to connect the database and also able to get the data. But when i am trying to Edit record it is giving the error "Runtime error '3027' cannot update. Database or object is read only". i am showing my code here:
Public LSWs As Workspace, LSDb As Database
Dim lsConnString As String
Dim l0 As Recordset, SQL0 As String
Dim lehReturn As Integer, retrycount As Integer
lsConnString = "ODBC;DRIVER=SQL Server;SERVER=SERVERName;DATABASE=" & DBname & ";APP=Visual Basic;UID=UID;PWD=PWD"
Set LSWs = DBEngine.Workspaces(0)
Set LSDb = LSWs.OpenDatabase(DBname, dbDriverNoPrompt, True, lsConnString)
retrycount = 0
SQL0 = "select * from schedule_hdr where status = '" & Trim(PCName) & "'"
Set l0 = LSDb.OpenRecordset(SQL0, dbOpenDynaset, dbSeeChanges, adLockPessimistic)
Do While Not (l0.EOF)
LSWs.BeginTrans
l0.Edit
l0!status = "R"
l0.Update
LSWs.CommitTrans
l0.MoveNext
Loop
l0.Close
but it is giving the error at l0.Edit this line code
giving the error
"Runtime error '3027' cannot update. Database or object is read only
Any Help! or suggesion please reply
Thanks in Advance!
Initially I'm thinking that the username and password you are using for the SQL server may possibly have read-only rights.
If this is not the case and you want to stick with DAO then I suggest using the built-in ODBC link feature of Access as this helps you connect successfully to your SQL server.
However, my advice has to be abandoning DAO and going with ADO, mainly because DAO restricts your data processing to client side which is slow, and the whole point of having a dedicated server is to run as much as you can server side to dramatically improve performance.
this is pretty strange.
I've got this string that connect to a SQLServer in the same domain as the computers are running and compares username with employeeID. Then takes that row and dumps it into the lokal computers registry. This is working in Windows XP, but not Windows 7 it seems.
I get this exact error message:
Line:39
Char:1
Error: [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
Code: 80004005
Source: Microsoft OLE DB Provider for SQL Server.
This is the script itself. I've removed the actual servernames. Reckon nobody needs those.
Set oConn = CreateObject("ADODB.Connection")
oConn.Open "Provider=sqloledb;Data Source=mysqlserver04\mysqlserver04;Initial Catalog=orginfo;Integrated Security=SSPI"
sSQL = "select top 1 * from dbo.Mal_personinfo where empid = '" & EID & "'"
'wscript.echo sSQL
set rs = oConn.Execute(sSQL)
set oSystem = CreateObject("WScript.Shell")
for iTeller = 0 to rs.fields.count - 1
Text = Text & rs.fields(iTeller).Name & "=" & rs.fields(iTeller).Value & " - "
oSystem.RegWrite "HKCU\Software\MalData\" & rs.fields(iTeller).Name,rs.fields(iTeller).Value,"REG_SZ"
next
'wscript.echo Text
Why does this work on Windows XP but not Windows 7?
Since you're using integrated security, let me ask you about the account that is running the application. Did you add that account as a user to the database? If not, and it's an admin account, you're probably relying on the old "admins can do everything" and under Windows 7 that's not true any more.
To test this, try running the application elevated (right click the exe and Run As Administrator.) This will cause it to keep your "admin-ness" and might let it into the database. If that works, don't continue to run it elevated, but instead go to SQL and add yourself as a user. Then the app should work fine non-elevated.