VBACCESS数据库的修改、删除、添加VB  数据库的修改、删除、添加
Sub Command1_Click()
Dim s As
Dim Conn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Conn.Open "Provider=Microsoft.Jet..4.0;Data Source=D:\mydb.mdb"
'请将库名及路径换成你的实际数据库名及路径
Dim UserName As String
Dim Password As String
s = Text1.Text
If s = "1" Or s = "2" Then
UserName = InputBox("请输⼊⽤户名")
Password = InputBox("请输⼊⽤户密码")
sql = "Select * From [UserInfo] where User='" & UserName & "'"
'UserInfo请换成你的实际数据表名
Rs.Open sql, Conn, 1, 3
If Rs.EOF Then
MsgBox "没有到此⽤户"
Else
If Rs("Password") = Password Then
If s = "1" Then
MsgBox "123"
ElseIf s = "2" Then
MsgBox "456"
End If
Else
MsgBox "密码错误"
vb设计模拟密码输入器
End If
End If
End If
End Sub
上⾯的例⼦实现了查询
如果是要添加,删除,更新等操作,只要编写相应的SQL语句,再⽤
Conn.Execute SQL 就可以了,如:
添加的:
SQL="Insert Into [UserInfo](User,Password) Values('aaaa','1234')"
Conn.Execute SQL '执⾏后,就添加了⼀条记录
删除:
SQL="Delete From [UserInfo] Where User='aaaa'"
Conn.Execute SQL '删除⽤户名为aaaa的⽤户记录
更新:
SQL="Update [UserInfo] Set Password='abcdefg' Where User='aaaa'"
Conn.Execute SQL '执⾏后,修改⽤户aaaa的密码为abcdefg
以上只是⽰例,在实际使⽤过程中,可以将⼀些内容⽤控件输⼊等⽅式进⾏,这样就有很⼤的灵活性了
动态创建数据库
引⽤ microsoft DAP 3.6 Object Library
Dim myDB As DAO.Database
Set myDB = CreateDatabase(App.Path + "\111.mdb", dbLangGeneral) '如果不存在数据库
Set myDB = OpenDatabase(App.Path + "\111.mdb") '如果存在数据库
Dim str_SQL As String
str_SQL = "Create Table NewTable1(Field1 Text(10),Field2 Short)"
myDB.Execute str_SQL
str_SQL = "Create Table NewTable2(Field1 Text(10),Field2 Short)"
myDB.Execute str_SQL
myDB.Close
向已经建好的数据库中添加记录
建⽴数据库a,表名字b,字段c,⽂本格式,在窗体上画⼀command ,画⼀个⽂本框,这个代码就是将⽂本内容写⼊数据库
要先引⽤microsoft activeX data object 2.5 library
代码如下:
Private Sub Command1_Click()
Dim cnDk As New ADODB.Connection
Dim strDk As String
Dim rDk As New ADODB.Recordset
Dim sql As String
strDk = "DBQ=" & App.Path & "\a.mdb;Driver={Microsoft Access Driver (*.mdb)};"
cnDk.Open strDk
sql = "insert into b (c) values ('" & & "')"
cnDk.Execute sql
cnDk.Close
Set cnDk = Nothing
End Sub
在数据库中添加多条记录
只要可以进⾏输⼊⽂本的都可以
改⼀下这些吧,你单击⼀次就提⽰输⼊⼀次,输完了⾃动保存
改后
Data1.Recordset.AddNew
Data1.Recordset.Fields("username") = trim(inputbox("请输⼊名字"))
Data1.Recordset("password") =val(trim(inputbox("请输⼊电话")))
Data1.UpdateRecord
——————————————————————————————
Data1.Recordset.AddNew
Data1.Recordset.Fields("username") = "张三|"
Data1.Recordset("password") = "fsdfsd"
Data1.UpdateRecord
Data1.Recordset.AddNew
Data1.Recordset.Fields("username") = "李四|"
Data1.Recordset("password") = "fsdf432d"
Data1.UpdateRecord
Data1.Recordset.AddNew
Data1.Recordset.Fields("username") = "王五|"
Data1.Recordset("password") = "f42342d"
Data1.UpdateRecord
MsgBox "adad"
======================================================================================== vb+access 数据库的记录的添加与删除
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command2_Click()
Set rs = cn.Execute("select * from wenzhang where 内容='" & Text1.Text & "'")
If Text1.Text = "" Then
MsgBox ("⽂章不能为空!")
ElseIf rs.EOF = False Then
MsgBox ("⽂章不能重复!")
Else
cn.Execute ("insert into wenzhang values('" & Combo1.Text & "','" & Text3.Text & "','" & Text4.Text & "','" &
Text1.Text & "')")
MsgBox ("ok")
End If
End Sub
'添加按钮
Private Sub Command3_Click()
Dim myval As String
Set rs = cn.Execute("select * from wenzhang where 内容='" & Text1.Text & "'")
If rs.EOF Then
myval = MsgBox("是否保存⽂章?", vbInformation + vbYesNoCancel, "提⽰")
If myval = vbYes Then
cn.Execute ("insert into wenzhang values('" & Combo1.Text & "','" & Text3.Text & "','" & Text4.Text & "','" &
Text1.Text & "')")
MsgBox ("保存成功")
Else
Combo1.Text = ""
End If
Else
Text1.Text = ""
Combo1.Text = ""
End If
End Sub
'删除按钮
Private Sub Command4_Click()
If Text1.Text = "" Then
MsgBox ("不能删除空记录!")
Else
Dim myval As String
myval = MsgBox("是否删除⽂章?", vbInformation + vbYesNo, "提⽰")
If myval = vbYes Then
Set rs = cn.Execute("delete from wenzhang where 内容 ='" & Text1.Text & "'")
MsgBox ("删除成功!")
End If
End If
End Sub
'连接数据库
Private Sub Form_Load()
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data =\vb\temp.mdb;Persist Security Info=False"
cn.CursorLocation = adUseClient
cn.Open
End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。