VBA - Remove Password from OLEDB Connection String - excel

I have an excel file that contains a series of OLEDB connections leveraged by several pivot tables. I would like to create a VBA function that removes all password from that several connection string as the file is closed(so that the users password will not be persisted). First I thought all I need to do was set the "Save Password" property to false, something like this:
Public Sub RemovePasswordByNamePrefix()
Dim cn As Object
Dim oledbCn As OLEDBConnection
For Each cn In ThisWorkbook.connections
Set oledbCn = cn.OLEDBConnection
oledbCn.SavePassword = False
Next
End Sub
Should work right, on closing the file and reopening it you shouldn't see the password anymore in the connection string. It should not be "Saved":
Wrong, password is still there... It has been "Saved". Not sure what that feature is supposed to do. Maybe there referring to a different password? So, I attempted the big hammer approach, unfortunately it has it's own challenges, and so far I haven't gotten that working.
I'm not quite sure how to do this... Why is this so massively insecure? It persists plaintext passwords every file that contains a connection string of this sort, easily readable by whoever could access that file.
Maybe I could make some sort of Regex to remove just the password from the file? When I do that in the interface my cubes refresh and prompt me for my credentials, (I wonder)would that occur if I did it in VBA, even if the trigger is upon excels closure?
Bottom Line: What is the best way to prevent these passwords from being persisted in the file upon it's closure?

#TomJohnRiddle points out that I should look at modifying the connection string similar to the following question. Initially I was concerned that taking this approach could prompt the user with a login screen after modifying the connection string. However since I don't have any better ideas I gave it a shot, and it seems to work, here's what I've mocked up:
Public Sub RemovePasswordByNamePrefix()
Dim cn As Object
Dim oledbCn As OLEDBConnection
Dim regEx As New RegExp
regEx.Pattern = "Password=[^;]*;"
For Each cn In ThisWorkbook.connections
Set oledbCn = cn.OLEDBConnection
oledbCn.SavePassword = False
oledbCn.connection = regEx.Replace(oledbCn.connection, "")
oledbCn.CommandText = "" 'My app repopulates this after open
Next
End Sub
and it seems to work:
So I think I'll go with this approach, but I'm still open to other suggestions. Would be nice to clear everything and fully reload it, but so far that doesn't appear to be possible.
I'm also concerned with what versions of VBA support the "Regex" references. I would like something that would be Excel 2010+ 32/64 bit compatible. I have yet to test this on any older version(I'm currently running Office 365). I assume it will all work fine, but I've been unpleasantly surprised with these things in the past.

See this on SQL Server authentication Authentication in SQL Server. There it says you can use 100% Windows Authentication or you can use Mixed-Mode (Windows Authentication and passwords). If you really want to banish passwords from connection strings do not install with Mixed Mode Authentication just run 100% Windows Authentication. However, there may be some code already deployed written to use passwords so that may not always be practical.
So, the other way to discipline no passwords is to use
Integrated Security=true;
in your connection strings. This Stack Overflow question on the subject is well visited.

#NigelHeffernan suggests a slightly different approach for how to do this, here's a version without regex's:
Public Sub RemovePasswordByNamePrefix()
Dim cn As Object
Dim oledbCn As OLEDBConnection
Dim stringArray
Dim stringElement As Variant
Dim newStringArray As Variant
For Each cn In ThisWorkbook.connections
Set oledbCn = cn.OLEDBConnection
oledbCn.SavePassword = False
stringArray = Split(oledbCn.connection, ";")
For Each stringElement In stringArray
If Not InStr(stringElement, "Password=") Then
If IsEmpty(newStringArray) Then
newStringArray = Array(stringElement)
Else
ReDim Preserve newStringArray(UBound(newStringArray) + 1)
newStringArray(UBound(newStringArray)) = stringElement
End If
End If
Next
oledbCn.connection = Join(newStringArray, ";")
oledbCn.CommandText = "" 'My app repopulates this after open
Next
End Sub
I'm not sure the benefit of this method(other than a lack of another library reference) and I haven't tested outside of one connection string/one machine yet. My connection strings don't contain the "Extended Properties" field, maybe this approach wouldn't work for that.

It looks like you are using DSNs, which is something that Excel will create if you use the default connection management tools in the GUI. When working with DSNs, the ODBC driver will sometimes put cleartext passwords in to the Registry, even when you don't select "Save Password".
Instead of allowing Excel to manage your connections you would need to manage them yourself. Here is some example code from MS MVP Ben Clothier. You would have to modify the connection string to match your use case. You might be able to copy the details from your existing connections before you remove them.
Public Function InitConnect(UserName As String, Password As String) As Boolean
‘ Description: Should be called in the application’s startup
‘ to ensure that Access has a cached connection
‘ for all other ODBC objects’ use.
On Error GoTo ErrHandler
Dim dbCurrent As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
‘<configuration specific to MySQL ODBC driver>
strConnection = “ODBC;DRIVER={MySQL ODBC 5.1 Driver};” & _
“Server=” & ServerAddress & “;” & _
“Port=” & PortNum & “;” & _
“Option=” & Opt & “;” & _ ‘MySql-specific configuration
“Stmt=;” & _
“Database=” & DbName & “;”
Set dbCurrent = DBEngine(0)(0)
Set qdf = dbCurrent.CreateQueryDef(“”)
With qdf
.Connect = strConnection & _
“Uid=” & UserName & “;” & _
“Pwd=” & Password
.SQL = “SELECT CURRENT_USER();”
Set rst = .OpenRecordset(dbOpenSnapshot, dbSQLPassThrough)
End With
InitConnect = True
ExitProcedure:
On Error Resume Next
Set rst = Nothing
Set qdf = Nothing
Set dbCurrent = Nothing
Exit Function
ErrHandler:
InitConnect = False
MsgBox Err.Description & ” (” & Err.Number & “) encountered”, _
vbOKOnly + vbCritical, “InitConnect”
Resume ExitProcedure
Resume
End Function
NOTE:
This is written for MS Access, not Excel. The concepts are all the same. You might want to try making your front end in Access and then export your views to Excel from Access. This would allow you better control of the link to your back-end and allow you to use SQL in Access to define what you want to export to Excel.
READ THIS:
https://www.microsoft.com/en-us/microsoft-365/blog/2011/04/08/power-tip-improve-the-security-of-database-connections/

Related

How to overcome a missing reference/library in VBA using Access/ADOB connection?

I have an 'excel front end' for an access database. The idea is that 10 users have their own spreadsheet which can query the database for items relating to them and then they can also respond and write back the database via a macro.
The code is as follows:
Sub update_access()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim cn As ADODB.Connection, rst As ADODB.Recordset
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=S:\Public\XYZ\XYZ\XYZ\XYZ\XYZ.mdb;"
' open a recordset
Set rst = New ADODB.Recordset
rst.Open "qryXYZ", cn, adOpenKeyset, adLockOptimistic, adCmdTable
g = Range("A2").End(xlDown).Row
For f = 3 To g
If Cells(f, 11) <> "" Then
lnId = Cells(f, 1)
rst.Filter = "ID = " & lnId
With rst
.Fields("response") = Cells(f, 11)
.Update
End With
Cells(f, 11).ClearContents
End If
Next f
wb.RefreshAll
MsgBox ("All finished!")
End Sub
It worked perfectly on my own PC whilst I was testing it, however when sending it to the users they all received the same 'project or library not found/missing'
On further investigation and reading I found a post regarding Missing libraries, in my case: Missing: Microsoft Access 16.0 Object Library . Not sure where to go from here, does this mean the users need to have access installed on their PC's to have access to the library or could a copy of my GUID/reference be copied over into their Program Files (x86) (same location as where my reference is)
Starting to get into a realm where I don't want to tread as this would likely mean asking IT department to do that, can anyone think of an alternative solution?
In addition to that I have had to resort to saving the database as .mdb instead of .accdbas "Provider=Microsoft.Jet.OLEDB.4.0; " & _ does anyone know the correct connection type/provider to enable me to access an .accdb as I feel like I've cheated by resorting to an older version of access in order to make my code work?

Uploading data from Excel 365 to Azure Sql Server using VBA only

Although I used to be pretty advanced using VBA many moons ago, apparently that guy has left the building.
I just recovered some very large commodity and equity data sets I had in excel files from years ago. I have compiled them all together into one file and am trying to upload the data to my Azure SQL server. I cannot find ANYTHING anywhere on how to load data TO Azure, but I find many articles on loading FROM Azure.
Inside excel you can only create a connection to Azure SQL to IMPORT data into excel, one way street, no way to make a connection you can load from.
Any help appreciated. I can always use the old CSV's, read them in using c# in my web app, but I can update the data to current each week in this one file and I'd prefer to write the whole routine in VBA to get the data, check it, and load it to Azure.
Note: Yes, I know how to use ADODB connections but I cant find any drivers specific to using Azure SQL
Further update:
Using the following and I've tried 2 dozen variations, produces this error each time. And I installed the Sql Native Client Driver #12
Here is the function with the Driver, which I believ eis the issue, or the permissioning, because the azure sql db exists. This is the connection string from the portal
Public Function ExportExcelDataToAzureDb(wsSource As Worksheet) As Boolean
'I am using Activex Data Object 2.8 reference
Dim connectionString As String
Dim oConn As ADODB.Connection
Dim record As ADODB.Recordset
Dim cmd As ADODB.Command
connectionString = "Driver={SQL Server Native Client 12.0};Server=tcp:myazuresqlserver.database.windows.net,1433;Initial Catalog=myappdb;Persist Security Info=False;User ID=myuser;Password={mypass};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
Set oConn = New ADODB.Connection
oConn.connectionString = connectionString
oConn.Open
'load the data
ExportExcelDataToAzureDb = True
End Function
Obviously, I have substituted for the server, db, user and password
After a LOT of searching for various connection strings examples AND about 100 different tests of string variations.
Here is the proper connection string format to use with Excel VBA to import/export data from Azure SQL Database. I am providing my whole function for inserting data, with some parts removed for security concerns.
Option Explicit
Public dbConn As ADODB.Connection
Public adodbRecordSet As ADODB.recordSet
Public rsc As ADODB.recordSet
Public Const AzureDataSource As String = "tcp:yourazuredbservername.database.windows.net,1433"
Public Const AzureDbName As String = "yourazuredbname"
Public Const dbDriverProvider As String = "SQLNCLI11"
'I am using Activex Data Object 2.8 reference, SQL Native Client 11
Public Function ExportExcelDataToAzureDb(wsSource As Worksheet, dataRange As Range) As Boolean
Dim connectionString As String, sql As String
Dim cnt As Integer
Dim cCell As Range
connectionString = "Provider=" & dbDriverProvider & ";" & "Password=yourpassword;User ID=yourusername; " & _
"Initial Catalog=" & AzureDbName & ";" & _
"Data Source=" & AzureDataSource & ";"""
Set dbConn = New ADODB.Connection
Set adodbRecordSet = New ADODB.recordSet
dbConn.connectionString = connectionString
dbConn.Open
' do work here
cnt = 0
With wsSource
For Each cCell In dataRange
sql = "your insert statement here"
adodbRecordSet.Open sql, dbConn, adOpenDynamic, adLockReadOnly, adCmdText
cnt = cnt + 1
Next cCell
End With
'clean up
dbConn.Close
' log count of uploaded data records
Call PrintLog("Records loaded on " & Now & ": " & cnt & " symbol records")
ExportExcelDataToAzureDb = True
End Function
If anyone knows how to get this working with later versions of the ActiveX Data Object or the SQL Native Client Library please post another answer.
After trying many solution, i have come to the conclusion that the most important thing is
using Activex Data Object 2.8 reference, SQL Native Client 11. using the lower version seems to work best
for my connection string i am using what is below format
connection = "Driver={SQL Server Native Client 11.0};" & _
"Server=tcp:YourDataBase.net,1433;" & _
"Database=YourDatabaseName;" & _
"Uid=XXXX;" & _
"Pwd=XXXXXXX;" & _
"Encrypt=yes;Connection Timeout=30;"
Hope this help other people. This was super frustrating for me

Keep Excel sub runing after calling access sub procedure

within my Excel sub I am calling an Access sub procedure that runs in the background (DB does not open) and updates a table in my DB. Everything works perfect, except that my Excel Sub will not proceed to the next line of code until Access is done running it's sub I called from Excel. So, my question is this...is there any way to call/run an Access Macro/Sub procedure from within an Excel Sub and have Excel proceed through the rest of the code and not have to wait for the Access Macro/sub to finish in order to proceed? Code: below:
Set acObj = CreateObject("Access.Application")
acObj.Application.Visible = False
acObj.OpenCurrentDatabase "C:\Intraday Data\Intraday.accdb"
acObj.Application.Run "RunData"
MsgBox "Done!"
So Basically I want to get the done prompt right away w/o having to wait 30 seconds for the access procedure to finish...anyone have any insight on this they can share w/ me please?
Thanks!
Here's some simple code to demonstrate an asynchronous call to an Access procedure:
Sub ExecuteAccessActionQuery()
' Sample demonstrating how to execute an action query in an Access accdb asynchronously
' Requires a reference to a Microsoft ActiveX Data Objects library
Dim cn As ADODB.Connection
Dim strQuery As String
Dim strPathToDB As String
Dim dTimer As Double
' Change path and query name as necessary
strPathToDB = "C:\some path\database name.accdb"
strQuery = "qmtTempTable"
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & strPathToDB & ";"
.Open
End With
dTimer = Timer
cn.Execute strQuery, , adCmdStoredProc + adAsyncExecute
MsgBox Timer - dTimer
Set cn = Nothing
End Sub
You can emulate an asynchronous call in VBA by creating a trigger in the client application which will then execute the required subroutine. This technique is described well here (note the 1 second time delay required a few posts down):
http://social.msdn.microsoft.com/Forums/en-US/0546f8eb-d786-4037-906e-1ee5d42e7484/asynchronous-applicationrun-call?forum=isvvba
Currently, you are manipulating data in the Access tables without initiating the application, so you can either use a new instance of Excel in which to create the trigger, or you can open the Access application and do it there (http://msdn.microsoft.com/en-us/library/office/aa213969%28v=office.11%29.aspx). Either way, you must have another independent application (process) within which to execute your 'asynchronous' routine.

Emulating a SHIFT key press when using VBA to open an ms-access database secured by an mdw file?

I want to run recursively through a directory of *.mdb files and search them to see which ones have a specific linked table.
These files are secured using several *.mdw files. I did not write any of them, but I am their maintainer.
I've found a way to do this, but it's too interactive; I need it to be non-interactive; since some of these *.mdbs I'm searching use an Autoexec macro.
From what I understand executing an Autoexec macro can be avoided if one holds the SHIFT key while opening them; however I use the command line in my macro to open these files, and there doesn't seem to be a way to hold the shift key.
I've found another example, which does allow you to hold down the shift key to avoid the Autoexec macro (see Bypassing Startup Settings When Opening a Database), but which does not allow you to unlock the database with an *.mdw file, because the OpenCurrentDatabase() method does not have a parameter for an *.mdw file
If your goal is simply to check whether a db file contains a specific linked table, you can use the ADO OpenSchema method. With that approach, you don't need to open the db file in an Access application session, so the AutoExec macro does not run.
Below is an example using late binding. I left comment notes in case you prefer early binding. Change the Provider if your Access version is older than 2007.
Since you're using Access user-level security, you will also have to adapt the connection string to include the path to your MDW and supply the Access security user name and password. Here is an example connection string (using the Jet 4 provider) from ConnectionStrings.com. I split the single-line string on the semicolons for readability:
Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\mydatabase.mdb;
Jet OLEDB:System Database=system.mdw;
User ID=myUsername;
Password=myPassword;
Public Function HasLinkedTable(ByVal pDb As String, _
ByVal pTable As String) As Boolean
Const adSchemaTables = 20&
Dim cn As Object ' ADODB.Connection
Dim rs As Object ' ADODB.Recordset
Dim strConnect As String
Dim blnReturn As Boolean
strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & pDb & ";"
'Set cn = New ADODB.Connection
Set cn = CreateObject("ADODB.Connection")
cn.Open strConnect
Set rs = cn.OpenSchema(adSchemaTables)
With rs
Do While Not .EOF
If !TABLE_NAME = pTable And !TABLE_TYPE = "LINK" Then
'Debug.Print !TABLE_NAME, !TABLE_TYPE
blnReturn = True
Exit Do
End If
.MoveNext
Loop
.Close
End With
cn.Close
Set cn = Nothing
HasLinkedTable = blnReturn
End Function

Access - Excel Integration

Hey all, have been working on designing a new database for work. They have been using Excel for their daily reports and all the data is stored in there, so I decided to have the back-end of the database in Access and the front-end in Excel, so any analytical work can be easily performed once all the data has been imported into Excel.
Now I'm fairly new to VBA, slowly getting used to using it, have written some code to transfer one of the calculated tables from Access to Excel:
Option Explicit
Public Const DataLocation As String = "C:\Documents and Settings\Alice\Desktop\Database\TestDatabase21.accdb"
Sub Market_Update()
Call ImportFromAccessTable(DataLocation, "Final_Table", Worksheets(2).Range("A5"))
End Sub
Sub ImportFromAccessTable(DBFullName As String, TableName As String, TargetRange As Range)
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
' open the database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";"
Set rs = New ADODB.Recordset
With rs
' open the recordset
' .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
' all records
.Open "SELECT * FROM Final_Table", cn, , , adCmdText
' filter records
For intColIndex = 0 To rs.Fields.count - 1 ' the field names
TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data
End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
Sub Company_Information()
Dim companyName As String
On Error GoTo gotoError
companyName = Application.InputBox(Prompt:="Enter Company Name", _
Title:="Company Name", Type:=2)
Exit Sub 'Don't execute errorhandler at end of routine
gotoError:
MsgBox "An error has occurred"
End Sub
The above code works fine and pulls up the desired calculated table and places it in the right cells in Excel.
I've got two problems that I'm having trouble with; firstly I have some cell-formatting already done for the cells where the data is going to be pasted into in Excel; I want it to apply the formatting to the values as soon as they are pasted in Excel.
Secondly; I have an add-on for Excel which updates some daily Stock Market values; these values need to be transferred into Access at the end of each working day, to keep the database maintained, I tried some code but have been having some problems with it running.
The code for this part can be seen following:
Sub UPDATE()
Dim cnt As ADODB.Connection
Dim stSQL As String, stCon As String, DataLocation As String
Dim stSQL2 As String
'database path - currently same as this workbook
DataLocation = ThisWorkbook.Path & DataLocation
stCon = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & DataLocation & ";"
'SQL code for GL Insert to Access
stSQL = "INSERT INTO Historical_Stock_Data SELECT * FROM [Portfolio] IN '" _
& ThisWorkbook.FullName & "' 'Excel 8.0;'"
'set connection variable
Set cnt = New ADODB.Connection
'open connection to Access db and run the SQL
With cnt
.Open stCon
.CursorLocation = adUseServer
.Execute (stSQL)
End With
'close connection
cnt.Close
'release object from memory
Set cnt = Nothing
End Sub
I get the following error with this.
Run-time Error '-2147467259 (80004005)'
The Microsoft Jet database engine cannot open the file 'Cocuments and Settings\Alice\Desktop\Database'. It is already opened exclusively by another user or you need permission to view its data.
I'm fairly new to databases, VBA and Access so any help would be greatly appreciated.
Also I have been told that the above method of having an Excel front-end and Access back-end is not recommended but alot of the analysis they conduct is done through Excel, and the charts feature in Excel is much better than Access in my experience atleast; and that is also one of the requirements for this project.
Thank you advance!
Solution to your first problem:
Sorry to be the bearer of bad news, but your entire first module is unnecessary. Instead, try:
Go to Data->Import External Data->Import Data, select your Access file, select your table, and presto! done!
Right-click on your new "External Data Range" to see a number of options, some related to formatting. You can even keep the original cell formatting and just update the values. I do this all the time.
To update the Excel data table later, there is a "External Data Range" toolbar that allows you to refresh it as well as a "refresh all" option to refresh every table in the Excel file. (You can also automate this thru code. It'll take some trial and error, but you're definitely up to the task)
Regarding your second problem
I've never used it, but there is also a "New Web Query" option in there as well. I assume it can be manipulated and updated the same way.
And lastly
Your choice of the Excel front-end and the Access back-end sounds good for your needs. It gets the data to your analysts in a medium they are familiar with (Excel) while keeping the calculations out of the way in Access. Technically, you could try putting all your calculations in Excel, but that might the Excel file much bigger and slower to open.
Do the data entry/updating/reviewing in Access. One of Access' strengths is using forms that allow you to update the tables without any code. Then allow the users to easily export the data to Excel such as by clicking on some command buttons.
Modules: Sample Excel Automation - cell by cell which is slow
Modules: Transferring Records to Excel with Automation
nothing wrong in principle with the excel/access pairing. I'm not familiar with ADO (I use DAO), but your error message seems to be indicating that the path to the datasource is not fully formed; or you already have it opened and hence are locking it.

Resources