vb 中如何往access数据库中添加1条新记录,又如何删除呢请写程序命令

2025-01-04 04:44:08
推荐回答(2个)
回答1:

Dim conn As ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
Set conn = CreateObject("adodb.connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path + "\dat.mdb" & ";Jet OLEDB:Database Password=123" '打开数据源
sql = "select * from 学生信息 "
rs.CursorLocation = adUseClient
rs.Open sql, conn, adOpenKeyset, adLockPessimistic
'rs.MoveLast
rs.AddNewrs.Fields(0).Value = "李"
rs.Fields(1).Value = 22
rs.Fields(2).Value = "男"
rs.Fields(3).Value = #6/8/1984#
rs.Update
rs.Close
conn.Close

Set conn = CreateObject("adodb.connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path + "\dat.mdb" & ";Jet OLEDB:Database Password=123" '打开数据源
sql = "select * from 学生信息 "
rs.CursorLocation = adUseClient
rs.Open sql, conn, adOpenKeyset, adLockPessimistic
rs.Move Label9
If MsgBox("你确定要删除姓名为:" & rs.Fields(0) & " 记录吗?", 1 + 32 + 0) = vbOK Then
rs.Delete
rs.Update
End If
rs.Close
conn.Close

回答2:

你用的是全角括号,要改过来
Reco.Open "insert into myuse values('0001','0004')"

插入
RunTrans "insert into myuse values('0001','0004')",Conn
删除
RunTrans "delete from myuse where id='0001'",Conn

Public Function RunTrans(ByVal tranSql As String, ByVal AdoConn As Object)
With AdoConn
.BeginTrans
.Execute tranSql
.CommitTrans
End With
End Function