这问题我好像回答过,典型的作业。你要是完全靠别人帮你做,那还学啥ACCESS?还是自己做吧,方法上次已经说过了,其实非常简单。
善意提醒:知道的目的是帮忙解决问题,而不是代替大家学习。
方法/步骤
1
首先我们打开电脑,找到access,鼠标双击打开。
2
进入access软件后,我们点击新建,选择空数据库。
3
紧接着,我们就要为这个数据库命名了,在左下方输入名称,并选择路径。
4
再然后,我们就可以点击创建按钮了,一个空的数据库就建好了。
5
下面我们就要为数据库添加点东西了,那就添加个表吧,点击创建,选择表。
6
接下来,我们鼠标点击(单击以添加),选择要添加的数据类型。
7
填写好数据类型,并把要记录的数据填入表格中即可,保存退出,这样一个简单的数据库就建好了。
VB access,添加/删除/修改记录代码 给你一个示例,这个是用户登录系统
'准备工作: 数据库 D:\Mydbmdb 数据库中,建数据表:UserInfo,数据表中,建字段:User,Password,并添加一条记录
'点工程->引用 选中 Microsoft Activex Data Objects 21 Library
'控件:添加一个文本框,一个命令按钮
Private Sub Command1_Click()
Dim s As String
Dim Conn As New ADODBConnection
Dim Rs As New ADODBRecordset
ConnOpen "Provider=MicrosoftJetOLEDB40;Data Source=D:\mydbmdb"
'请将数据库名及路径换成你的实际数据库名及路径
Dim UserName As String
Dim Password As String
s = Text1Text
If s = "1" Or s = "2" Then
UserName = InputBox("请输入用户名")
Password = InputBox("请输入用户密码")
sql = "Select From [UserInfo] where User='" & UserName & "'"
'UserInfo请换成你的实际数据表名
RsOpen sql, Conn, 1, 3
If RsEOF 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语句,再用
ConnExecute SQL 就可以了,如:
添加的:
SQL="Insert Into [UserInfo](User,Password) Values('aaaa','1234')"
ConnExecute SQL '执行后,就添加了一条记录
删除:
SQL="Delete From [UserInfo] Where User='aaaa'"
ConnExecute SQL '删除用户名为aaaa的用户记录
更新:
SQL="Update [UserInfo] Set Password='abcdefg' Where User='aaaa'"
ConnExecute SQL '执行后,修改用户aaaa的密码为abcdefg
以上只是示例,在实际使用过程中,可以将一些内容用控件输入等方式进行,这样就有很大的灵活性了
动态创建数据库
引用 microsoft DAP 36 Object Library
Dim myDB As DAODatabase
Set myDB = CreateDatabase(AppPath + "\111mdb", dbLangGeneral) '如果不存在数据库
Set myDB = OpenDatabase(AppPath + "\111mdb") '如果存在数据库
Dim str_SQL As String
str_SQL = "Create Table NewTable1(Field1 Text(10),Field2 Short)"
myDBExecute str_SQL
str_SQL = "Create Table NewTable2(Field1 Text(10),Field2 Short)"
myDBExecute str_SQL
myDBClose
向已经建好的数据库中添加记录
建立数据库a,表名字b,字段c,文本格式,在窗体上画一command ,画一个文本框,这个代码就是将文本内容写入数据库
要先引用microsoft activeX data object 25 library
代码如下:
Private Sub Command1_Click()
Dim cnDk As New ADODBConnection
Dim strDk As String
Dim rDk As New ADODBRecordset
Dim sql As String
strDk = "DBQ=" & AppPath & "\amdb;Driver={Microsoft Access Driver (mdb)};"
cnDkOpen strDk
sql = "insert into b (c) values ('" & text1text& "')"
cnDkExecute sql
cnDkClose
Set cnDk = Nothing
End Sub
在数据库中添加多条记录
只要可以进行输入文本的都可以
改一下这些吧,你单击一次就提示输入一次,输完了自动保存
改后
Data1RecordsetAddNew
Data1RecordsetFields("username") = trim(inputbox("请输入名字"))
Data1Recordset("password") =val(trim(inputbox("请输入电话")))
Data1UpdateRecord
——————————————————————————————
Data1RecordsetAddNew
Data1RecordsetFields("username") = "张三|"
Data1Recordset("password") = "fsdfsd"
Data1UpdateRecord
Data1RecordsetAddNew
Data1RecordsetFields("username") = "李四|"
Data1Recordset("password") = "fsdf432d"
Data1UpdateRecord
Data1RecordsetAddNew
Data1RecordsetFields("username") = "王五|"
Data1Recordset("password") = "f42342d"
Data1UpdateRecord
MsgBox "adad"
========================================================================================
vb+access 数据库的记录的添加与删除
Dim cn As New ADODBConnection
Dim rs As New ADODBRecordset
Private Sub Command2_Click()
Set rs = cnExecute("select from wenzhang where 内容='" & Text1Text & "'")
If Text1Text = "" Then
MsgBox ("文章不能为空!")
ElseIf rsEOF = False Then
MsgBox ("文章不能重复!")
Else
cnExecute ("insert into wenzhang values('" & Combo1Text & "','" & Text3Text & "','" & Text4Text & "','" &
Text1Text & "')")
MsgBox ("ok")
End If
End Sub
'添加按钮
Private Sub Command3_Click()
Dim myval As String
Set rs = cnExecute("select from wenzhang where 内容='" & Text1Text & "'")
If rsEOF Then
myval = MsgBox("是否保存文章?", vbInformation + vbYesNoCancel, "提示")
If myval = vbYes Then
cnExecute ("insert into wenzhang values('" & Combo1Text & "','" & Text3Text & "','" & Text4Text & "','" &
Text1Text & "')")
MsgBox ("保存成功")
Else
Combo1Text = ""
End If
Else
Text1Text = ""
Combo1Text = ""
End If
End Sub
'删除按钮
Private Sub Command4_Click()
If Text1Text = "" Then
MsgBox ("不能删除空记录!")
Else
Dim myval As String
myval = MsgBox("是否删除文章?", vbInformation + vbYesNo, "提示")
If myval = vbYes Then
Set rs = cnExecute("delete from wenzhang where 内容 ='" & Text1Text & "'")
MsgBox ("删除成功!")
End If
End If
End Sub
'连接数据库
Private Sub Form_Load()
cnConnectionString = "Provider=MicrosoftJetOLEDB40;Data Source=\vbnet\tempmdb;Persist Security Info=False"
cnCursorLocation = adUseClient
cnOpen
End Sub
mports SystemDrawing
Imports SystemComponentModel
Imports SystemWindowsForms
Imports SystemDataOleDb
Imports SystemData
Public Class Form1
Inherits Form
Private components As SystemComponentModelContainer = Nothing
Private WithEvents lastrec As Button
Private WithEvents nextrec As Button
Private WithEvents previousrec As Button
Private WithEvents firstrec As Button
Private t_books As TextBox
Private t_nl As TextBox
Private t_xb As TextBox
Private t_xm As TextBox
Private t_id As TextBox
Private l_books As Label
Private l_nl As Label
Private l_xb As Label
Private l_xm As Label
Private l_id As Label
Private label1 As Label
Private myDataSet As DataSet
Private WithEvents button1 As Button
Private WithEvents button2 As Button
Private WithEvents button3 As Button
Private WithEvents button4 As Button
Private myBind As BindingManagerBase
Public Sub New ( )
MyBaseNew ( )
GetConnected ( )
InitializeComponent ( )
End Sub
'清除在程序中使用过的资源
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
componentsDispose ( )
End If
End If
MyBaseDispose ( disposing )
End Sub
Public Sub GetConnected ( )
'创建一个数据连接
Dim strCon As String = " Provider = MicrosoftJetOLEDB40 ;
Data Source = dbmdb "
Dim myConn As OleDbConnection = New OleDbConnection ( )
myConnConnectionString = strCon
Dim strCom As String = " SELECT FROM person "
'创建一个 DataSet
myDataSet = New DataSet ( )
myConnOpen ( )
'通过OleDbDataAdapter对象得到一个数据集
Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter
( strCom , myConn )
'把Dataset绑定books数据表
myCommandFill ( myDataSet , "person" )
'关闭此数据连接
myConnClose ( )
End Sub
'插入数据记录 *** 作代码
Private Sub button2_Click (ByVal sender As Object , _
ByVal e As SystemEventArgs) Handles button2Click
'判断所有字段是否添完,添完则执行,反之d出提示
If ( t_idText <> "" And t_xmText <> ""
And t_xbText <> "" And t_nlText <> ""
And t_booksText <> "" ) Then
Dim myConn1 As String = " Provider = MicrosoftJetOLEDB40 ;
Data Source = dbmdb"
Dim myConn As OleDbConnection = New OleDbConnection ( myConn1 )
myConnOpen ( )
Dim strInsert As String = " INSERT INTO person ( id , xm , xb , nl
, zip ) VALUES ( " & _
t_idText + " , '" & _
t_xmText + "' , '" & _
t_xbText + "' , " & _
t_nlText + " , " & _
t_booksText + ")"
Dim inst As OleDbCommand = New OleDbCommand ( strInsert , myConn )
instExecuteNonQuery ( )
myConnClose ( )
myDataSetTables ( "person" )Rows ( myBindPosition )BeginEdit ( )
myDataSetTables ( "person" )Rows ( myBindPosition )EndEdit ( )
myDataSetTables ( "person" )AcceptChanges ( )
Else
MessageBoxShow ( "必须填满所有字段值!" , "错误!" )
End If
End Sub
建4个表:
表1:美食名称、美食类型ID、美食区域ID、美食分属ID、说明
表2:美食类型名称、美食类型ID
表3:美食区域名称、美食区域ID
表4:美食分属、美食分属ID(可注明具体地点、美食具体菜单等内容)
以上就是关于用access做数据库全部的内容,包括:用access做数据库、如何使用access建立数据库、请朋友帮忙:VB2008 *** 作Access数据库。完成连接、添加、修改、删除、读某个列值至变量等完整的代码示例谢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)