Guest User

Untitled

a guest
Aug 11th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. Issues declaring an SQL Connection in VBA Excel?
  2. Private Sub CommandButton1_Click()
  3.  
  4. 'This sub-routine defines an ADOBD command to return variables to VBA from SQL Stored Procedures.
  5. 'The ADODB command executes a Stored Procedure on the SQL Server (cmd.CommandText = [Stored Procedure Name])
  6. 'Input requirements from the Stored procedure are declared as variants at the start of the sub-routine.
  7.  
  8. Dim cnt As ADODB.Connection
  9. Dim rst As ADODB.Recordset
  10. Dim cmd As ADODB.Command
  11. Dim stCon As String 'SQL Connection string
  12. Dim stProcName As String 'Stored Procedure name
  13.  
  14. 'Declare variables for Stored Procedure
  15. Dim user As String
  16. Dim password As String
  17. Dim companyId As Integer
  18.  
  19. 'Set ADODB requirements
  20. Set cnt = New ADODB.Connection
  21. Set rst = New ADODB.Recordset
  22. Set cmd = New ADODB.Command
  23. 'Define database connection string
  24. stCon = "Provider=SQLOLEDB.1;"
  25. stCon = stCon + "Password=Cosin123;"
  26. stCon = stCon + "Persist Security Info=True;"
  27. stCon = stCon + "User ID=easywork;"
  28. stCon = stCon + "Initial Catalog=EasyWorkDesenvolvimento;"
  29. stCon = stCon + "Data Source=192.168.0.21;"
  30. stCon = stCon + "Use Procedure for Prepare=1;"
  31. stCon = stCon + "Auto Translate=True;"
  32. stCon = stCon + "Packet Size=4096;"
  33. stCon = stCon + "Workstation ID=WEBMOBILE04;"
  34. stCon = stCon + "Use Encryption for Data=False;"
  35. stCon = stCon + "Tag with column collation when possible=False;"
  36.  
  37.  
  38. 'Open database connection
  39. cnt.ConnectionString = stCon
  40. cnt.Open
  41.  
  42. ' Defines the stored procedure commands
  43. stProcName = "SP_GE_EFFECTUSERLOGON" 'Define name of Stored Procedure to execute.
  44. cmd.CommandType = adCmdStoredProc 'Define the ADODB command
  45. cmd.ActiveConnection = cnt 'Set the command connection string
  46. cmd.CommandText = stProcName 'Define Stored Procedure to run
  47.  
  48.  
  49. 'set parameters to be executed
  50. user = UserForm2.TextBox1.Text
  51. password = UserForm2.TextBox2.Text
  52.  
  53.  
  54.  
  55. Set prm = cmd.CreateParameter("@user", adVarChar, adParamInput, 50, user)
  56. Set prm1 = cmd.CreteParameter("@password", adVarChar, adParamInput, 50, password)
  57. 'Append parameters
  58. With cmd
  59. .Parameters.Append prm, prm1
  60. End With
  61.  
  62. 'Execute stored procedure and return to a recordset
  63. rst.Open cmd.Execute
  64.  
  65. myReturn = rst.Fields("myVariable").Value
  66.  
  67. Call Sub-Routine_That_Uses_The_Returned_Data
  68.  
  69. 'Close database connection and clean up
  70. If CBool(rst.State And adStateOpen) = True Then rst.Close
  71. Set rst = Nothing
  72.  
  73. If CBool(cnt.State And adStateOpen) = True Then cnt.Close
  74. Set cnt = Nothing
  75.  
  76. End Sub
Add Comment
Please, Sign In to add comment