Connecting to SQL Server over network using ADO from Excel VBA - sql-server

I need to connect to a SQL Server DB running on my laptop from another computer on a network. I am doing this both from a C# app as well as in a VBA macro. Both run perfectly on my laptop and the C# app runs perfectly over the network. However I cannot connect using VBA over the network. This is my connection string:
ConnectionString = "Driver={SQL Server};Server=MY-LAPTOP; DAtabase=SAFEXlive; UID = MyUsername; PWD=MyPassword"
Aside from the 'Driver={SQL Server}' this is the same as the connection string I am using in the C# app which works.
I am then using the following code (With a reference to Microsoft ActiveX Data Objects 6.0 Library in VBE) to open the connection:
Dim oConnection As Connection
Set oConnection = New Connection
oConnection.ConnectionString = strConnectionString
oConnection.Open
This works correctly if I run it on my laptop but if I run it on another computer on the network I get the error: "Run-time error '-2147217843 (80040e4d) [Microsoft][ODBC Server Driver][SQL Server]Login failed for user..." and the user it specifies it the windows log in for the computer.
Are there some security settings I need to set in code or in excel? Or am I constructing the connection string incorrectly? What am I doing wrong?

Solved. The answer is rather infuriating. The problem is in fact with the connection string, with the UID. Believe it or not changing ...UID= MyUsername;... to ..UID=MyUsername;..., i.e. removing the space character, was all it took! Thanks for suggestions though.

Try this Connection string,
ConnectionString = "Provider=SQLOLEDB;Data Source=MY-LAPTOP;Initial Catalog=SAFEXlive;User ID=MyUsername;Password=MyPassword"
Is this an AD Domain login? Make sure you have appended the domain to the username e.g, domain\user .
I suggest using integrated security instead of this.

Related

Can't connect to SQL Server with my vb.net program

I'm trying to test SQL Server (I've never used before), and I always get this annoying error:
I've traveled multiple websites, multiple questions, and do not get the solution. The user 'MrDan' is like the WINDOWS admin... I can't understand why can't connect.
Dim con As New MySqlConnection("Server=********;Database=PruebaMultiple;User Id=MrDan;Password=*****;")
Am I missing something? Or am I using a bad connection string?
First, connections to SQL Server are made using SqlConnection, not MySqlConnection. MySQL and SQL Server are different database systems. Second, explicit credentials work only for SQL accounts. You're trying to log in with a Windows user, and should therefore use Integrated Security=SSPI in your connection string (and omit user name and password). To sum up:
Dim con As New SqlConnection("Server=********;Database=PruebaMultiple;Integrated Security=SSPI")

MS Access - SQL Server does not exist or access denied (Error), Tried Everything

I have a MS Access DB front end that connects to SQL Server Express backend. Port 1433 is enabled on the In-Bound server side, TCP/IP is enabled with correct Port, I've tried to include "tcp:" in front of the "Server=", I've included "Port=1433" and have tried multiple connection strings. This error only occurs with certain users that are on a Guest Network. However, other users using different Guest Networks have had success with no issues. I've also tried creating ODBC connection through Administrative Tools using SQL Server and SQL Native Client with no success and TCP/IP Enabled. Using cmd.exe and pinging to the IP is successful. After googling for hours and trying multiple solutions, I am completely stumped! Does anyone have some guidance?
MS Access 2010 (On user end) DSN-Less connection to tables.
Dim cns As ADODB.Connection
Set cns = New ADODB.Connection
cns.ConnectionTimeout = 4
cns.ConnectionString = "Driver={SQL Server};Server=111.111.111\MYDB;Trusted_Connection=no;Database=MYDB;Uid=UserID;Pwd=Password;"
Also tried Driver={SQL Native Client} as well as other drivers and connection strings.

ODBC DSN Connection Error - SQL Server 2014 Express Classic ASP

Trying to connect a classic asp page to sql server express 2014 using a dsn.
I've created a new database (in sql server management studio) called warehouse, with a table called users. I've added a new login called user1 with a password and mapped it to the warehouse database
I've created an ODBC System DSN on the same machine, using sql server native client 11 driver called SQLServer2 with integrated windows authentication, and a default database warehouse. Testing the connection at this point works fine!
On the ASP page I'm using:-
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "dsn=SQLServer2;uid=user1;pwd=xyz;"
Error:- Microsoft OLE DB Provider for ODBC Drivers error '80040e4d'
[Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open
database "warehouse" requested by the login. The login failed.
I'm pretty sure that its a permissions issue and that the connection string is OK otherwise but after hours and hours of trying different strings, different users, I can't seem to get the damn thing to connect
If Anyone can throw any ideas my way I'd be very grateful. Thanks!
The way you are describing the problem the DSN uses integrated security so your credentials might be ignored and the database is called using the application pool identity.
If you have no specific reason why you need to use a DSN I would recommend using a connection string like the following:
objConn.Open "Provider=sqloledb;Server=yourdbserver;uid=user1;pwd=xyz;Database=warehouse"
A look into your SQL server log might give you a hint as to which user is actually failing to connect to your database.
I think you should have done something like this
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString "dsn=SQLServer2;uid=user1;pwd=xyz;"
objConn.open

Classic Asp Connection String to SQL Server 2012 with Windows Authentication on Windows 8

I'm want to migrate a number of MS Access 2007 Databases to SQL Server 2012 locally but before I do I would like to set up a generic connection string that works, I will the have to get them all up and running on the production server too.
This is a sample of the string I have to the .mdb file:
conditions_report = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" + Server.MapPath("_private/conditions_report.mdb")
set conn = Server.CreateObject("ADODB.Connection")
conn.Mode = 3
conn.Open conditions_report
And this is about as far as I've come with creating something for the .mdf:
conditions_report = "Provider=SQLNCLI11;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=conditions_report;Data Source=XXX"
set conn = Server.CreateObject("ADODB.Connection")
conn.Mode = 3
conn.Open conditions_report
I only work in MVC these days and have never connected Classic Asp to SQL Server and due to time frames migrating from Classic Asp to MVC is not an option at the moment but this would be a at least a step in the right direction.
Edit
Another problem I'm having is I'm not sure what the error messages are telling me. I'm not sure if they are telling me my server is setup incorrectly, it's a permissions problem, a TCP IP issue, if it's not finding the server or finding the server and not the database and my connection string is just wrong! Some of the errors if have been getting are here:
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
Named Pipes Provider: Could not open a connection to SQL Server [53].
Cannot open database "conditions_report" requested by the login. The login failed.
TCP Provider: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Any help would be greatly appreciated.
I think SQLNCLI11 is the preferred driver for SQL Server 2012
http://www.connectionstrings.com/sql-server-native-client-11-0-oledb-provider/

Excel VBA connect to SQL Server Login Error

I am a beginner in Excel VBA and faced a problem: I can connect to remote SQL Server through Management Studio, but got error when connecting through Excel VBA.
Below is my connection string and the error code.
Const connection_string As String = "Provider=SQLOLEDB.1;Persist Security Info=True;Integrated Security=SSPI;Initial Catalog=RPGTFDB;Data Source=122.xxx.xxx.xxx;User ID=sa;Password=xxx"
Error is:
Run-time error (80004005): Login failed for user ". The user is not associated with a trusted SQL Server connection.
Is it not able to get the User ID?
However when I connect to local computer, like 127.0.0.1, the above connection string works.
Thanks a lot!
In your connection string you should not use
User ID=sa;Password=xxx
As the error seems to be indicating that only Windows Logins are enabled on the SQL Server and this connection string item is only for SQL Logins.
Do you have to punch in a login and password when connecting with management studio? If not then I suggest you have a database that authenticates a connection using your windows login.
If this is the case, then remove the User ID and Password bits and insert Trusted_Connection=Yes instead.
Finally, the connection string is created using UDL application.
Despite I still cannot find any difference between the string and the manual created one, it works!
Follow the instructions in this link:
http://social.technet.microsoft.com/wiki/contents/articles/1409.how-to-create-a-sql-connection-string-for-an-application-udl-file.aspx

Resources