1、用SQL添加数据:
insert into 表名(字段1,字段2,字段3)
values(值01,值02,值03)
例如:
docmd.runsql"insert into EmpTable(Name,age,Duty,Salary) values('王明',35,'外贸总监',12500)"
2、用ADO的办法:
ADO比较复者做碧杂,给你一个完整的例子吧:
Dim strSqlfitjc As String
strSqlfitjc = "select * from *表"
Set RSfitjc = New ADODB.Recordset ‘定义一个ADO连接,连进数据库
RSfitjc.Open strSqlfitjc, cn, adOpenKeyset, adLockOptimistic ’打开首举连接
RSfitjc.addnew'添加一个纪录
RSfitjc.Fields("配件编号") = “style” ‘给每个字段赋值
.Update’储存赋值的新纪录
.Close 关闭连接
End With
vb向access插入数据:Public Conn As New ADODB.Connection
Public HidErr As Boolean
Public RS As New ADODB.Recordset
Public comm As New ADODB.Command
'打开ACCESS数据库,可设定登陆的密码
Function OpenAccess(Mdbfilepath As String, Optional Mdbpassword As String, Optional usemdw As Boolean) As Boolean
'帮你森胡写函数,帮你写代码,帮你写模块,帮你设计软件
'联系QQ:1085992075--需要什么函数或功能,可以联系我。
On Error GoTo err
Dim ConnStr As String
Conn.ConnectionTimeout = 999999999
ConnStr = "Provider=MSDASQL.1Persist Security Info=falseUser ID=saExtended Properties=DRIVER=Microsoft Access Driver (*.mdb)"
If usemdw Then ConnStr = ConnStr &"DBPROP_JETOLEDB_SYSDBPATH=" &Chr(34) &"F:\PROGRA~1\MICROS~2\OFFICE\SYSTEM.MDW" &Chr(34) &"user=adminpass=abc"
ConnStr = ConnStr &"DBQ=" &Mdbfilepath &"绝携Password=" &Mdbpassword
Conn.open ConnStr
OpenAccess = True
Exit Function
err:
If Not HidErr Then
MsgBox err.Number &vbCrLf &err.Description
End If
End Function
'┗〓〓〓〓〓〓〓〓〓 OpenAccess,end 〓〓〓〓〓〓〓〓〓┛
Sub Test()
Dim RS As New ADODB.Recordset
'Set Rs = CreateObject("adodb.recordset")
Dim Sql As String
Sql = "select * from 表1 WHERE USER='" &"李三" &"'"
'OpenAccess函数相关定义声明等 Start
Public Conn As New ADODB.Connection
Public HidErr As Boolean
Public RS As New ADODB.Recordset
Public comm As New ADODB.Command
'OpenAccess函数相关定义声明等 End
'OpenAccess,start
'[详介]:
'函数注释:
'[简介]:
'打开ACCESS数据库,可设定登陆的密码
Function OpenAccess(Mdbfilepath As String, Optional Mdbpassword As String, Optional usemdw As Boolean) As Boolean
'帮你写函数,帮你写代码,帮你并春伏写模块,帮你设计软件
'联系QQ:1085992075--需要什么函数或功能,可以联系我。
On Error GoTo err
Dim ConnStr As String
Conn.ConnectionTimeout = 999999999
ConnStr = "Provider=MSDASQL.1Persist Security Info=falseUser ID=saExtended Properties=DRIVER=Microsoft Access Driver (*.mdb)"
If usemdw Then ConnStr = ConnStr &"DBPROP_JETOLEDB_SYSDBPATH=" &Chr(34) &"F:\PROGRA~1\MICROS~2\OFFICE\SYSTEM.MDW" &Chr(34) &"user=adminpass=abc"
ConnStr = ConnStr &"DBQ=" &Mdbfilepath &"Password=" &Mdbpassword
Conn.open ConnStr
OpenAccess = True
Exit Function
err:
If Not HidErr Then
MsgBox err.Number &vbCrLf &err.Description
End If
End Function
'OpenAccess,end
Sub Test()
Dim RS As New ADODB.Recordset
'Set Rs = CreateObject("adodb.recordset")
Dim Sql As String
Sql = "select * from 表1 WHERE USER='" &"李三" &"'"view plaincopy to clipboardprint?
'以上用来查李三这条数据
RS.open Sql, Conn, 1, 3
MsgBox RS(0)
rs(1)="aaa"
'rs.update'保存
End Sub
Private Sub Command1_Click()
OpenAccess APP.PATH &"\1.mdb"
Test
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)