Refresh a connection in Excel to SQL Server (with password) using VBA

你。 提交于 2019-12-01 10:20:32

问题


I have 10 Excel files that are connected to a SQL Server by OLEDB and I need to refresh them everyday one by one manually. When I refresh them I need to insert a password. I tried to make a macro to automate this process, but I didn't succeed. I did this function to automate one of my workbooks:

Sub updateABC()
  Workbooks("Teste").Connections("SQL Server_Azure1").OLEDBConnection.Refresh
End Sub

And the Excel ask for the password to SQL Server logon. Is there any way to put this password automatically using vba?

Thanks.


回答1:


There is a way in Excel without VBA:

  1. You can go to Data then Connections
  2. Click Properties on the top Connection
  3. Click the Definition Tab
  4. There is a tick box "Save Password" about half way down, you will get a prompt about the password being stored un-encrypted.
  5. Say yes to this
  6. Click OK

Do the same for all of your connections

You can then refresh all connections and you will be prompted to enter the password for each connection. Once you do this and save the document it will not ask you for your passwords again.

You can also tick "Refresh data when opening the file" on the Usage tab to have the connections refreshed when the workbook is opened.



来源:https://stackoverflow.com/questions/47271322/refresh-a-connection-in-excel-to-sql-server-with-password-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!