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"
'請將數(shù)據(jù)庫名及路徑換成你的實際數(shù)據(jù)庫名及路徑
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請換成你的實際數(shù)據(jù)表名
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
上面的例子實現(xiàn)了查詢
如果是要添加,刪除,更新等操作,只要編寫相應(yīng)的SQL語句,再用
Conn.Execute SQL 就可以了,如:
添加的:
SQL="Insert Into [UserInfo](User,Password) Values('aaaa','1234')"
Conn.Execute SQL '執(zhí)行后,就添加了一條記錄
刪除:
SQL="Delete From [UserInfo] Where User='aaaa'"
Conn.Execute SQL '刪除用戶名為aaaa的用戶記錄
更新:
SQL="Update [UserInfo] Set Password='abcdefg' Where User='aaaa'"
Conn.Execute SQL '執(zhí)行后,修改用戶aaaa的密碼為abcdefg
以上只是示例,在實際使用過程中,可以將一些內(nèi)容用控件輸入等方式進(jìn)行,這樣就有很大的靈活性了
動態(tài)創(chuàng)建數(shù)據(jù)庫
引用 microsoft DAP 3.6 Object Library
Dim myDB As DAO.Database
Set myDB = CreateDatabase(App.Path + "\111.mdb", dbLangGeneral) '如果不存在數(shù)據(jù)庫
Set myDB = OpenDatabase(App.Path + "\111.mdb") '如果存在數(shù)據(jù)庫
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
向已經(jīng)建好的數(shù)據(jù)庫中添加記錄
建立數(shù)據(jù)庫a,表名字b,字段c,文本格式,在窗體上畫一command ,畫一個文本框,這個代碼就是將文本內(nèi)容寫入數(shù)據(jù)庫
要先引用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
在數(shù)據(jù)庫中添加多條記錄
只要可以進(jìn)行輸入文本的都可以
改一下這些吧,你單擊一次就提示輸入一次,輸完了自動保存
改后
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 數(shù)據(jù)庫的記錄的添加與刪除
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub Command2_Click()
Set rs = cn.Execute("select * from wenzhang where 內(nèi)容='" & Text1.Text & "'")
If Text1.Text = "" Then
MsgBox ("文章不能為空!")
ElseIf rs.EOF = False Then
MsgBox ("文章不能重復(fù)!")
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 內(nèi)容='" & 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 內(nèi)容 ='" & Text1.Text & "'")
MsgBox ("刪除成功!")
End If
End If
End Sub
'連接數(shù)據(jù)庫
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