急求,有关数据库的
现在这两段代码有点问题,但不知道怎么改正,初学VB不久望大家知道
1、Dim xbc As String
Dim rs As New ADODB.Recordset
Dim num As Integer
Dim cn2 As New ADODB.Connection
Dim ls As String
ls = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & App.Path & "\diren.mdb"
cn2.Open ls
xbc = "select * from 敌人"
rs.Open xbc, cn2, 3, 1
MSFlexGrid1.Cols = rs.Fields.Count
num = rs.RecordCount
MSFlexGrid1.Row = num + 1
MSFlexGrid1.FixedRows = 1
MSFlexGrid1.FixedCols = 1
rs.MoveFirst
For i = 1 To num - 1
For j = 0 To rs.Fields.Count - 1
MSFlexGrid1.TextMatrix(i, j) = rs(j)
Next j
rs.MoveNext
Next i
rs.Close
End Sub
这个错误时实时错误30009 row值无效什么的
2、Private Sub Command1_Click()
Dim rs As New ADODB.Recordset
Dim sql As String
Dim jr As Integer
sql = "select*from 组织编号"
rs.Open sql, cn, 3, 1
For jr = 1 To sr.RecordCount
If rs("编号") = Text1.Text Then
MsgBox "已成功登陆"
Command2.Enabled = True
Exit For
End If
rs.MoveNext
Next jr
End Sub
其中cn在前一个form里是 Public cn As New ADODB.Connection
这个错误是实时错误3001 数据库 SQL
[解决办法]
Dim xbc As String
Dim rs As New ADODB.Recordset
Dim num As Integer
Dim cn2 As New ADODB.Connection
Dim ls As String
ls = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & App.Path & "\diren.mdb"
cn2.Open ls
xbc = "select * from 敌人"
rs.Open xbc, cn2, 3, 1
'这段代码(称A代码块)可以用一个简单语句替换:set MSFlexGrid1.datasource=rs
MSFlexGrid1.cols = rs.fields.Count
num = rs.RecordCount
MSFlexGrid1.Rows = num + 1 '是rows 不是row
MSFlexGrid1.cols = rs.fields.Count + 1
MSFlexGrid1.FixedRows = 1
MSFlexGrid1.FixedCols = 1
rs.MoveFirst
i = 1
While Not rs.EOF
For j = 0 To rs.fields.Count - 1
MSFlexGrid1.TextMatrix(i, j) = rs(j)
Next j
rs.movenext
Wend
rs.Close
'称A代码块到此结束
End Sub
Private Sub Command1_Click()
Dim rs As New ADODB.Recordset
Dim sql As String
Dim jr As Integer
sql = "select*from 组织编号"
rs.Open sql, cn, 3, 1 'cn不在同个窗体,应该使用Form1.cn ,或者将cn定义到bas模块
For jr = 1 To sr.RecordCount
If rs("编号") = Text1.Text Then
MsgBox "已成功登陆"
Command2.Enabled = True
Exit For
End If
rs.movenext
Next jr
End Sub