Import excel cell value into window form application text box

Joined
Feb 3, 2015
Messages
8
Programming Experience
Beginner
Hi, I've created a windowsform application into VB.net using excel as a data base. one the button click even all the filled data export into the excel file. now I want on the same time one particular excel value come into the windows form defined text box.

below is the code. please modify the same.




Imports System.Data.OleDb
Public Class Form1

Dim Cn As New OleDbConnection
Dim cm As New OleDbCommand

Private Sub BtnSave_Click(sender As System.Object, e As System.EventArgs) Handles BtnSave.Click

Cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=C:\Users\rajkumar.shukla\Desktop\Testing\testing.xlsm;extended properties=excel 8.0"
Cn.Open()

With cm
.Connection = Cn
.CommandText = "insert into [testing$]values('" & TxtComp.Text & "','" & TxtDep.Text & "','" & TxtVer.Text & "','" & TxtDat.Text & "', '" & TxtIni.Text & "','" & TxtSig.Text & "','" & TxtSub.Text & "','" & Txtyear.Text & "')"
.ExecuteNonQuery()

End With

Cn.Close()

MsgBox("Reference Number Generate Successfully", MsgBoxStyle.Information, Text)

End Sub
End Class
 
You clearly already know how to work with the Excel file as a database. In order to retrieve data from it you would do just as you would for any other database. You would create and open a connection and execute a command. ExecuteScalar would be the method to call if you want just one value or you can call ExecuteReader to get multiple values from one or more rows.

That said, I really must suggest NOT using Excel as a database because it is NOT a database. If you have data in Excel files and must use them as a data source then so be it but if it's within your power to select a database for your app then at least use Access or maybe something better like SQLite, SQL Server CE or even SQL Server Express.
 
Back
Top