VB对ACCESS的数据库操作问题,
我想把每个记录中Z(I)字段值=“银行存款”时对应的J(I)或D(I)的值排列输出来!
以前是总计数,代码如下:
For i2 = 1 To 9
s2 = "Select Sum([j(" & i2 & ")]) from fl2 where [z(" & i2 & ")]='银行存款' GROUP BY [Z(" & i2 & ")]"
If conn.Execute(s2).EOF = False And IsNull(conn.Execute(s2)(0)) = False Then
a2 = a2 + conn.Execute(s2)(0)
End If
Next
现在为实现全部一一输出排列显示出来,而不是统计,改为:
Private Sub Command1_Click()
Dim jj, ss, ii As Integer
Dim mysql As String
For jj = 1 To 9
mysql = "select * from fl2 where [z(" & jj & ")]='银行存款' "
If Conn.Execute(mysql).EOF = False And IsNull(Conn.Execute(mysql)(0)) = False Then
Set Rst = Conn.Execute(mysql)
End If
ss = Rst.Fields.Count
If Rst.BOF = True Then
MsgBox "no!"
Else
Set DataGrid1.DataSource = Rst
For ii = 0 To ss - 1
DataGrid1.Columns(ii).Alignment = dbgCenter
DataGrid1.Columns(ii).Width = 1000
Next ii
DataGrid1.AllowUpdate = False
End If
Next
End Sub
怎么就显示一个记录呢?
没有实现循环???!!!
改:
Private Sub Command1_Click()
Dim jj, ss, ii As Integer
Dim d() As Integer
Dim f() As Integer
Dim h() As Integer
For ii = 0 To Rst.RecordCount
For jj = 0 To Rst.Fields.Count
If Rst.RecordCount > 0 Then
If Rst.Fields(jj).Value = "银行存款" Then
d(ii) = Rst.Fields(1).Value
h(ii) = Rst.Fields(2).Value
f(ii) = Val(Rst.Fields(jj + 2).Value) + Val(Rst.Fields(jj + 3).Value)
Else
Rst.MoveNext
Print d(ii)
Print f(ii)
Print h(ii)
End If
End If
Next jj
Next ii
End Sub
执行后,出现:
实时错误“3021”
BOF或EOF中有一个是“真”,或者当前的记录已被删除,所需的操作要求一个当前的记录。
再改:
Private Sub Command1_Click()
Dim jj, ss, ii As Integer
Dim d() As Integer
Dim f() As Integer
Dim h() As Integer
For ii = 0 To Rst.RecordCount
For jj = 0 To Rst.Fields.Count
If Rst.RecordCount > 0 Then
If Rst.Fields(jj).Value = "银行存款" Then
d(ii) = Rst.Fields(1).Value
h(ii) = Rst.Fields(2).Value
f(ii) = Val(Rst.Fields(jj + 2).Value) + Val(Rst.Fields(jj + 3).Value)
Else
Rst.MoveNext
Print d(ii)
Print f(ii)
Print h(ii)
End If
End If
Next jj
Next ii
End Sub
运行后,出现:
下标越界!
再改为:
For ii = 0 To Rst.RecordCount
If Rst.RecordCount > 0 Then
For jj = 0 To Rst.Fields.Count
If Rst.Fields(jj).Value = "银行存款" Then
d(ii) = Rst.Fields(1).Value
h(ii) = Rst.Fields(2).Value
f(ii) = Val(Rst.Fields(jj + 2).Value) + Val(Rst.Fields(jj + 3).Value)
Print d(ii)
Print f(ii)
Print h(ii)
Else
Rst.MoveNext(位置调整)
End If
Next jj
Else
MsgBox "没有记录!"
End If
Next ii
执行时出现:
实时错误“3021”
BOF或EOF中有一个是“真”,或者当前的记录已被删除,所需的操作要求一个当前的记录。
再改为(添加):。。。
。。。
ReDim d(Rst.RecordCount)
ReDim f(Rst.RecordCount)
ReDim h(Rst.RecordCount)
。。。
。。。
插入了RDEM,结果:
执行时仍然出现了与上面相同的结果:
实时错误“3021”
BOF或EOF中有一个是“真”,或者当前的记录已被删除,所需的操作要求一个当前的记录。