Option Explicit
Private Conn As ADODB.Connection
Dim Rst As ADODB.Recordset
Private Sub cboStuName_Click()
On Error GoTo errhandle
Dim sql As String
sql = "SELECT 学号,姓名,语文,英文,数学 FROM score WHERE 姓名 ='" & cboStuName.Text & "'"
If Rst.State = adStateOpen Then Rst.Close
Rst.Open sql, Conn, adOpenKeyset, adLockPessimistic, adCmdText
Set dgrid.DataSource = Rst
Exit Sub
errhandle:
MsgBox Err.Description
Exit Sub
End Sub
Private Sub Form_Load()
On Err GoTo errhandle
Set Conn = CreateObject("ADODB.Connection")
Dim constring As String
constring = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & App.Path & "\Orders.mdb"
Conn.ConnectionString = constring
Conn.Open
Set Rst = CreateObject("ADODB.Recordset")
Rst.CursorLocation = adUseClient
Dim sql As String
sql = "SELECT 学号, 姓名,语文,英文,数学 FROM score"
Rst.Open sql, Conn, adOpenKeyset, adLockPessimistic, adCmdText
Dim i As Long
Dim lngRecordCount As Long
Rst.MoveLast
lngRecordCount = Rst.RecordCount
Rst.MoveFirst
For i = 0 To lngRecordCount - 1
cboStuName.AddItem Rst.Fields("姓名").Value
Rst.MoveNext
Next
Set dgrid.DataSource = Rst
Exit Sub
errhandle:
MsgBox Err.Description
Exit Sub
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set Rst = Nothing
Set Conn = Nothing
End Sub