简体   繁体   English

Excel VBA-记住SQL ODBC用户/密码

[英]Excel VBA - Remember SQL ODBC User/Password

First time here, so I hope to get my answer, I looked throughout the Google but I couldn't find the answer. 第一次来这里是我希望得到答案,我在Google各处浏览,但找不到答案。

Well, I got VBA code which refresh a code in SQL on Exel. 好吧,我得到了VBA代码,该代码在Exel上的SQL中刷新了代码。

VBA code: Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False VBA代码: Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False

It's asking everytime my ODBC login/password and I try to refresh manually, and I'd like to run automatically by saving those information on Excel Cells or in VBA directly (I prefer a variable that is linked in excel cells in "Validação de Dados" sheets). 每当我的ODBC登录名/密码都询问时,我会尝试手动刷新,并且我想通过将这些信息保存在Excel单元格或直接在VBA中自动运行(我更喜欢在“Validaçãode Dados”中的excel单元格中链接的变量”)。 And after that, making it better by forcing the user to insert those information in a form (to keep better looking). 然后,通过强制用户以表格形式插入这些信息(以保持更好的外观)来使其变得更好。 But the problem is that everytime I tried to run the macro, it gets an error (something like "authenticantion failure" ). 但是问题在于,每次我尝试运行宏时,都会收到错误消息(类似“身份验证失败” )。

  • My SQL code in Excel location: 我在Excel位置中的SQL代码:

SQL代码窗口

  • Asking about my user and login: 询问我的用户和登录名:

用户登录

  • Error getting by not saving user/login: 通过不保存用户/登录名而出错:

尝试使用VBA代码时出错

Office version: Microsoft Office 365 Business Office版本:Microsoft Office 365商业版

Thanks everyone! 感谢大家!

You should be able to use windows integrated authentication as well if you have an SQL Server. 如果您有SQL Server,则还应该能够使用Windows集成身份验证。 You'd have to add your user in SSMS. 您必须在SSMS中添加用户。

https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/using-integrated-authentication?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/using-integrated-authentication?view=sql-server-2017

I managed to ask the password from the standard window with this code: 我设法通过以下代码从标准窗口中询问密码:

ActiveWorkbook.Worksheets("Base").ListObjects(1).QueryTable.Refresh

"Base" is the name of your worksheet, and ListObjects(1) is if you have only one SQL query. “ Base”是工作表的名称,而ListObjects(1)是您只有一个SQL查询的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM