VB ACCESS数据库的修改、删除、添加
Private Sub Command1_Click()
Dim s As String
Dim Conn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Conn.Open "Provider=Microsoft.Jet.OLEDB.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 "密码错误"
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 ('" & text1.text& "')"
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 Source=\vb.net\temp.mdb;Persist Security Info=False"
cn.CursorLocation = adUseClient
cn.Open
End Sub