Simple Way to Connect Microsoft Excel to VB.Net

In this tutorial, I will teach you how to connect Microsoft Excel to VB.Net. This simple method will illustrate how to connect MS Excel to Visual Basic 2015. It will be a big help for an individual to create in their own way that requires this method. Let’s begin.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in visual basic. ps1

Step 2

Add a button inside a form. ps2

Step 3

Double click the button and add the following codes for connecting Microsoft Excel to Visual Basic 2015.
  1. ' it represents the connection of data source
  2. Dim con As OleDb.OleDbConnection
  3. ' declare a variable to store the string path
  4. Dim string_path As String
  5. ' set a string path
  6. string_path = "c:/IT.xls"
  7. 'initailize the oledb connection with the specified connection string
  8. con = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & string_path & " ; " & "Extended Properties=Excel 8.0;")
  9.  
  10. Try
  11. 'opening connection
  12. con.Open()
  13. 'validating the connection
  14. If ConnectionState.Open Then
  15. MsgBox("Connected successfuly")
  16. con.Close()
  17. Else
  18. MsgBox("Unable to connect")
  19. End If
  20. Catch ex As Exception
  21. 'catching errors
  22. MsgBox(ex.Message)
  23. End Try
Output output

Add new comment