ms 列中的最大值访问并显示在文本框中 VB.NET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16317479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Largest value in a column of ms access and display in a text box VB.NET
提问by Thanzeem
How to find the largest value in a column of a access table. and displayed in a text box. I give 101 as the default value of the column and the table is empty. I try like this.. But its not working. Code is given below
如何在访问表的列中找到最大值。并显示在文本框中。我给 101 作为列的默认值,表为空。我像这样尝试..但它不起作用。代码如下
Dim empid As Integer
empid=101
TXTEMPID.Text=empid
getConnect()
Dim strSQL As String = "SELECT MAX(EMP_ID) FROM EMPLOYEE "
Dim cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader
cmd = New OleDb.OleDbCommand(strSQL, Conn)
Try
Conn.Open()
Reader = cmd.ExecuteReader()
If Reader.Read Then
empid = CInt(Reader("EMP_ID"))
End If
MessageBox.Show(empid)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Conn.Close()
End Try
TXTEMPID.Text = empid + 1
回答by Gord Thompson
If the table is empty then there is no "largest value" because the table contains no values at all.
如果表为空,则没有“最大值”,因为该表根本不包含任何值。
Edit
编辑
Ah, okay. It sounds like you were tripping over the fact that, for an empty table, expressions like DMax("EMP_ID","YourTable")will return Null, and Null + 1will return Null, so how do we get started? You could try something like...
啊好吧。听起来您好像被这样一个事实绊倒了,对于一个空表,像DMax("EMP_ID","YourTable")will returnNull和Null + 1will returnNull这样的表达式,那么我们如何开始呢?你可以尝试类似...
Me.txtEMP_ID.Value = Nz(DMax("EMP_ID","YourTable"), 100) + 1
...in the Form Loadevent, although I should mention that this type of approach can cause problems is your database is (or will ever become) multi-user.
...在这种情况Form Load下,尽管我应该提到这种方法可能会导致问题是您的数据库是(或将成为)多用户。
回答by Shafqat Masood
here is the query
这是查询
Select Top 1 MAX(col 2), col1 from Table1 group by col1
to get the results you have to perform
为了得到你必须执行的结果
Dim cnnOLEDB As New OleDbConnection
Dim cmdOLEDB As New OleDbCommand
Dim strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.Environment.CurrentDirectory & "\URDataBaseName.mdb"
cnnOLEDB.ConnectionString = strConnectionString
cnnOLEDB.Open()
cmdOLEDB.CommandText ="Select Top 1 MAX(col 2), col1 from Table1 group by col1"
cmdOLEDB.Connection = cnnOLEDB
txtbox.text = cmdOLEDB.ExecuteScalar().ToString()
ExecuteScalar Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored
ExecuteScalar 执行查询,返回查询返回的结果集中第一行的第一列。其他列或行被忽略
回答by Thanzeem
I change my code like this given below.. Its working coooool......
我像下面这样更改我的代码..它的工作coooool......
getConnect()
Conn.Open()
Dim str As String
Dim newNumber As Integer
str = "SELECT MAX(EMP_ID) AS MAXIMUM FROM EMPLOYEE"
Dim cmd As OleDbCommand = New OleDbCommand(str, Conn)
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read()
TXTEMPID.Text = dr("MAXIMUM").ToString
newNumber = CInt(Val(TXTEMPID.Text))
If newNumber = 0 Then
newNumber = 101
TXTEMPID.Text = CStr(newNumber)
Else
newNumber = newNumber + 1
TXTEMPID.Text = CStr(newNumber)
End If
End While
End If
Conn.Close()
Thank you all for replay and comment my question
感谢大家重播和评论我的问题

