I am trying to create a script that connects to sql server, selects some data, and saves the results to a csv. Windows Authentication is enabled in Sql Server.
Here's what I have so far:
Dim connect, sql, resultSet, pth, txt
Set connect = CreateObject("ADODB.Connection")
connect.ConnectionString = "Provider=SQLOLEDB;Server=server\instance;Database=pm;Trusted_Connection=True;"
connect.Open
sql="SELECT col1, col2 FROM tbl1 order by col1, col2"
Set resultSet = connect.Execute(sql)
pth = "d:\test.csv"
Set txt = fs.CreateTextFile(pth, True)
On Error Resume Next
resultSet.MoveFirst
Do While Not resultSet.eof
txt.WriteLine(resultSet(0) & "," & resultSet(1))
resultSet.MoveNext
Loop
resultSet.Close
connect.Close
Set connect = Nothing
I get error:
d:\vbs_test.vbs(5, 1) Microsoft OLE DB Provider for SQL Server: Invalid authorization specification
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.
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 have a .vbs script that runs the following sql query:
Select COUNT (*) from sys.objects
Which count the rows, from the sql query output:
https://i.stack.imgur.com/wduXW.png[1]
And if there is any rows found (> 0). genereate an alert in SCOM using the PropertyBag scripting runtime in SCOM.
Problem is,
When debugging the script (using cscript), i get the following error messeage:
(11,1) Microsoft OLE DB Provider for ODBC Drivers:
[Microsoft][ODBC SQL Server Driver][Shared Memory]SQL Server does not exist or access denied.
Although the Connection string seems to be correct:
strConnection = "Driver={SQL Server};Server=SCOMSRVDB01;Database=DBABee;Trusted_Connection=TRUE"
Here is the Full VBScript:
Dim objCN, strConnection
Dim oAPI, oBag
Set objCN = CreateObject("ADODB.Connection")
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
strConnection = "Driver={SQL Server};Server=SCOMSRVDB01;Database=DBABee;Trusted_Connection=TRUE"
objCN.Open strConnection
Dim strSQLQuery
strSQLQuery = "Select COUNT (*) from sys.objects"
Dim objRS
Set objRS=CreateObject("ADODB.Recordset")
Set objRS = objCN.Execute(strSQLQuery)
Do Until objRS.EOF
'WScript.Echo objRS.Fields("No column name")
if objRS.Fields("No column name") > 0 then
'WScript.Echo "evaluated as bad"
Call oBag.AddValue("State","BAD")
Call objAPI.Return(oBag)
else
Call oBag.AddValue("State","GOOD")
Call objAPI.Return(oBag)
end if
objRS.MoveNext
Loop
objRS.Close
It worth mentioning,
That in our company you can't connect to an sql server without mention Port Number.
But when i tried to add it (Port: 2880) in the connection string:
strConnection = "Driver={SQL Server};Server=SCOMSRVDB01,2880;Database=DBABee;Trusted_Connection=TRUE"
The script returen the following error:
(23,17) ADODB.Recordset: Item cannot be found in the collection corresponding to the requested name or ordinal.
The ADODB error indicating that the item connect be found means that you successfully connected to the DB, and it can't find the column you requested. This is what is can't find: objRS.Fields("No column name")
Change your query and name the column:
strSQLQuery = "Select COUNT (*) as countStuff from sys.objects"
Then change what you are looking for:
if objRS.Fields("countStuff") > 0 then
So I have this application and I moved all local tables to SQL Server using upsizing, now they are currently linked tables. I'm able to access tables and forms related to tables can be accessed with no problems. But when I programmatically fetch a record, or perform a sql operation in VBA script, a SQL Server Login prompt pops up asking me to enter in the SQL Authentication login to access the database.
I followed this link here:
https://support.microsoft.com/en-us/kb/177594
Where this is my end code:
Dim db1 As Database
Dim db2 As Database
Dim rs As Recordset
Dim strConnect As String
Set db1 = OpenDatabase("C:\Workspace\ms1.mdb")
strConnect = UCase(db1.TableDefs("dbo_TableA").Connect) & ";UID=User1;PWD=Password1"
Set db2 = OpenDatabase("", False, False, strConnect)
db2.Close
Set db2 = Nothing
Set rs = db1.OpenRecordset("dbo_TableA")
rs.Close
db1.Close
Set rs = Nothing
Set db1 = Nothing
DoCmd.RunCommand acCmdSaveRecord
'Sql Server login prompt pops up after running the below code;'
If DCount("*", "TableA", "[ColA] = [forms]![FRM_LOGS]![USER]") = 0 Then
MsgBox "User ID not found - contact HelpDesk", vbCritical
DoCmd.Quit
Exit Sub
End If
The DCount is triggering the SQL Server Login Prompt. I need this prompt to go away. If I open up a form, query, report, anything where the access object is bound to the data, I get no message. It ONLY happens in VBA when I'm trying to access the data object.
Edit! I did find the culprit. I deleted the linked table to the TableA in sql server, and I relinked it again, and clicked the Save password checkbox. I did this before, and it didn't work. Did it again, and it fixed everything. Not sure why this didn't work the first time. I marked the below as an answer because that did solve the problem given the circumstances.
Not sure what you're doing here with two database connections and using DCOUNT on an internal table?
It looks like your database connection has linked tables that have stored passwords
Why not just use your recordset that works to check for a valid user?
Set db1 = OpenDatabase("C:\Workspace\ms1.mdb")
Set rs = db1.OpenRecordset("SELECT [ColA] FROM [dbo_TableA] WHERE [ColA] = """ & [forms]![FRM_LOGS]![USER] & """")
if rs.EOF Then
MsgBox "User ID not found - contact HelpDesk", vbCritical
DoCmd.Quit
Exit Sub
End If
rs.Close
db1.Close
Set rs = Nothing
Set db1 = Nothing
I'm not able to figure out how to make a multi database query usign two diffent ditabase.
The first database is an ACCESS mdb database and the second one is a SQL Server 2012 database.
I'm using Visual Basic 6.
The code I'm using is that:
ConnectionString1 = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=C:\MyDatabase.mdb"
ConnectionString2 = "DSN=ODBC_NAME;UID=user;PWD=password"
SQL = "SELECT tabScheda.sWorkNum FROM tabScheda WHERE codScheda NOT IN" + _
" (SELECT ORDERID FROM [" + ConnectionString2 + "].V_PRESETTING_SP_HEADER WHERE MODE = 'TC')"
cn.Open ConnectionString1
Set rc = cn.Execute(SQL)
rc.Close
Set rc = Nothing
cn.Close
Set cn = Nothing
Please help me :D