excel 本机数据库查询 - 默认接受弹出消息?
excel native database query - popup message accept by default?
我们有一个直接 link 从 excel 到我们的 redshift 数据库 - 这太棒了。
然而,当我们刷新数据时,用户必须点击很多 "accept" 类型 windows,选择 运行(下面的屏幕截图)
知道如何将其设置为默认接受吗?
关于本机查询的官方文档似乎没有提到它:https://support.office.microsoft.com/en-US/article/Import-Data-from-Database-using-Native-Database-Query-Power-Query-f4f448ac-70d5-445b-a6ba-302db47a1b00
将 DisplayAlerts 设置为 False 将导致 Excel 自动为所有弹出窗口选择 "default" 选项*,对于本机数据库查询是 运行 选项。
Application.DisplayAlerts = False
记得在 运行 查询后将其设置回 True。
这解决了您自动接受这些 windows 的问题。
但是,这可能会导致一个单独的问题,即不允许用户在需要时输入凭据,而是简单地取消该过程。如果需要,这可以通过错误处理来解决。在 运行 查询之前,设置错误处理程序:
On Error GoTo EnterCredentials
并且在子例程的末尾,有一个错误处理块重新启用 DisplayAlerts,以便输入密码的必要提示可用。请注意,错误处理块必须使用 "Resume" 而不是 "Resume Next",因为您希望 Excel 再次尝试连接并显示警报。
下面的示例错误处理块使用布尔变量 bolTriedPassword 来跟踪之前是否已引发错误,以向用户显示更合适的错误消息。 (它还将服务器名称保存到变量 strServerName 中。)
它为用户提供了一个取消选项,以逃避一个永无止境的循环,并自动完全结束其他类型的连接错误。您可能必须根据 connection/login 的设置方式进行一些更改,但如果需要,它应该可以帮助您入门。
Exit Sub
EnterCredentials:
If InStr(Err.Description, "credentials provided for the SQL source are invalid") > 0 Or Err.Number = 1004 Then
'Network account doesn't have access. Allow alerts to display the login dialog. If Cancel is pressed, stop entirely.
'If error occurs a 2nd time, then password was not entered correctly.
Application.DisplayAlerts = True
If MsgBox(IIf(bolTriedPassword, "A valid username and password were not entered for ", "Your network account has not been granted access to ") & strServerName & "." & vbCrLf & vbCrLf & _
IIf(bolTriedPassword, "Please ensure you selected DATABASE as the login type and entered the correct username and password." & vbCrLf & vbCrLf, "") & _
"If you have a database username and password, press OK, then: " & vbCrLf & _
" - Select DATABASE on the left of login window." & vbCrLf & " - Click RUN on the native query window.", _
vbOKCancel + vbExclamation, "Error Establishing Connection") = vbCancel Then End
bolTriedPassword = True
Else
'For any other error, display error description and stop entirely
MsgBox "Error connecting to SQL Server:" & vbCrLf & vbCrLf & Err.Description, vbCritical, "Error Establishing Connection"
End
End If
Resume
End Sub
*Excel 不选择 SaveAs 方法弹出窗口的默认选项,而是选择更有用的选项:
https://docs.microsoft.com/en-us/office/vba/api/excel.application.displayalerts
When using the SaveAs method for workbooks to overwrite an existing
file, the Confirm Save As dialog box has a default of No, while the
Yes response is selected by Excel when the DisplayAlerts property is
set to False. The Yes response overwrites the existing file.
我们有一个直接 link 从 excel 到我们的 redshift 数据库 - 这太棒了。
然而,当我们刷新数据时,用户必须点击很多 "accept" 类型 windows,选择 运行(下面的屏幕截图)
知道如何将其设置为默认接受吗?
关于本机查询的官方文档似乎没有提到它:https://support.office.microsoft.com/en-US/article/Import-Data-from-Database-using-Native-Database-Query-Power-Query-f4f448ac-70d5-445b-a6ba-302db47a1b00
将 DisplayAlerts 设置为 False 将导致 Excel 自动为所有弹出窗口选择 "default" 选项*,对于本机数据库查询是 运行 选项。
Application.DisplayAlerts = False
记得在 运行 查询后将其设置回 True。
这解决了您自动接受这些 windows 的问题。
但是,这可能会导致一个单独的问题,即不允许用户在需要时输入凭据,而是简单地取消该过程。如果需要,这可以通过错误处理来解决。在 运行 查询之前,设置错误处理程序:
On Error GoTo EnterCredentials
并且在子例程的末尾,有一个错误处理块重新启用 DisplayAlerts,以便输入密码的必要提示可用。请注意,错误处理块必须使用 "Resume" 而不是 "Resume Next",因为您希望 Excel 再次尝试连接并显示警报。
下面的示例错误处理块使用布尔变量 bolTriedPassword 来跟踪之前是否已引发错误,以向用户显示更合适的错误消息。 (它还将服务器名称保存到变量 strServerName 中。)
它为用户提供了一个取消选项,以逃避一个永无止境的循环,并自动完全结束其他类型的连接错误。您可能必须根据 connection/login 的设置方式进行一些更改,但如果需要,它应该可以帮助您入门。
Exit Sub
EnterCredentials:
If InStr(Err.Description, "credentials provided for the SQL source are invalid") > 0 Or Err.Number = 1004 Then
'Network account doesn't have access. Allow alerts to display the login dialog. If Cancel is pressed, stop entirely.
'If error occurs a 2nd time, then password was not entered correctly.
Application.DisplayAlerts = True
If MsgBox(IIf(bolTriedPassword, "A valid username and password were not entered for ", "Your network account has not been granted access to ") & strServerName & "." & vbCrLf & vbCrLf & _
IIf(bolTriedPassword, "Please ensure you selected DATABASE as the login type and entered the correct username and password." & vbCrLf & vbCrLf, "") & _
"If you have a database username and password, press OK, then: " & vbCrLf & _
" - Select DATABASE on the left of login window." & vbCrLf & " - Click RUN on the native query window.", _
vbOKCancel + vbExclamation, "Error Establishing Connection") = vbCancel Then End
bolTriedPassword = True
Else
'For any other error, display error description and stop entirely
MsgBox "Error connecting to SQL Server:" & vbCrLf & vbCrLf & Err.Description, vbCritical, "Error Establishing Connection"
End
End If
Resume
End Sub
*Excel 不选择 SaveAs 方法弹出窗口的默认选项,而是选择更有用的选项:
https://docs.microsoft.com/en-us/office/vba/api/excel.application.displayalerts
When using the SaveAs method for workbooks to overwrite an existing file, the Confirm Save As dialog box has a default of No, while the Yes response is selected by Excel when the DisplayAlerts property is set to False. The Yes response overwrites the existing file.