⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 -

📁 数据库access基本应用
💻
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   4740
   ClientLeft      =   120
   ClientTop       =   450
   ClientWidth     =   4650
   LinkTopic       =   "Form1"
   ScaleHeight     =   4740
   ScaleWidth      =   4650
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Delete 
      Caption         =   "删除"
      Height          =   495
      Left            =   3120
      TabIndex        =   7
      Top             =   4200
      Width           =   1095
   End
   Begin VB.CommandButton Change 
      Caption         =   "修改"
      Height          =   495
      Left            =   3120
      TabIndex        =   6
      Top             =   3360
      Width           =   1095
   End
   Begin VB.CommandButton Add 
      Caption         =   "添加"
      Height          =   495
      Left            =   3120
      TabIndex        =   5
      Top             =   2520
      Width           =   1095
   End
   Begin MSWinsockLib.Winsock Winsock1 
      Left            =   120
      Top             =   1680
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin VB.CommandButton Find 
      Caption         =   "查询"
      Height          =   495
      Left            =   3120
      TabIndex        =   2
      Top             =   1680
      Width           =   1095
   End
   Begin VB.TextBox StudentNumber 
      Height          =   500
      Left            =   1320
      TabIndex        =   1
      Top             =   1000
      Width           =   2895
   End
   Begin VB.TextBox StudentName 
      Height          =   500
      Left            =   1320
      TabIndex        =   0
      Top             =   300
      Width           =   2895
   End
   Begin VB.Label Label2 
      Alignment       =   2  'Center
      Caption         =   "学号"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   14.25
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   350
      Left            =   350
      TabIndex        =   4
      Top             =   1080
      Width           =   800
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      Caption         =   "姓名"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   14.25
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   350
      Left            =   350
      TabIndex        =   3
      Top             =   350
      Width           =   800
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Public cnn As ADODB.Connection          'ado.connection变量
Public rs1 As ADODB.Recordset           'ADODB.Recordset 变量


Public Sub db_open() '连接并打开数据库

    Set cnn = New ADODB.Connection
    Set rs1 = New ADODB.Recordset
    
    Dim strSQL As String
    strSQL = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\user.mdb;Persist Security Info=False "
    cnn.Open strSQL '连接数据库

End Sub


Private Sub Add_Click() '添加
    Dim strSQL As String
    
    If StudentName.Text <> "" And StudentNumber.Text <> "" Then
    
    Call db_open                                    '连接数据库
    
    strSQL = "select * from 学生信息 where 姓名 = '" & StudentName.Text & "' or 学号='" & StudentNumber.Text & "'"
    Set rs1 = cnn.Execute(strSQL)                   '执行sql查询语句并返回建立记录集
    
    If rs1.EOF = False Then
    
    MsgBox "对不起,该姓名或学号已存在!", vbOKCancel, "提示信息"
    cnn.Close                                       '断开与数据库的连接
    
    Exit Sub
    End If
    
    If rs1.EOF = True Then
                                                    '添加新的信息
    strSQL = "INSERT INTO 学生信息 (姓名, 学号) VALUES ('" & StudentName.Text & "', '" & StudentNumber.Text & "')"
    cnn.Execute (strSQL)                            '执行sql查询语句并返回建立记录集
    cnn.Close                                       '断开与数据库的连接
    MsgBox "数据添加成功!", vbOKCancel, "提示信息"
    Exit Sub
    End If
    
    End If
    
    If StudentName.Text = "" Or StudentNumber.Text = "" Then
  
    MsgBox "对不起,学号和姓名都不能为空!", vbOKCancel, "提示信息"
    
    Exit Sub
    End If
    
End Sub

Private Sub Change_Click()
    Dim strSQL As String
  
    If StudentName.Text <> "" And StudentNumber.Text <> "" Then
  
    Call db_open                                    '连接数据库

    strSQL = "Update  学生信息 Set  学号='" & StudentNumber.Text & "' where 姓名='" & StudentName.Text & "'"
    cnn.Execute (strSQL)                            '执行sql查询语句并返回建立记录集
    cnn.Close                                       '断开与数据库的连接
    MsgBox "数据修改成功!", vbOKCancel, "提示信息"
    
    Exit Sub
    End If
    
    If StudentName.Text = "" Or StudentNumber.Text = "" Then
  
    MsgBox "学号和姓名不能为空!", vbOKCancel, "提示信息"
  
    End If
    
End Sub

Private Sub Delete_Click() '删除
    Dim strSQL As String
  
    If StudentName.Text <> "" And StudentNumber.Text <> "" Then
  
    Call db_open                                    '连接数据库
    
    strSQL = "Delete from 学生信息  Where 姓名='" & StudentName.Text & "'"
    cnn.Execute (strSQL)                            '执行sql查询语句并返回建立记录集
    cnn.Close                                       '断开与数据库的连接
    
    MsgBox "数据删除成功!", vbOKCancel, "提示信息"
    
    Exit Sub
    End If
  
    If StudentName.Text = "" Or StudentNumber.Text = "" Then
  
    MsgBox "你还没有选择需要删除的记录!", vbOKCancel, "提示信息"
  
    End If
End Sub

Private Sub Find_Click() '查询
    Dim strSQL As String
    '---------------------------------------姓名查询-------------------------------------
    If StudentName.Text <> "" Then
  
    Call db_open                                    '连接数据库
    
    strSQL = "select * from 学生信息 where 姓名 = '" & StudentName.Text & "'"
    Set rs1 = cnn.Execute(strSQL)                   '执行sql查询语句并返回建立记录集
    
    If rs1.EOF = False Then
    
    StudentNumber.Text = rs1.Fields("学号").Value   '将返回信息显示在界面上的相应控件
    cnn.Close                                       '断开与数据库的连接
    
    Exit Sub
    End If
    
    If rs1.EOF = True Then
    
    MsgBox "对不起,查无此人!", vbOKCancel, "提示信息"
    cnn.Close                                       '断开与数据库的连接
    
    Exit Sub
    End If
    
    End If
    '---------------------------------------学号查询-------------------------------------
    If StudentNumber.Text <> "" Then
  
    Call db_open                                    '连接数据库
  
    strSQL = "select * from 学生信息 where 学号 = '" & StudentNumber.Text & "'"
    Set rs1 = cnn.Execute(strSQL)                   '执行sql查询语句并返回建立记录集
  
    If rs1.EOF = False Then
  
    StudentName.Text = rs1.Fields("姓名").Value     '输出表中各字段
    cnn.Close
    
    Exit Sub
    End If
    
    If rs1.EOF = True Then
  
    MsgBox "对不起,查无此人!", vbOKCancel, "提示信息"
    cnn.Close                                       '断开与数据库的连接
    
    Exit Sub
    End If
    
    End If
    
    '---------------------------------------输入都为空-------------------------------------
    If StudentName.Text = "" And StudentNumber.Text = "" Then
  
    MsgBox "请输入姓名或学号!", vbOKCancel, "提示信息"
    
    Exit Sub
    End If
    
End Sub


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -