VB Access 查询出最大值如何显示在Text控件上?

liuyi0524 2015-12-24 06:28:28
Private Sub Command3_Click()

Dim MaxVal As Single
Dim sql As String
sql = "Select Max(Val) As MaxVal From FloatTable where DateAndTime between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"

Text1.Text = MaxVal

End Sub

已经可以查询出最大值,但是为什么在Text1.Text显示不出来? 新手,见笑
...全文
482 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyi0524 2015-12-28
  • 打赏
  • 举报
回复
感谢大家,代码如下,供大家参考 Private Sub Command1_Click() Adodc1.CommandType = adCmdText Adodc1.RecordSource = " select * from FloatTable where DateAndTime between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "# " Adodc1.Refresh Set Cnn = New ADODB.Connection Cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Common Files\ODBC\Data Sources\VBA.mdb;Persist Security Info=False" Set rec = New ADODB.Recordset Set rec.ActiveConnection = Cnn rec.Open "Select Max(Val) From FloatTable where DateAndTime between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" Text1.Text = (rec.Fields(0) & " N") rec.Close rec.Open "Select Min(Val) From FloatTable where DateAndTime between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" Text2.Text = (rec.Fields(0) & " N") rec.Close rec.Open "Select Avg(Val) From FloatTable where DateAndTime between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" Text3.Text = rec.Fields(0) Text3.Text = (Format(Text3.Text, "0.00") & " N") rec.Close End Sub
熊孩子开学喽 2015-12-25
  • 打赏
  • 举报
回复
因为你只是定义了一句SQL语句, 却根本没有执行它.
gtclulong 2015-12-24
  • 打赏
  • 举报
回复
这样子是没法查出最大值吧 查询返回结果集,遍历结果集找到自己需要的数据


Private Sub Command3_Click()
Dim cnn As Object, Rs as object, CnnStr as string,SqlStr as string
Set cnn = New ADODB.Connection
set Rs= New ADODB.Recordset
CnnStr ="PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data.mdb"  '////注意要把Data.mdb的路径写全,C:\Data.mdb只是我随便写的
cnn.Open CnnStr
if cnn.state=1 then
    sqlstr="Select  Max(Val) As MaxVal  From  FloatTable where DateAndTime between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
    set rs=cnn.execute(sqlstr)
    if not rs.eof then
        Text1.Text = rs(0)
    end if
end if
End Sub

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧