vb 连接sql数据库中recordset问题
大虾帮看看这段程序。怎么老提示有错误。PublicconnAsNewADODB.ConnectionPublicrsAsNewADODB.RecordsetPublicF...
大虾帮看看这段程序。怎么老提示有错误。
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public Function dbrefresh()
Dim connstr As String
Dim str As String
On Error GoTo errcontrl
Set conn = New ADODB.Connection
conn.ConnectionTimeout = 20
conn.Open ("provider=sqloledb.1;data source=localhost;UID=sa;PWD=;initial catalog=jxkh;")
Exit Function
errcontrl:
Call MsgBox("Error:Data connect", vbOKCancel)
End Function
Public Function dbrs()
Dim rownum As Integer
Dim MSFlexGrid1 As MSFlexGrid
On Error GoTo errcontrl
rs.Open "select * from khzb", conn
rownum = 1
MSFlexGrid1.Rows = rownum
MSFlexGrid1.Cols = rs.Fields.Count
For i = 0 To rs.Fields.Count - 1
MSFlexGrid1.TextMatrix(0, j) = rs.Fields(j).Name
Next i
Do While Not rs.EOF
rownum = rownum + 1
MSFlexGrid1.Rows = rownum
For j = o To rs.Fields.Count - 1
If Not IsNull(rs.Fields(j).Value) Then
MSFlexGrid1.TextMatrix(rownum - 1, j) = rs.Fields(j).Value
End If
Next j
rs.MoveNext
Loop
errcontrl:
Call MsgBox("error:data refresh!", vbOKCancel)
End Function
尤其是:rs.Open "select * from khzb", conn这一部分,是不是这里有错误?MSFlexGrid1总是显示不了!谢了先。 展开
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public Function dbrefresh()
Dim connstr As String
Dim str As String
On Error GoTo errcontrl
Set conn = New ADODB.Connection
conn.ConnectionTimeout = 20
conn.Open ("provider=sqloledb.1;data source=localhost;UID=sa;PWD=;initial catalog=jxkh;")
Exit Function
errcontrl:
Call MsgBox("Error:Data connect", vbOKCancel)
End Function
Public Function dbrs()
Dim rownum As Integer
Dim MSFlexGrid1 As MSFlexGrid
On Error GoTo errcontrl
rs.Open "select * from khzb", conn
rownum = 1
MSFlexGrid1.Rows = rownum
MSFlexGrid1.Cols = rs.Fields.Count
For i = 0 To rs.Fields.Count - 1
MSFlexGrid1.TextMatrix(0, j) = rs.Fields(j).Name
Next i
Do While Not rs.EOF
rownum = rownum + 1
MSFlexGrid1.Rows = rownum
For j = o To rs.Fields.Count - 1
If Not IsNull(rs.Fields(j).Value) Then
MSFlexGrid1.TextMatrix(rownum - 1, j) = rs.Fields(j).Value
End If
Next j
rs.MoveNext
Loop
errcontrl:
Call MsgBox("error:data refresh!", vbOKCancel)
End Function
尤其是:rs.Open "select * from khzb", conn这一部分,是不是这里有错误?MSFlexGrid1总是显示不了!谢了先。 展开
展开全部
rs.Fields(j).Name有问题
rs.Fields(j)好像也有问题
举例说明
如果sql=select name,pwd,userid,decode,address from 表
上面只有5个字段
字段的值应该分别为
rs.Fields("name") 也可为 rs.Fields(0)
rs.Fields("pwd") 或者 rs.Fields(1)
rs.Fields("userid") 或者 rs.Fields(2)
rs.Fields("decode") 或者 rs.Fields(3)
rs.Fields("address") 或者 rs.Fields(4)
如果只有5个字段,却出现rs.Fields(5),是会出问题的
rs.Fields(j)好像也有问题
举例说明
如果sql=select name,pwd,userid,decode,address from 表
上面只有5个字段
字段的值应该分别为
rs.Fields("name") 也可为 rs.Fields(0)
rs.Fields("pwd") 或者 rs.Fields(1)
rs.Fields("userid") 或者 rs.Fields(2)
rs.Fields("decode") 或者 rs.Fields(3)
rs.Fields("address") 或者 rs.Fields(4)
如果只有5个字段,却出现rs.Fields(5),是会出问题的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询