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

📄 添加成绩.frm

📁 初学vb,做的第一个系统.一个星期内看vb并做完这个系统的.
💻 FRM
字号:
VERSION 5.00
Object = "{67397AA1-7FB1-11D0-B148-00A0C922E820}#6.0#0"; "MSADODC.OCX"
Begin VB.Form gra_add 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "添加成绩"
   ClientHeight    =   2895
   ClientLeft      =   5850
   ClientTop       =   4410
   ClientWidth     =   3870
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   2895
   ScaleWidth      =   3870
   Begin MSAdodcLib.Adodc Adodc1 
      Height          =   330
      Left            =   240
      Top             =   2520
      Visible         =   0   'False
      Width           =   1200
      _ExtentX        =   2117
      _ExtentY        =   582
      ConnectMode     =   0
      CursorLocation  =   3
      IsolationLevel  =   -1
      ConnectionTimeout=   15
      CommandTimeout  =   30
      CursorType      =   3
      LockType        =   3
      CommandType     =   8
      CursorOptions   =   0
      CacheSize       =   50
      MaxRecords      =   0
      BOFAction       =   0
      EOFAction       =   0
      ConnectStringType=   1
      Appearance      =   1
      BackColor       =   -2147483643
      ForeColor       =   -2147483640
      Orientation     =   0
      Enabled         =   -1
      Connect         =   "Provider=MSDASQL.1;Persist Security Info=False;Data Source=stu_manage"
      OLEDBString     =   "Provider=MSDASQL.1;Persist Security Info=False;Data Source=stu_manage"
      OLEDBFile       =   ""
      DataSourceName  =   ""
      OtherAttributes =   ""
      UserName        =   "sa"
      Password        =   "manager"
      RecordSource    =   ""
      Caption         =   "Adodc1"
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      _Version        =   393216
   End
   Begin VB.CommandButton Command1 
      Caption         =   "添加"
      Height          =   375
      Left            =   2400
      TabIndex        =   6
      Top             =   2400
      Width           =   975
   End
   Begin VB.TextBox Text2 
      Height          =   375
      Left            =   1680
      TabIndex        =   5
      Top             =   1800
      Width           =   1455
   End
   Begin VB.TextBox Text1 
      Height          =   375
      Left            =   1680
      TabIndex        =   3
      Top             =   1320
      Width           =   1455
   End
   Begin VB.ComboBox Combo1 
      Height          =   300
      Left            =   1680
      Style           =   2  'Dropdown List
      TabIndex        =   1
      Top             =   240
      Width           =   1455
   End
   Begin VB.Label sub_name 
      Height          =   255
      Left            =   1680
      TabIndex        =   8
      Top             =   840
      Width           =   1455
   End
   Begin VB.Label Label4 
      Caption         =   "课程名称"
      Height          =   255
      Left            =   720
      TabIndex        =   7
      Top             =   840
      Width           =   735
   End
   Begin VB.Label Label3 
      Caption         =   "成    绩"
      Height          =   255
      Left            =   720
      TabIndex        =   4
      Top             =   1920
      Width           =   735
   End
   Begin VB.Label Label2 
      Caption         =   "学生学号"
      Height          =   255
      Left            =   720
      TabIndex        =   2
      Top             =   1380
      Width           =   735
   End
   Begin VB.Label Label1 
      Caption         =   "课程编码"
      Height          =   255
      Left            =   720
      TabIndex        =   0
      Top             =   240
      Width           =   735
   End
End
Attribute VB_Name = "gra_add"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
    Public rs As New ADODB.Recordset
    Public adoconn As New ADODB.Connection 'Connection 对象代表了打开与数据源的连接。
    Public sub_id As String
    Public stu_no As String
    Public str_gra As String '获取成绩输入处字符串
    Public grade As Single '转换成数值型
    
Private Sub Combo1_Click()
    sub_id = Combo1.Text
    adoconn.Open
    rs.Open "select * from subject where id='" & sub_id & "'", adoconn
    If rs.RecordCount = 0 Then
        MsgBox "没有该课程!"
        rs.Close
        adoconn.Close
        Exit Sub
    End If
    If sub_id = rs.Fields("id") Then
        sub_name = rs.Fields("name")
        rs.Close
        adoconn.Close
    End If
End Sub

Private Sub Command1_Click()
    sub_id = Combo1.Text
    stu_no = Text1.Text
    str_gra = Text2.Text
    grade = Val(str_gra)
    
    If Len(sub_id) = 0 Then
        MsgBox "请选择课程编码!"
        Combo1.SetFocus
        Exit Sub
    ElseIf Len(stu_no) = 0 Then
        MsgBox "请输入学生学号!"
        Text1.SetFocus
        Exit Sub
    ElseIf Len(str_gra) = 0 Then
        MsgBox "请输入成绩!"
        Text2.SetFocus
        Exit Sub
    ElseIf grade < 0 Or grade > 100 Then
        MsgBox "成绩必须在0-100之内!"
        Text2.Text = ""
        Text2.SetFocus
        Exit Sub
    End If
    
    '没有任何学生
    adoconn.Open
    rs.Open "select * from stu", adoconn
    If rs.RecordCount = 0 Then
        rs.Close
        adoconn.Close
        Exit Sub
    End If
    rs.Close
    
    '判断该学生是否存在
    rs.Open "select * from stu where no='" & stu_no & "'", adoconn
    '不存在
    If rs.RecordCount = 0 Then
        MsgBox "没有该学号学生!"
        rs.Close
        adoconn.Close
        Text1.SetFocus
        Exit Sub
    Else
    '存在该学号
        Dim count As Integer, i As Integer
        rs.Close
        count = 0
        
        rs.Open "select * from grade where stu_no='" & stu_no & "'", adoconn
        
        '不存在该生的成绩记录
        If rs.RecordCount = 0 Then
            rs.Close
            Call add
            Exit Sub
        End If
      
        rs.Close
        rs.Open "select * from grade where sub_id='" & sub_id & "' and stu_no='" & stu_no & "'", adoconn
        
        '不存在该生该科目的成绩
        If rs.RecordCount = 0 Then
            rs.Close
            Call add
            Exit Sub
        End If
        
        '存在该生该科目的成绩
        Dim r As Integer
        r = MsgBox("该学生成绩已经存在,是否修改?", 4 + 32 + 0, "确认修改")
        If r = 6 Then
            rs.Fields("grade") = grade
            rs.Update
            MsgBox "修改原成绩成功!"
        End If

        '选择否
            Text1.Text = ""
            Text2.Text = ""
            Text1.SetFocus
            rs.Close
            adoconn.Close
    End If
End Sub
'添加
Private Sub add()
            rs.Open "select * from grade", adoconn
            rs.AddNew
            rs.Fields("sub_id") = sub_id
            rs.Fields("stu_no") = stu_no
            rs.Fields("grade") = grade
            rs.Update
            rs.Close
            adoconn.Close
            MsgBox "添加成功!"
            Text1.Text = ""
            Text2.Text = ""
            Text1.SetFocus
End Sub

Private Sub Form_Load()
    adoconn.ConnectionString = Adodc1.ConnectionString 'Adodc1为窗体中的ADO控件,并已成功连接数据库
    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    rs.CursorType = adOpenKeyset
    rs.LockType = adLockOptimistic
    
    '加载下拉列表
    adoconn.Open
    rs.Open "select * from subject", adoconn
    If rs.RecordCount = 0 Then
        rs.Close
        adoconn.Close
        Exit Sub
    End If
    
    Dim i As Integer
    rs.MoveFirst
    For i = 0 To rs.RecordCount - 1
        Combo1.AddItem rs.Fields("id")
        rs.MoveNext
    Next i
    
    rs.Close
    adoconn.Close
End Sub

⌨️ 快捷键说明

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