请教,datagrid不加载数据库记录问题
公用模块代码:Option Explicit
Public DB As ADODB.Connection
Public RS As ADODB.Recordset
Public RS1, RS2 As ADODB.Recordset
Public sqlStr As String
Public sqlStrX As String
Public strX(5) As String
Public Sub DataBase()
Set DB = New ADODB.Connection
DB.Open "provider=microsoft.jet.oledb.4.0;data source=" & App.Path & "\EMS.mdb;Persist Security Info=False"
Set RS = New ADODB.Recordset
RS.Open "select * from teachers", DB, adOpenKeyset, adLockPessimistic
Set RS1 = New ADODB.Recordset
Set RS2 = New ADODB.Recordset
RS1.Open "select distinct 姓名 from teachers", DB, adOpenKeyset, adLockPessimistic
RS2.Open "select distinct 编号 from teachers", DB, adOpenKeyset, adLockPessimistic
End Sub
Public Sub Rsize()
frmTeachers.DGteachers.RowHeight = 250
frmTeachers.DGteachers.Columns(0).Width = 1500
frmTeachers.DGteachers.Columns(1).Width = 800
frmTeachers.DGteachers.Columns(2).Width = 600
frmTeachers.DGteachers.Columns(3).Width = 800
frmTeachers.DGteachers.Columns(4).Width = 1200
frmTeachers.DGteachers.Columns(5).Width = 3000
frmTeachers.DGteachers.Columns(6).Width = 600
End Sub
frm窗体代码:
Private Sub Combo1_Change(Index As Integer)
strX(Index) = Combo1(Index).Text
If Index < 2 Then
Call RefreshX2
Else
Call RefreshX1
End If
End Sub
Private Sub Combo1_Click(Index As Integer)
strX(Index) = Combo1(Index).Text
If Index < 2 Then
Call RefreshX2
Else
Call RefreshX1
End If
End Sub
Private Sub Form_Load()
For i = 0 To 4
Combo1(i).AddItem "所有"
Next
Call Init
End Sub
Sub Init()
Call DataBase
Set DGteachers.DataSource = RS
Call Rsize
Do While Not RS1.EOF
Combo1(0).AddItem RS1("姓名")
RS1.MoveNext
Loop
Do While Not RS2.EOF
Combo1(1).AddItem RS2("编号")
RS2.MoveNext
Loop
With Combo1(2)
.AddItem "男"
.AddItem "女"
End With
With Combo1(3)
.AddItem "教授"
.AddItem "副教授"
.AddItem "讲师"
.AddItem "助教"
End With
With Combo1(4)
.AddItem "物理系"
.AddItem "化学系"
.AddItem "新闻系"
.AddItem "翻译系"
.AddItem "行管系"
End With
End Sub
Sub RefreshX()
RS.Close
RS.Open "select * from teachers", DB, adOpenKeyset, adLockPessimistic
Set DGteachers.DataSource = RS
Call Rsize
For i = 0 To 4
strX(i) = "所有"
Next
End Sub
Sub RefreshX1()
RS.Close
sqlStrX = "select * from teachers where 姓名 like '%'"
If strX(2) <> "所有" And strX(2) <> "" Then
sqlStrX = sqlStrX & "and 性别='" & strX(2) & "'"
End If
If strX(3) <> "所有" And strX(3) <> "" Then
sqlStrX = sqlStrX & "and 职称='" & strX(3) & "'"
End If
If strX(4) <> "所有" And strX(4) <> "" Then
sqlStrX = sqlStrX & "and 所在系='" & strX(4) & "'"
End If
RS.Open sqlStrX, DB, adOpenKeyset, adLockPessimistic
Set DGteachers.DataSource = RS
Call Rsize
End Sub
Sub RefreshX2()
RS.Close
If strX(0) <> "所有" And strX(1) <> "" Then
sqlStrX = "select * from teachers where 姓名 like '%" & strX(0) & "%'"
Else
sqlStrX = "select * from teachers where 姓名 like '%'"
End If
If strX(1) <> "所有" And strX(1) <> "" Then
sqlStrX = sqlStrX & "and 编号 like '%" & strX(1) & "%'"
End If
RS.Open sqlStrX, DB, adOpenKeyset, adLockPessimistic
Set DGteachers.DataSource = RS
Call Rsize
End Sub
问题是运行不报错,可DATAGrid控件不显示数据记录