vb向access数据库添加记录,vb读取access数据库

http://www.itjxue.com  2023-01-24 10:12  来源:未知  点击次数: 

VB 如何往 ACCESS数据库理添加数据

一,准备工作:

1,建立新工程,保存到文件夹。

2,把ACCESS数据库文件复制到上述文件夹。

3,在窗体上,添加一个ADODC控件和一个按钮。

4,在按钮的单击事件里编写程序。

程序代码如下:

'连接数据库的三步曲:

Adodc1.ConnectionString

=

"Provider=Microsoft.Jet.OLEDB.4.0;Data

Source="

App.Path

"\数据库.mdb;Persist

Security

Info=False"

Adodc1.RecordSource

=

"数据表名"

Adodc1.Refresh

‘添加记录

Adodc1.Recordset.AddNew

'添加新的空纪录

Adodc1.Recordset("姓名").Value

=

Text1.Text

Adodc1.Recordset.Update

’更新到数据表

如何在VB中实现向ACCESS数据库添加数据

以下代码能使用的前提是库引用、和数据库连接代码正确:

两种办法,一种是 用SQL,另一种是ADO

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数据库中添加一条记录

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.1;Persist Security Info=false;User ID=sa;Extended 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=admin;pass=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.1;Persist Security Info=false;User ID=sa;Extended 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=admin;pass=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

VB access添加记录

1,点击菜单栏的创建,点击查询设计,点击后会跳出显示表,把它关了。

2,此时表名自定义为查询1,右击左上角的表名,选择SQL。

3,输入SQL语言:ALTER TABLE 表名 ADD 要添加的记录字段名;

4,点击保存,在保存为里更改表名,也可以不更改,点击运行,再打开要添加记录的表,就可以看到添加了记录了。

(责任编辑:IT教学网)

更多

推荐mac苹果系统文章