这两天给一个朋友做VB作业,我对VB原来是不懂的,现学,终于完成任务,发现挺有意思的。
Dim n As Integer
Dim Answer(20) As Integer '计分数组
Dim total As Integer '总分
Private Sub Command3_Click()
response = MsgBox("你确定要交卷吗?", vbYesNo)
If response = vbYes Then
For n = 0 To 20
total = total + Answer(n)
Next n
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
'定义连接字符串
Dim connectionstring As String
connectionstring = "provider=Microsoft.Jet.oledb.4.0;" & "data source=db1.mdb"
'定义SQL语句,实现更新数据库功能
sql = "update class set 成绩 = " & total & " where 姓名 = '" & Frmtart.txtuser.Text & "'"
'连接
conn.Open connectionstring
rs.CursorLocation = adUseClient
'执行
rs.Open sql, conn, adOpenKeyset, adLockPessimistic
'用完之后关闭数据库
conn.Close
Set conn = Nothing
MsgBox ("你本次考试的成绩为" & Str(total) & ",考试成绩已经记录!")
Unload Me
End
End If
End Sub
相关连接 http://www.emugua.net/vbgl/1-20/pr2.htm VB问题全功略
利用ADO(ActiveX Data Objects)进行编程:
建立连接:
dim conn as new adodb.connection
dim rs as new adodb.recordset
dim str
str = App.Path
If Right(str, 1) <> "" Then
str = str + ""
End If
str = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & str & "tsl.mdb"
conn.open str
rs.cursorlocation=aduseclient
rs.open "数据表名",conn,adopenkeyset.adlockpessimistic
用完之后关闭数据库:
conn.close
set conn=nothing