1. 程式人生 > >學生資訊管理系統之增(二):新增班級資訊流程

學生資訊管理系統之增(二):新增班級資訊流程

這裡寫圖片描述

第一步:資訊載入

組合框內新增資訊(AddItem屬性)

Private Sub Form_Load()
    comboGrade.AddItem "初中一年級"
    comboGrade.AddItem "初中二年級"
    comboGrade.AddItem "初中三年級"
    comboGrade.AddItem "高中一年級"
    comboGrade.AddItem "高中二年級"
    comboGrade.AddItem "高中三年級"

End Sub

第二步:文字框判斷

2.1 判斷各個文字框有無文字(無文字彈出提示,有文字進入第三步)

If Not Testtxt(txtClassno.Text) Then
        MsgBox "請輸入班號!", vbOKOnly + vbExclamation, "警告"
        txtClassno.SetFocus
        Exit Sub
    End If

    If Not Testtxt(comboGrade.Text) Then
        MsgBox "請選擇年級!", vbOKOnly + vbExclamation, "警告"
        comboGrade.SetFocus
        Exit Sub
    End
If If Not Testtxt(txtDirector.Text) Then MsgBox "請輸入班主任姓名!", vbOKOnly + vbExclamation, "警告" txtDirector.SetFocus Exit Sub End If If Not Testtxt(txtClassroom.Text) Then '如果沒有檢測到txt,那麼 MsgBox "請輸入教室房間號!", vbOKOnly + vbExclamation, "警告" txtClassroom.SetFocus Exit
Sub End If

2.2 如何判斷有無文字?

Testtxt函式

Public Function Testtxt(txt As String) As Boolean
    If Trim(txt) = "" Then
        Testtxt = False
    Else
        Testtxt = True
    End If
End Function

2.3 如何判斷文字為數字?

IsNumeric函式

If Not IsNumeric(Trim(txtClassno.Text)) Then
        MsgBox "請輸入數字!", vbOKOnly + vbExclamation, "警告"
        txtClassno.SetFocus
        Exit Sub
End If

第三步:連線資料庫,判斷文字框內容是否已存在

3.1 ExecuteSQL

Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
'execute SQL and return recordset
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim sTokens() As String
    On Error GoTo ExecuteSQL_Error
    sTokens = Split(SQL)
    Set cnn = New ADODB.Connection
    cnn.Open ConnectString
    If InStr("INSERT,DELETE,UPDATE", UCase$(sTokens(0))) Then
        cnn.Execute SQL
        MsgString = sTokens(0) & " query successful"
    Else
        Set rst = New ADODB.Recordset
        rst.Open Trim$(SQL), cnn, adOpenKeyset, adLockOptimistic
        Set ExecuteSQL = rst
        MsgString = "查詢到" & rst.RecordCount & "條記錄"
    End If
ExecuteSQL_Exit:
    Set rst = Nothing
    Set cnn = Nothing
    Exit Function
ExecuteSQL_Error:
    MsgString = "查詢錯誤:" & _
        Err.Description
    Resume ExecuteSQL_Exit
End Function

3.2判斷所查詢內容是否存在(存在彈出提示,不存在就進入第四步)

If Not (mrc.EOF And mrc.BOF) Then
        mrc.MoveFirst
    End If
    While (mrc.EOF = False)
        If (Trim(mrc.Fields(0)) = Trim(txtClassno.Text)) Then
            MsgBox "班號已經存在,請重新輸入班號!", vbOKOnly + vbExclamation, "警告"
            txtClassno.Text = ""
            txtClassno.SetFocus
            Exit Sub
        Else
            mrc.MoveNext
        End If
    Wend

第四步: 將輸入的文字內容新增至資料庫

mrc.AddNew
    mrc.Fields(0) = Trim(txtClassno.Text)
    mrc.Fields(1) = Trim(comboGrade.Text)
    mrc.Fields(2) = Trim(txtDirector.Text)
    mrc.Fields(3) = Trim(txtClassroom.Text)
    mrc.Update
    mrc.Close
    MsgBox "新增班級資訊成功!", vbOKOnly + vbExclamation, "新增班級資訊"

第五步:資訊新增成功