我有个DB1.MDB数据表,想统计某字段的合计数.
Private Sub HEJI1_Click()
Dim i, j, b As Single
Dim a
If Not Rst.EOF() Then
For i = 1 To Rst.RecordCount
a = a + Val(Rst.Fields("j1").Value)
Next i
Text1.Text = Val(a)
End If
End Sub
执行后,结果不对啊!
哪地方错了?
我有个DB1.MDB数据表,想统计某字段的合计数.
Private Sub HEJI1_Click()
Dim i, j, b As Single
Dim a
If Not Rst.EOF() Then
For i = 1 To Rst.RecordCount
a = a + Val(Rst.Fields("j1").Value)
Next i
Text1.Text = Val(a)
End If
End Sub
执行后,结果不对啊!
哪地方错了?
我有个DB1.MDB数据表,想统计某字段的合计数.
Private Sub HEJI1_Click()
Dim i, j, b As Single
Dim a
If Not Rst.EOF() Then
For i = 1 To Rst.RecordCount
a = a + Val(Rst.Fields("j1").Value)
Next i
Text1.Text = Val(a)
End If
End Sub
执行后,结果不对啊!
哪地方错了?
你这段代码只是累加了RST返回的第一条记录的"J1"字段值Rst.RecordCount次,你那个For循环相当于a=Rst.Fields("j1").Value * Rst.RecordCount,你在For循环中加上一条语句就行了:
为什么不在SQL语句中直接SUM呢?
用SQL语句,这样写,行么?
Private Sub HEJI1_Click()
If Not Rst.EOF() Then
select SUM as total from FL1
Text1.Text = rst.field(sun).value
End If
End Sub
在我的DB1里面的表FL1里还有个字段:SUM
我在表FL1的设计视图添加了:“heji”的字段,
我改用SQL语句如下:
Private Sub HEJI1_Click()
If Not Rst.EOF() Then
select sum(heji) as total from FL1
Text1.Text = rst.field(heji).value
End If
End Sub
执行时出现:
缺少CASE???
到底sum是函数表达式,还是total函数表达式呢?
[此贴子已经被作者于2007-7-24 22:41:22编辑过]
“Dim oCnn As Object
Set oCnn = CreateObject("ADODB.connection")
oCnn.connectionstring = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;pwd=sa;Initial Catalog=zw" ';Data Source="这个字符串根据你自己的改
oCnn.ConnectionTimeout = 30
oCnn.Open
”
这个在FORM_Load()里已经不是有了么?
如下:
Private Sub Form_Load()
Dim ConString As String
ConString = "Provider=Microsoft.Jet.OleDb.4.0;Persist Security Info = False;" _
& "Data Source =" & App.Path & "\db1.mdb;Jet OleDb"
Set Conn = CreateObject("ADODB.Connection")
With Conn
.ConnectionString = ConString
.Open
End With
Rst.CursorLocation = adUseClient
Rst.Open "Select * From FL1", Conn, adOpenKeyset, adLockPessimistic, adCmdText
End Sub