Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have tabel1 in accesses database and i need to read Field1 from it
but i didont need to read all Fiedld1 lines
i need to read only last line number on this filed

Field1 contain numbers like
001
002
003
004

and i need to read only last number witch in this case is 004 as example
how can i do it in dr.Read ???

Dim Cmd As New OleDbCommand 'cmd as command to use
        Cmd.Connection = conn 'open connection to database
        Cmd.CommandText = "select * from Table1
        conn.Open() 'open connection
        dr = Cmd.ExecuteReader 'start to read from database
        While dr.Read

            Textbox1.text = dr("Field1").ToString()

        End While
        dr.Close()
        conn.Close()


What I have tried:

<pre>Dim Cmd As New OleDbCommand 'cmd as command to use
        Cmd.Connection = conn 'open connection to database
        Cmd.CommandText = "select * from Table1
        conn.Open() 'open connection
        dr = Cmd.ExecuteReader 'start to read from database
        While dr.Read

            Textbox1.text = dr("Field1").ToString()

        End While
        dr.Close()
        conn.Close()
Posted
Updated 9-Jun-20 9:03am
Comments
[no name] 9-Jun-20 9:34am    
Think about/try something like SELECT MAX(Field1) FROM Table1
Keep in mind, it is usually very delicate for fields of the type CHAR (x)
Maciej Los 9-Jun-20 9:54am    
That doesn't mean the last value ;)
[no name] 9-Jun-20 10:08am    
Basically you are right, but I think/speculate it is, what OP is looking for ;)
Richard MacCutchan 9-Jun-20 9:47am    
What result do you see when you run that code?
Maciej Los 9-Jun-20 9:58am    
There's no "last value"! If datatable object has got primary key, indexes and foreign keys, a 004 value - which is not related to primary key - have NOT to be the last one.
You should provide table schema and more details about the way you're creating such of value.

MS Access, just like SQL Server, has a MAX[^] function which will return the Maximum value found within a field.
As the updated query will only return a single value, you can then use the ExecuteScalar() method and skip using the DataReader.
VB
Dim Cmd As New OleDbCommand
	Cmd.Connection = conn
	Cmd.CommandText = "SELECT Max(Field1) FROM Table1"
	conn.Open()

'	dr = Cmd.ExecuteReader 'start to read from database
'	While dr.Read
'		Textbox1.text = dr("Field1").ToString()
'	End While
'	dr.Close()

	Textbox1.text = Cmd.ExecuteScalar().ToString()

	conn.Close()
Cmd.Dispose()
References:
Min, Max functions (Microsoft Access SQL) | Microsoft Docs[^]
OleDbCommand.ExecuteScalar Method (System.Data.OleDb) | Microsoft Docs[^]

Update
To avoid SQL Injection, never ever should a query be created from concatenating commands and variables. The proper way to add variables to a command is by using the Parameter[^] collection of the command object.

Here is an updated snippet for you with your variables added in
VB
Cmd.CommandText = "SELECT Max(ID) FROM Drums WHERE DrumType = @DrumType AND Flange = @Flange"
Cmd.Parameters.AddWithValue("@DrumType", ComboBox7.SelectedItem)
Cmd.Parameters.AddWithValue("@Flange", YY)

conn.Open()
Added Reference:
SqlParameterCollection Class (System.Data.SqlClient) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
katkot_rewsh 9-Jun-20 15:06pm    
Excellent, and many thanks
I found the solution

Cmd.CommandText = "select MAX (ID) from Drums where DrumType = '" & ComboBox7.SelectedItem & "' and Flange = " & YY & "" 'select data from table
        conn.Open() 'open connection

        Label54.Text = Cmd.ExecuteScalar

        dr.Close()
        conn.Close()
 
Share this answer
 
Comments
MadMyche 9-Jun-20 15:45pm    
! You just created a SQL Injection Vulnerability !

I will update my answer with the correct way to do this

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