| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1047 人关注过本帖
标题:DBGrid控件的设置和使用是怎样的?
只看楼主 加入收藏
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
结帖率:84.62%
收藏
 问题点数:0 回复次数:6 
DBGrid控件的设置和使用是怎样的?

DBGrid控件的设置和使用是怎样的?

我在窗体添加了DBGrid控件,并将DataSource属性设置为“Data”

代码如下:希望实现排列相关z(i)字段=“银行存款”信息所有记录到DBGrid控件里!

执行后却显示:变量未定义!

黄条指在:“Private Sub Command1_Click()”

蓝条指在:“Set DataGrid.DataSource = Rst”


代码如下:

Option Explicit
Private Conn As ADODB.Connection
Private Rst As New ADODB.Recordset
Private Sub Command1_Click()
Dim ii, ss, jj As Integer
Dim mysql As String
For jj = 1 To 9
mysql = "Select [j(" & jj & ")] from fl2 where [z(" & jj & ")]='银行存款' "
Set Rst = Conn.Execute(mysql)
ss = Rst.Fields.Count
If Rst.BOF = True Then
MsgBox "no!"
Else
Set DataGrid.DataSource = Rst
For ii = 0 To ss - 1
DataGrid.Columns(ii).Alignment = dbgCenter
DataGrid.Columns(ii).Width = 1000
Next ii
DataGrid.AllowUpdate = False


End Sub

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 fl2", Conn, adOpenKeyset, adLockPessimistic, adCmdText

End Sub

搜索更多相关主题的帖子: DBGrid 控件 
2007-08-21 22:16
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

DataGrid.改为DataGrid1.

少了“1”怪不得,

但是执行后,执行结果为“NO!”

没有实现预计的结果!


“ MsgBox "no!"
Else”

删除掉后,再执行,出现:

行集合不能作为标签!

黄条指在:
“ Set DataGrid1.DataSource = Rst ”

2007-08-21 22:27
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

又改为:

Private Sub Command1_Click()
Dim ii, jj As Integer
Dim ss, kk As Long
Dim aa, bb, cc, dd

For ii = 1 To Rst.RecordCount
For jj = 0 To 50
If Rst.Fields(jj).Value = "银行存款" Then
ss = Val(Rst.Fields(jj + 2).Value) - Val(Rst.Fields(jj + 3).Value)
aa = Rst.Fields(jj - 1).Value
Print ss
Print aa

End If
Next jj

Next ii

End Sub

执行后,只出现第一个记录中含有“银行存款”的所对应的字段的值

并出现:

在。。。集合中,未找到项目!

2007-08-21 22:58
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

我又改为:

Private Sub Command1_Click()
Dim ii, jj As Integer
Dim ss, kk As Long
For jj = 1 To 9
If Rst.Fields(z(" & jj & ")).Value = "银行存款" Then
ss = Val(Rst.Fields(j(" & jj & ")).Value) - Val(Rst.Fields(d(" & jj & ")).Value)

Print ss
End If
Next
End Sub

执行后出现:

编译错误!
子程序或函数未定义

??

2007-08-21 22:59
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

我又添加了ADOC控件与DBGrid结合使用,ADOC控件且测试连接成功!

DBGrid的DataSource属性为:ADOC1
执行后还是不行!出现了:

没有指定记录源,没有为命令对象设置命令!

但DBGrid表格还是有些动静!

代码修改如下:

Option Explicit
Private Conn As ADODB.Connection
Private Rst As New ADODB.Recordset
Private Sub Command1_Click()
Dim ii, ss, jj As Integer
Dim mysql As String
For jj = 1 To 9
mysql = "Select [j(" & jj & ")] from fl2 where [z(" & jj & ")]='银行存款' "
Set Rst = Conn.Execute(mysql)
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 jj
End Sub

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
Conn.CursorLocation = adUseClient
Rst.CursorLocation = adUseClient
Rst.Open "Select * From fl2", Conn, adOpenKeyset, adLockPessimistic, adCmdText

End Sub

2007-08-22 00:24
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

只显示J(1)字段下所有的含“银行存款”数额的记录!


代码如下:


Option Explicit
Private Conn As ADODB.Connection
Private Rst As New ADODB.Recordset
Private Sub Command1_Click()
Dim ii, ss, jj As Integer
Dim mysql As String
For jj = 1 To 9
mysql = "Select [j(" & jj & ")] 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
Next jj
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

End Sub

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
Conn.CursorLocation = adUseClient
Rst.CursorLocation = adUseClient
Rst.Open "Select * From fl2", Conn, adOpenKeyset, adLockPessimistic, adCmdText

End Sub

2007-08-22 17:19
lijun337
Rank: 1
等 级:新手上路
帖 子:379
专家分:5
注 册:2006-4-12
收藏
得分:0 

已经实现绑定了!ADOC1的RecordSource属性上写上“select * from fl2”

就不再出现“没有指定源。。。”

但是,这程序却没有能够实现循环!!??

2007-08-22 21:27
快速回复:DBGrid控件的设置和使用是怎样的?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017503 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved