Run SQL Job Agent via Macro Excel by Windows Authentication - excel

I have macro to run manually SQL Job Agent by using userid and Password
Sub Run_Job()
Dim con As Object
Set con = CreateObject("ADODB.Connection")
con.Open = "Provider=sqloledb; Data Source=SERVERNAME; Initial Catalog=DATABASENAME; User Id=USERNAME; Password=PASSWORD;"
con.Execute "exec msdb.dbo.sp_start_job ‘AGENTJOBNAME’"
Workbooks("WORKBOOKNAME.xlsx").Close
End Sub
But when my SQL Server must using Windows Authentication to login ,
how to create macro by using windows auth to run it?

Do small correction to your connection string
con.Open = "Provider=sqloledb; Data Source=SERVERNAME; Initial Catalog=DATABASENAME; Integrated Security=SSPI;"
It switches to integrated authentication.

Related

Excel to MS Access connection: Issues with with space in connection string

I have been able to successfully connect my excel to MS access, and using the connection, I am able to push and pull data to and from excel to Access DB. However, I noticed, if my connection string has space in between like mentioned below, I am unable to establish the connection. Is there any way around it?
Connection string with space
Set oADODBCon = New ADODB.Connection
oADODBCon.Open Microsoft.ACE.OLEDB.12.0;Data Source=\\XYZ-123\0123\ABC 2020 Test Operations\MyDb.accdb;Persist Security Info=False;
I have tried using single quotes in my connection string as below but it still didn't work
Set oADODBCon = New ADODB.Connection
oADODBCon.Open Microsoft.ACE.OLEDB.12.0;Data Source='\\XYZ-123\0123\ABC 2020 Test Operations\MyDb.accdb';Persist Security Info=False;
Any help would be much appreciated!
oADODBCon.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\XYZ-123\0123\ABC 2020 Test Operations\MyDb.accdb;Persist Security Info=False;"

ADODB connection to a DBF in Azure WebApp

I'm using an Azure WebApp (Tier: F1)
I hosted these files and directories:
/wwwroot/default.asp
/wwwroot/Data
/wwwroot/Data/customers.dbf
/wwwroot/Data/vessels.dbf
The default.asp file contains the following:
<%
On Error Resume Next
Response.Write("Path: " & Server.MapPath("Data") & "<br>")
DBRoot = "D:\home\site\wwwroot\Data\"
Set conn = Server.CreateObject("ADODB.Connection")
DataBase = "DRIVER={Microsoft Visual FoxPro Driver};SourceType=DBF;sourcedb="& DBRoot
conn.Open DataBase
If Err.Number <> 0 Then
Response.Write (Err.Description)
Response.End
End If
%>
And i'm getting this output:
Path: D:\home\site\wwwroot\Data
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I'm not sure if the error is because:
The driver (Microsoft Visual FoxPro Driver) is not installed or supported in Azure
The path is incorrect (because web apps in Azure have a shared physical path)
How can I make this connection work?
Note: just as a reference, I intend to use these dbf files with SQL queries such as this:
<%
conn.Execute("SELECT * FROM [customers.dbf] WHERE userId ='"& userId &"'")
%>
All these used to work in an old hosting I had. It doesn't works in Azure.

What is the method for making an ADODB connection from VBA to a (Google) Cloud SQL database?

Connecting to Cloud SQL appears to be different than connecting to normal MySQL databases.
I have added the 'Instance connection name' after the 'Public IP address'.
I am getting an error:
'Unknown MySQL server host'
Dim Cnn As Object
Set Cnn = CreateObject("ADODB.Connection")
Cnn.Open ("DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=xxx.xxx.xxx.xxx\instanceConnectionName;DATABASE=databaseName" & ";USER=user;PASSWORD=abcd")
If Cnn.State = adStateOpen Then
Debug.Print "Connection"
Else
Debug.Print "No Connection"
End If
Cnn.Close
I would recommend to use the SQL Proxy and then for the connection would be like if the database was in your localhost.
So the steps would be:
Install Cloud SQL Proxy in your computer
Start the cloud SQL Proxy
Connect as I f you were connecting to a local database
the code would be:
Dim Cnn As Object
Set Cnn = CreateObject("ADODB.Connection")
Cnn.Open ("DRIVER={MySQL ODBC 5.3 Unicode Driver};SERVER=172.0.0.1;DATABASE=<DATABASENAME>;USER=<USERNAME>;PASSWORD=<PASSWORD>)
If Cnn.State = adStateOpen Then
Debug.Print "Connection"
Else
Debug.Print "No Connection"
End If
Cnn.Close
What kind of DB are you connection to? You can find all kinds of connection strings here.
https://www.connectionstrings.com/
Also, go to your ODBC drivers and see what you have installed. Your code has to match what you have installed. Here is an image of what I have on my system.
So, for instance, you have to have 'MySQL ODBC 5.3 Unicode Driver' installed because that's what you are referencing in your code. Try that and feed back with your findings and results.

Error while creating Sybase Connection from VB

I am trying to connect to Sybase ASE from VB and getting following error
Function createConnection() As ADODB.Connection
Dim ConnectString As String Dim ConnString As String
Set conn = New ADODB.Connection
.Mode = adModeReadWrite
.ConnectionTimeout = 15
.ConnectionString = "Provider=ASEOLEDB;" & _
"Server Name=" & Range("DBServerName") & _
";Initial Catalog=db;" & _
"User Id=user;" & _
"Password=password"
.Open
End With
Set createConnection = conn
End Function
Any help would be much appreciated.
I would suggest you fist to check odbcad as described here.
If you can find any mention of ASEOLEDB there, then you just have problem either with the app using wrong type of drivers (32-bit vs 64-bit) or with a connection string.
But as the error message suggests, you might be missing just the provider - same problem as here. These libraries are usually part of these installations:
SAP/Sybase ASE
SAP/Sybase ASE PC Client CD
SAP/Sybase ASE SDK
All of these can be acquired on SAP portal, however you need a login to that place as this software is accessible only on purchase. You can find here a link of PC Client CD content (Sybase infocenter website) and a manual on OLEDB here.
Unfortunately only freely downloadable ASE is for Linux, which is useless for you. Maybe you can find on that W7 machine installation media ...
Download & Install Provider
http://devzone.advantagedatabase.com/dz/content.aspx?key=20&Release=19&Product=15
use below commands to register provider
regsvr32 sybdrvodb.dll
regsvr32 sybdrvodb64.dll
Details:
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc20116.1550/html/aseodbc/aseodbc43.htm

Get Current user's Workstation name logged into Host computer

I have a host machine running windows-7 and users from the home network login using windows remote desktop connection and run some applications ,
I can visually see the log of who logged in under
Event Viewer->Windows Logs->Security->Audit Success->Details->Workstation name
Since only one connection is allowed using Remote Desktop connection, I want to display in an access database who the current user is, from the audit trail so users know if anyone is currently using the Host machine
So basically a VBA code to grab the most recent workstation that logged in, specially I am interested in Event ID-4624
I am open for other suggestions!
what I have tried so far?
Function Reachable(strComputer) As String 'Test Connectivty to computer
'On Error Resume Next
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", "username", "password")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_NetworkLoginProfile")
For Each objItem In colItems
Reachable = "Full Name: " & objItem.FullName
Next
End Function
What about Environ("clientname")? At least in the RDP setup I use, it is an environment variable that names my PC that has connected to the remote desktop host computer.
Would this work?
Dim sHostName As String
sHostName = Environ$("computername")

Resources