VBA to connect to SQL Server (I'm able to connect user to server using SSMS)

yshpjwxd  于 2023-08-02  发布在  SQL Server
关注(0)|答案(1)|浏览(165)

Our organization has a shared Excel document that is used to keep track of inventory where our accounting system fails. That being said, I've been working on connecting the two. To do so, I've created several scripts and SQL queries to make this work. However, I'm struggling making the queries update on the end users computers. I keep getting the database login prompt. What do I need to do to get this working?

Key pieces of information:

  1. I can login in the user into the SQL Server database using the credentials provided in the code using SSMS
  2. When running it on my computer, the code attached works fine.
  3. The SQL Server is on my computer
  4. The SQL Server is TCP/IP enabled

Code:

  1. Option Explicit
  2. ' Refreshing data from outside data sources
  3. ' Add the required references:
  4. ' - OLE Automation
  5. ' - Microsoft ActiveX Data Objects 2.8 Library
  6. ' - Microsoft Excel 16.0 Object Library
  7. ' - Microsoft Office 16.0 Object Library
  8. ' - Microsoft Outlook 16.0 Object Library
  9. Sub RefreshQueries()
  10. Dim dbConnection As ADODB.Connection
  11. Dim dbServerName As String, qbDbName As String, aggDbName As String
  12. Dim connectionStr As String, qbDataArray(3), connectionName
  13. On Error GoTo ErrorHandler
  14. ' Database server details
  15. dbServerName = "111.11.0.1" ' not actual server ip
  16. ' Database names
  17. qbDbName = "QbData"
  18. aggDbName = "NonQBInventory"
  19. ' Connection string (WITH USERNAME AND PASSWORD) ##########################################################
  20. connectionStr = "Provider=SQLOLEDB.1;" & _
  21. "Data Source=" & dbServerName & ";" & _
  22. "User ID=officeUser;" & _
  23. "Password=passWord;" & _
  24. "Connect Timeout=10;" ' Set the connection timeout value (in seconds)
  25. ' "Encrypt=yes;" & _
  26. ' "TrustServerCertificate=no;" & _ (TRIED ADDING THIS TO CONNECTION STRING)
  27. ' Establish the database connection
  28. Set dbConnection = New ADODB.Connection
  29. dbConnection.Open connectionStr
  30. On Error GoTo 0
  31. ' Refresh queries in QbData database
  32. ' Fill qbDataArray
  33. qbDataArray(0) = "Query - GetCustomers SQL"
  34. qbDataArray(1) = "Query - GetItems SQL"
  35. qbDataArray(2) = "Query - GetOpenSOs SQL"
  36. qbDataArray(3) = "Query - GetUnitOfMeasures SQL"
  37. For i = 0 To UBound(qbDataArray)
  38. connectionName = qbDataArray(i)
  39. ThisWorkbook.Connections(connectionName).Refresh
  40. Next i
  41. ' Refresh queries in database2 ############ FOR LATER #################################################
  42. 'Call FillNonQbDataSqlArray
  43. 'For i = 0 To UBound(nonQbData)
  44. 'queryName = nonQbData(i)
  45. 'With ThisWorkbook.Connections("Connection1") ' Replace "Connection1" with the actual name of the connection
  46. ' .OLEDBConnection.Connection = dbConnection
  47. ' .OLEDBConnection.CommandText = queryName ' Set the query name
  48. ' .Refresh
  49. 'End With
  50. 'Next i
  51. ' Close the database connection
  52. dbConnection.Close
  53. Set dbConnection = Nothing
  54. ' Refresh all other data connections in the workbook
  55. ThisWorkbook.RefreshAll
  56. Exit Sub
  57. ErrorHandler:
  58. ' Handle the error here (e.g., display a message box or write to a log file)
  59. MsgBox "An error occurred: " & Err.Description, vbExclamation
  60. On Error Resume Next
  61. ' Close the database connection (if it is open)
  62. If Not dbConnection Is Nothing Then
  63. If dbConnection.State = adStateOpen Then
  64. dbConnection.Close
  65. End If
  66. Set dbConnection = Nothing
  67. End If
  68. End Sub
hgtggwj0

hgtggwj01#

Looking at Microsoft's documentation, it appears that when you go to "Get Data", in the Data ribbon, selecting the "From Database" option only works when the query is native (i.e. the query is occurring directly on the computer/server which the SQL server is on).

By selecting "From other sources" and then "From OLEDB", I was able to make the query run from whatever computer by providing the below code:

  1. ThisWorkbook.RefreshAll

If it is the users first time refreshing the queries on their workbook, they'll be asked to input a username and password. I suppose, I could just insert it programmatically as well.

相关问题