Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there a simple way of importing numeric data from a single cell in excel (2003) into
a textbox in a visual basic form?
Posted

You can Take all the data from the excel into datagrid view and Select the perticular cell of datagrid view and take cell data to textbox..
 
Share this answer
 
 
Share this answer
 
Ok I would create a class to handle the OleDB Connection(I'm assuming you're using VB .NET), so that you can reuse code; lets assume that you have an Excel Database with Names and Ages of people and you would like your Textbox to display the age of the selected person.

Public Class ExcelReader

         Private Function ConnString(ByVal str As String) As String
                 'str is the Path to the Excel File.
              Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
              + str + ";Extended Properties=""Excel 8.0;HDR=Yes"""
         End Function

         Public Sub RetrieveRecord(ByVal str As String, ByVal Name As String)
               'str is again the Path to the Excel File, and Name is the Name
               'you would like to find the age for.
              Dim Conn As New OleDBConnection
              Dim Comm As New OleDBCommand
              Dim DReader As OleDBDataReader

              Conn.ConnectionString = ConnString(str)
              Comm.Connection = Conn
              Comm.CommandText = "Select * From [Sheet1$] Where Name =" & Name

              Try
                Conn.Open

                DReader = Comm.ExecuteReader
                txtDisplayAge.Text = DReader.GetString(1)
                 'OleDBDataReader.GetString() takes a zero based index
                 'to reference the column you would like to retrieve
                 'and it also reads the field as a string so that there
                 'is no need to use the .ToString() Method.

                 'Close the OleDBDataReader Object
                 DReader.Close()

                 'Close the Connection
                 Conn.Close()
               Catch ex As Exception
                  MessageBox.Show("Error")
                 Conn.Close()
               End Try
            End Sub


I hope this helps you out.
 
Share this answer
 
use ur textbox where i use dd,mm,yyyy veriable


VB
Dim app As Excel.Application
Dim wb As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim var As Variant

Set app = New Excel.Application
Dim path As String

Set wb = app.Workbooks.Open(filepath)
Set sheet = wb.Worksheets.Item("JAN-2012")

Dim c As Integer
c = 2

With sheet
Dim lastrow As Integer
Do While .Cells(c, 1) <> ""
c = c + 1
Loop
End With

With sheet
Dim strdata As String
Dim i As Integer
Dim n As Integer
Dim dy1 As String
Dim dy As String

dy = DateTime.Date
dy1 = Format(dy, "dd/mm/yyyy")

For i = 2 To c - 1 Step 1
strdata = .Cells(i, 1)
dd = Format(strdata, "dd/mm/yyyy")
mm = Format(strdata, "mm/yyyy")
yy = Format(strdata, "yyyy")

dd1 = Format(dy1, "dd/mm/yyyy")
mm1 = Format(dy1, "mm/yyyy")
yy1 = Format(dy1, "yyyy")

If dd = dd1 Then
day = day + 1
End If

If mm = mm1 Then
month = month + 1
End If

If yy = yy1 Then
year = year + 1
End If

Next i
End With
Dim d1 As String
Dim mo1 As String
Dim ye1 As String

d1 = decimal_a(CStr(day))
mo1 = decimal_a1(CStr(month))
ye1 = decimal_a2(CStr(year))



Dim str As String
str = "M;" + d1 + ";" + mo1 + ";" + ye1 + ";;"
MsgBox str

'MsgBox day
'MsgBox month
'MsgBox year
wb.Close


app.Quit
Set sheet = Nothing
Set eb = Nothing
Set app = Nothing
 
Share this answer
 
v2
Comments
S Kersten 27-Feb-22 12:49pm    
Solution 1 link does not work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900