VB如何向access数据库中添加一条记录

VB如何向access数据库中添加一条记录,第1张

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

一,准备工作:

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

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

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

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

程序代码如下:

'连接数据库的三步曲:

Adodc1.ConnectionString

=

"Provider=Microsoft.Jet.OLEDB.4.0Data

Source="

&

App.Path

&

"\数据库.mdbPersist

Security

Info=False"

Adodc1.RecordSource

=

"数据表名"

Adodc1.Refresh

‘添加记录

Adodc1.Recordset.AddNew

'添加新的空纪录

Adodc1.Recordset("姓名").Value

=

Text1.Text

Adodc1.Recordset.Update

’更新到数据表


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9771098.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存