Re: Simple: Excel Jet 4.0 Link vb 2005 by Rhyd
Rhyd
Tue Mar 15 06:25:02 CST 2005
"Paul Clement" wrote:
> On Tue, 8 Mar 2005 01:49:07 -0800, "Rhyd" <Rhyd@discussions.microsoft.com> wrote:
>
> ¤
> ¤ I understand this part of the code:
> ¤
> ¤ cmd = conn.CreateCommand()
> ¤
> ¤ cmd .CommandText = "SELECT * FROM [Sheet1$]"
> ¤ Prices = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
> ¤
> ¤ Its this next part I am having trouble with as I don't understand what this
> ¤ points to:
> ¤
> ¤ While (Prices.Read())
> ¤ Console.WriteLine(Prices.Item(0).ToString)
> ¤ Console.WriteLine(Prices.Item(1).ToString)
> ¤ End While
> ¤
> ¤ From the while loop I don't understand what the writeline command is
> ¤ writing, or where it is writing it. When this command is used in the program,
> ¤ there are no errors, except nothing happening.
> ¤
>
> The WriteLine code is simply displaying the value of each column (for each row in the DataReader) to
> the Console Window so you can see what the values are. It's just an example.
>
> ¤ Here is an example of what I want to do but in a different way:
> ¤
> ¤ Dim value as Single
> ¤ value = xlsheet.cells(2,2). Value
> ¤ Textbox1.Text = value
> ¤
> ¤ Before this code I opened an excel file, declaring the sheet the information
> ¤ was on as xlsheet.
> ¤ This is what I was doing before, but I identified the problem tha not all of
> ¤ the computers in the place of work have excel installed. As you can see a
> ¤ specific value is defined in a textbox after an action, I just need to apply
> ¤ this to the oledatareader or dataset
>
> If you need to retrieve the data from specific cells in the Sheet then you probably want to specify
> a range in your query. For example, the following will only query the cell 2,2 (a single row/column)
> of Sheet8 (or from B2 to B2):
>
> Dim ConnectionString As String
>
> Dim ExcelConnection As New System.Data.OleDb.OleDbConnection(ConnectionString)
> ExcelConnection.Open()
>
> Dim ExcelCommand As System.Data.OleDb.OleDbCommand
> ExcelCommand = ExcelConnection.CreateCommand()
>
> ExcelCommand.CommandText = "SELECT * FROM [Sheet8$B2:B2]"
> Dim ExcelReader As OleDbDataReader
> ExcelReader = ExcelCommand.ExecuteReader(CommandBehavior.SequentialAccess)
>
> Dim CellValue As String
>
> If (ExcelReader.Read()) Then
> CellValue = ExcelReader.Item(0).ToString
> End If
>
> ExcelConnection.Close()
>
>
> Paul ~~~ pclement@ameritech.net
> Microsoft MVP (Visual Basic)
>
Thanks again Paul,
but after using the code you have advised, with a little modification to
suit what I am doing, the textbox just returns a zero value, which isn't the
value in the cell. My co-ordinates and the sheet is correct, and I feel that
this zero is just a null value, I don't know why though.
Do you know how I can create a connection through the designer view with the
database explorer, because i have also tried this but I am informed when I
test the connection that it only reads .mdb's and .mdf's.
Would an odbc connection be a better option, considering what I want to
achieve and the circumstances??
Thanks again
Rhyd