vb中DataGrid绑定Recordset记录集的问题
DimconnAsConnectionDimrsAsRecordsetSetconn=CreateObject("adodb.connection")Setrs=Crea...
Dim conn As Connection
Dim rs As Recordset
Set conn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
Dim SQL As String
SQL = "select tName,tPhone,tSum,tDate from t_basic where tInvoice = '" & sInvoice.Text & "'"
conn.Open "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath & ";Persist Security Info=False;Jet OLEDB:Database Password=abc123"
rs.Open SQL, conn, 1, 1, 1
Set DataGrid1.DataSource = rs
(rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing)
有段DataGrid绑定Recordset记录集的代码如上
但是运行下来什么反应都没有,也没有报错
谁能帮我看看这应该怎么做的?
还有括号内的代码如果执行了会使DataGrid内数据失效吗?如果会的话最后应该怎么关闭数据集和连接?
(括号内的代码我加上去和去掉结果都没反应的)
DataGrid1.ClearFields
DataGrid1.ReBind
我发现可能是漏了这两条
但我加上去之后却出现"不能初始化数据绑定"
这是怎么回事?网上查了下大多是说几个组件注册一下,我全部注册过了还是没用 展开
Dim rs As Recordset
Set conn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
Dim SQL As String
SQL = "select tName,tPhone,tSum,tDate from t_basic where tInvoice = '" & sInvoice.Text & "'"
conn.Open "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath & ";Persist Security Info=False;Jet OLEDB:Database Password=abc123"
rs.Open SQL, conn, 1, 1, 1
Set DataGrid1.DataSource = rs
(rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing)
有段DataGrid绑定Recordset记录集的代码如上
但是运行下来什么反应都没有,也没有报错
谁能帮我看看这应该怎么做的?
还有括号内的代码如果执行了会使DataGrid内数据失效吗?如果会的话最后应该怎么关闭数据集和连接?
(括号内的代码我加上去和去掉结果都没反应的)
DataGrid1.ClearFields
DataGrid1.ReBind
我发现可能是漏了这两条
但我加上去之后却出现"不能初始化数据绑定"
这是怎么回事?网上查了下大多是说几个组件注册一下,我全部注册过了还是没用 展开
2个回答
推荐于2016-04-27 · 知道合伙人软件行家
关注
展开全部
Private Sub Command1_Click()
Dim cn As New ADODB.Connection '定义数据库的连接
Dim rs As New ADODB.Recordset
Dim sql As String
Dim l As Integer
Dim i As Integer
Dim fieldName() As String
sql = "select tName,tPhone,tSum,tDate from t_basic where tInvoice = '" & sInvoice.Text & "'"
cn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath & ";Persist Security Info=False;Jet OLEDB:Database Password=abc123"
cn.Open
rs.CursorLocation = adUseClient
rs.Open sql, cn, adOpenDynamic, adLockOptimistic
rs.MoveFirst
rs.MoveLast
l = rs.Fields.Count
Debug.Print l
Set DataGrid1.DataSource = rs
End Sub
Dim cn As New ADODB.Connection '定义数据库的连接
Dim rs As New ADODB.Recordset
Dim sql As String
Dim l As Integer
Dim i As Integer
Dim fieldName() As String
sql = "select tName,tPhone,tSum,tDate from t_basic where tInvoice = '" & sInvoice.Text & "'"
cn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath & ";Persist Security Info=False;Jet OLEDB:Database Password=abc123"
cn.Open
rs.CursorLocation = adUseClient
rs.Open sql, cn, adOpenDynamic, adLockOptimistic
rs.MoveFirst
rs.MoveLast
l = rs.Fields.Count
Debug.Print l
Set DataGrid1.DataSource = rs
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询