Dinesh Santhalingam

Dinesh Santhalingam

  • NA
  • 737
  • 370.3k

Insert Values to SQLTable from VBA

Mar 25 2018 10:43 PM
I need to insert a Values to the table in SQL Server from VBA .I tried Something I mentioned below .But I couldn't able to achieve my results .please guide me to solve this .I am using Exact Conn String in my Option even though its not functioning.
  1. Sub To_SQL()  
  2.   
  3. Dim conn As ADODB.Connection  
  4. Dim cmd As ADODB.Command  
  5. Dim strSQL As String  
  6.   
  7. 'Create a new Connection object  
  8. Set conn = New ADODB.Connection  
  9.   
  10. 'Set the connection string  
  11. conn .ConnectionString = "Data Source=55.50.53.9;Initial Catalog=Management;Persist Security Info=True;User ID=licenceusr;Password=******"  
  12.   
  13. 'Open the Connection to the database  
  14. conn .Open **
  15.  conn .Execute "insert into MyTableName (firstname, lastname) values('""Dhini""','""Dhini""')"  
  16. 'Create a new Command object  
  17. Set cmd = New ADODB.Command  
  18.   
  19. End Sub()  
Thanks in advance.

Answers (3)