VB+access帮忙弄段代码

发布网友 发布时间:2024-10-24 00:28

我来回答

2个回答

热心网友 时间:2024-10-26 13:32

改了下,你再试试吧
Form代码
——————————————————————————————
Private Sub Form_Load()
Dim conn As ADODB.Connection
Set conn = OpenConnForAccess(App.Path & "\Test.mdb")
Dim strSql As String
strSql = "select * from b where Id = """ & Text1.Text & """"
Dim rs As Recordset
Set rs = OpenRecordset(conn, strSql)
If rs.EOF Then
strSql = "insert into b(id,num) values(""" & Text1.Text & """," & Text2.Text & ")"
Else
strSql = "update b set num =" & Val(rs.Fields("num")) + Val(Text2.Text) & " where Id = """ & Text1.Text & """"
End If
RunTrans conn, strSql
strSql = "select * from b"
Set rs = OpenRecordset(conn, strSql)
End Sub

'公共函数(把下面的代码要放在公共模块里)
——————————————————————————————
Public Function OpenConnForAccess(ByVal FileName As String) As ADODB.Connection
Dim AdoConn As New ADODB.Connection
With AdoConn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileName & ";Persist Security Info=False"
.Open
End With
Set OpenConnForAccess = AdoConn
End Function

Public Function OpenRecordset(ByVal AdoConn As ADODB.Connection, ByVal strSql As String) As ADODB.Recordset
Dim rs As New ADODB.Recordset
With rs
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.Open strSql, AdoConn, , , adCmdText
End With
Set OpenRecordset = rs
End Function

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

热心网友 时间:2024-10-26 13:32

把表发给我,可以帮你做好发给你

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com