
请把以下代码放到模块里面
Public Function ExeSql(ByVal sql As String) As ADODB.Recordset
sql = Trim$(sql)
Dim strpath As String
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset
'strpath = App.Path
'If Right(strpath, 1) <>"/" Then
'strpath = strpath &"/"
'End If
strpath = "\\192.168.0.131\本地磁盘 (F)\data.mdb"'这里放数据库文件路径
Set conn = New ADODB.Connection
Set rst = New ADODB.Recordset
conn.ConnectionString = "provider=microsoft.jet.oledb.4.0data source=" &strpath &"Mode=ReadWritePersist Security Info=False"
conn.ConnectionTimeout = 30
conn.Open
Set rst.ActiveConnection = conn
rst.LockType = adLockOptimistic
rst.CursorType = adOpenKeyset
rst.Open sql
Set ExeSql = rst
Set rst = Nothing
Set conn = Nothing
End Function
然后在需要使用的地方先声明dim rs as new adodb.recordset
使用set rs=exesql("select * from 数据表名")
这样就可以实现与数据库的交互了
with rs
.addnew
rs.fields("字段名")=text1.text
......
.update
end with
删除用户使用
with rs
.delete
.upate
end with
如果上面的是TXET,下面是LIST的话。添加是Private Sub Command1_Click()
List1.AddItem Text1.Text
End Sub
删除的是
Private Sub Command2_Click()
Dim I
GR:
For I = 0 To List1.ListCount - 1
If List1.Selected(I) = True Then
List1.RemoveItem I
GoTo GR
End If
Next I
End Sub
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)