I'm trying to connect to a SQL Server from multiple PCs in the same domain.
When using the following code:
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection 'Neue Verbindung initialisieren
'Verbindung einrichten'
conn.ConnectionString = "PERSIST SECURITY INFO=True;Provider=SQLOLEDB.1;Server=WWDDB;Database=01Projekt;User ID=XXX;Password=XXX;Trusted_Connection=True;Integrated Security=SSPI;"
conn.Open
conn.Open returns the error:
Error on login for the user 'XXXX'
The issue is because you are using a named user with Integrated Security. These two modes are incompatible.
Try removing Integrated Security=SSPI:
conn.ConnectionString = "PERSIST SECURITY INFO=True;Provider=SQLOLEDB.1;Server=WWDDB;Database=01Projekt;User ID=XXX;Password=XXX;Trusted_Connection=True;" conn.Open
Or the named user:
conn.ConnectionString = "PERSIST SECURITY INFO=True;Provider=SQLOLEDB.1;Server=WWDDB;Database=01Projekt;Trusted_Connection=True;Integrated Security=SSPI;" conn.Open
this is my snippet, try it :
Set cnConn = New ADODB.Connection
With cnConn
.Provider = "SQLOLEDB.1"
.CursorLocation = adUseClient
.ConnectionTimeout = 0
.Properties("Data Source").Value = ' serverName
.Properties("Password").Value = ' pswd
.Properties("User ID").Value = ' userName
.Properties("Initial Catalog").Value = ' init DB
.Open
End With
Related
Here's the code
Dim cmd As ADODB.Command
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.CursorLocation = adUseClient
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Nirvana.mdb" & "; Persist Security Info=False;"
Set cmd = New ADODB.Command
cmd.CommandText = "Select [Last Name] From Accounts Where [First Name]=#FN"
Set cmd.ActiveConnection = conn
cmd.Parameters.Item("#FN").Value = txtFirstName.Text
cmd.Execute
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
conn.Close
How do i pass the result of this query to a variable in Visual Basic 6.0 ?
Results are stored in a Record Set, create one:
dim rs as ADODB.RecordSet
Then instead of cmd.execute use:
rs.open cmd
if not rs.eof then
''//got rows
msgbox "first row, first col=" & rs.collect(0)
...
Here is my problem: I need to get data from .mdb file that is on the network hard drive. I am using ADODB to connect to it, but when i try to return field value from RecordSet that i created it returns error:
Operation is not allowed when the object is closed
Here is my code:
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
cmd.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ".mdb"
cmd.CommandText = SQLRequst
cmd.CommandType = adCmdText
cmd.CommandTimeout = 900
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenForwardOnly, adLockReadOnly
Debug.Print rs.Fields(0)
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
Set rs.ActiveConnection = Nothing
At the moment i tried many different types of connection. The main thing thats makes me confused is that it connects to .mdb file and also creates RecordSet but most values are set to "Operation is not allowed when object is closed"
I think the problem is with the way i am connecting and getting records sets because I use same method to connect to sql database and it works just fine.
Any help will be appreciated
*Edited : SQLRequest = "SELECT * FROM tblStack WHERE StackID=XXXXX"
Changed code to this:
Dim rs As New ADODB.Recordset
Dim conn As New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ".mdb"
rs.Open SQLRequst, conn, adOpenForwardOnly, adLockReadOnly
Debug.Print rs.Fields(0)
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
Set rs.ActiveConnection = Nothing
Still debug doesn't print anything and in Locals window shows that Fields(0).Value Operation is not allowed when the object is closed
Problem was in SQLRequst string :
SQLRequest = "SELECT * FROM tblStack WHERE StackID='XXXXX'"
It needed ''.
I am trying to connect to the Sybase server through VBA and run the sql file that contains a set of sql lines (sometimes more than 60 lines).
I have written the code as below. However, it is giving me error that " There is an incorrect statement around '\'"
Please help.
Sub sqltest()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Set conn = New ADODB.Connection
conn.Open "DRIVER={Sybase ASE ODBC Driver};UID=" & ThisWorkbook.Sheets("Sheet1").Cells(2, 13).Value & ";pwd=" & ThisWorkbook.Sheets("Sheet1").Cells(2, 14).Value & ";NA=<server address> ; CommandTimeout = 50000 ;ConnectionTimeout = 50000; ConnectionIdleTimeout = 50000;Connection LifeTime = 50000;LoginTimeout = 50000;AlternateServers = <server address>;"
conn.Open
Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "\\<IP address>\pag\OffShore PAG\Parallel Run\Reports\Sunil\Mymacros\sample macros\SQL NC DB.sql"
cmd.Execute
conn.Close
Set conn = Nothing
Set cmd = Nothing
End Sub
Why do it by VBA? I would do it manually using the "From other sources" in the "Data" ribbon menu item.
"From other sources"->"From Microsoft Query"->"".
Next specify your database, driver etc. and the SQL.
What is the syntax to call stored procedure in SQL Server from Excel (VBA) using windows authentication?
'Remember to reference Microsoft ActiveX Data Objects via Tools - References
Dim strConn As String
Dim conn As New Connection
Dim cmd As New ADODB.Command
Dim rs As New Recordset
strConn = "DRIVER=SQL Server;SERVER=ServerName;DATABASE=DBname"
conn.ConnectionString = strConn
conn.Open
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "SPName"
cmd.Parameters.Refresh 'requires a trip to server / can add parameters manually using cmd.Parameters.Append
cmd.Parameters("#Param1").Value = ""
Set rs = cmd.Execute
If Not rs.EOF Then
'your code here
End If
conn.Close
Set rs = Nothing
I am not sure whether it has been disscussed before or not but I have the following code (I changed from example for an Access database.)
I don't know what to put inside the 'Provider' and the 'Data Source'. I'm using MS SQL Server 2005 and how to find this information in my machine? Please, even I try to follow from here http://www.connectionstrings.com/, yet I still don't know how to do it.
Dim conn, sql
sql = "SELECT * FROM tblOutbox"
Set conn = CreateObject("ADODB.Connection")
With conn
.Provider = "myProvider"
.Mode = adModeReadWrite
.ConnectionString = "Data Source=mysource;" & _
"database=myDbase.mdf; "
.Open
End With
If conn.State = adStateOpen Then
WScript.Echo "Connection was established."
End If
conn.Close
Set conn = Nothing
here is using access database:
Dim conn
Dim sql
sql = "SELECT * FROM tblOutbox"
Set conn = CreateObject("ADODB.Connection")
With conn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Mode = adModeReadWrite
.ConnectionString = "Data Source= f:/Status.mdb"
.Open
End With
WScript.Echo "Connection was opened."
conn.Close
Set conn = Nothing
WScript.Echo "Connection was closed."
Try setting the provider in the connection string
conn.ConnectionString = "Provider=SQLOLEDB.1;Data Source=XXX;Initial Catalog=XXX;User ID=<XXX>;Password=<XXX>;"
Note: I haven't tested it but it should work
For all your connection string needs: http://www.connectionstrings.com/
I finally found the solution thanks to your help.
Under the Administrative Tools, Data Source (ODBS), driver tabs, I use provider: SQLNCLI, my Server: Data Source, and I add Recordset rs to the code. It seems that, without this one, the connection is not established. I don't know what happened.
Dim conn , rs, sql, ConnString
sql = "SELECT * FROM tblOutbox"
Set rs = CreateObject("ADODB.Recordset")
Set conn = CreateObject("ADODB.Connection")
With conn
.Provider = "SQLNCLI"
.Mode = adModeReadWrite
.ConnectionString = "SERVER=.\SQLExpress;AttachDbFilename=F:\Test2.mdf;Database=Test2.mdf; Trusted_Connection=Yes;"
.Open
WScript.Echo "Connection was established."
End With
rs.Open sql,conn
If conn.State = adStateOpen Then
WScript.Echo "Connection was established."
Else
WScript.Echo "No Connection ."
End If
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
With SQL Server you don't connect directly to the file. Access, being a file-based RDBMS is a little different.
Following your example, it would look something like this:
Dim conn, sql
sql = "SELECT * FROM tblOutbox"
Set conn = CreateObject("ADODB.Connection")
With conn
.Mode = adModeReadWrite
.ConnectionString = "Provider=SQLOLEDB;server=[servername];database=[databasename]uid=[insertuser];pwd=[insertpassword];"
.Open
End With
If conn.State = adStateOpen Then
WScript.Echo "Connection was established."
End If
conn.Close
Set conn = Nothing
There's usually a more compact way of doing this, but without the context its hard to give a better example