Option ExplicitDim rst As Recordset
Dim strSQL As StringPrivate Sub cmdAddNew_Click()    With MSFlexGrid1
        .AddItem ""
        .Col = 0
        .Row = .Rows - 1
        .RowHeight(.Row) = 300
        .Text = str(.Row)
        '如果记录数大于9,则上卷至最后一行
        If .Rows > 9 Then
            .TopRow = .Rows - 9
        End If
        .Col = 1
        .SetFocus
        Call MSFlexGrid1_DblClick
    End WithEnd SubPrivate Sub cmdDel_Click()    With MSFlexGrid1
        Dim c As Integer, r As Integer
        c = .Col: r = .Row
        If r = 0 Then
            MsgBox "请选择您要删除的列!", vbCritical, "提示"
            Exit Sub
        End If
        .Col = 0
        If MsgBox("您真的要删除 " + .Text + " 号人员的档案吗?", vbQuestion + vbYesNo, "确认") = vbNo Then
            .Col = c
            Exit Sub
        Else
            If .Rows <= 2 Then
                .Rows = 1
                Exit Sub
            End If
            .RemoveItem r
            '调整序号
            Dim i As Integer
            For i = r To .Rows - 1
                .Row = i
                .Text = str(i)
            Next i
            .Refresh
            .Col = c
        End If
    End WithEnd SubPrivate Sub cmdExit_Click()    Unload MeEnd SubPrivate Sub Form_Load()'    Me.Left = gintX
'    Me.Top = gintY
    
    Dim i As Integer
    
    strSQL = "select EID,EName from Employee order by EID"
    Set rst = gdb.OpenRecordset(strSQL)    Label1.Caption = "如果您发现有的记录始终不能保存,请检查编号是否有重复!" + vbCr + _
        vbCr + "您更新的数据将会在窗口退出时自动保存。" + vbCr + vbCr + "当前已保存的员工数 "    With MSFlexGrid1
        .Cols = 3
        If rst.RecordCount = 0 Then
'            MsgBox "目前尚无记录!", vbCritical, "提示"
            .Rows = 1
            .RowHeight(0) = 300
            .Row = 0
            '设置第0列为序号
            .ColWidth(0) = 500
            .ColAlignment(0) = flexAlignLeftCenter
            .Text = "序号"
            .Col = 1
            .ColAlignment(1) = flexAlignCenterCenter
            .Text = "姓名"
            .Col = 2
            .ColAlignment(2) = flexAlignCenterCenter
            .Text = "编号"
            Label1.Caption = Label1.Caption + "0"
        Else
            rst.MoveLast
            rst.MoveFirst
            Label1.Caption = Label1.Caption + str(rst.RecordCount)
            '设置第0列为序号
            .Col = 0
            .ColWidth(0) = 500
            .ColAlignment(0) = flexAlignCenterCenter
            .Rows = rst.RecordCount + 1
            For i = 0 To rst.RecordCount
                .RowHeight(i) = 300
                .Row = i
                .Text = str(i)  '设置行号
            Next i
            .Row = 0
            .Text = "序号"  '消除首行行号
            '设置第1列为姓名,第2列为编号
            .Col = 1
            .ColAlignment(1) = flexAlignCenterCenter
            .Row = 0
            .Text = "姓名"
            rst.MoveFirst
            Do While Not rst.EOF
                .Row = rst.AbsolutePosition + 1
                .Text = rst("EName")
                rst.MoveNext
            Loop
            .Col = 2
            .ColAlignment(2) = flexAlignCenterCenter
            .Row = 0
            .Text = "编号"
            rst.MoveFirst
            Do While Not rst.EOF
                .Row = rst.AbsolutePosition + 1
                .Text = rst("EID")
                rst.MoveNext
            Loop
        End If
        '设置焦点
        .Col = 0
        .Row = 0
        .Width = .ColWidth(0) + .ColWidth(1) + .ColWidth(2) + 200
        Frame1.Width = .Width + (.Left - Frame1.Left) * 2
    End WithEnd SubPrivate Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)    Dim i As Integer
    
    '由于员工不会很多,故采取删除原表在建新表的方法来更新,避免了复杂的效验
    strSQL = "delete * from Employee"
    gdb.Execute (strSQL)
    
    With MSFlexGrid1
        If .Rows = 1 Then   '无记录
'            Cancel = 1
            Exit Sub
        Else
            '数据合法性效验
            For i = 1 To .Rows - 1
                .Row = i
                .Col = 1
                If .Text = "" Then
                    MsgBox "第 " + str(i) + " 号员工姓名未填!" + vbCr + "请填充或删除该职工。", vbCritical, "出错"
                    Cancel = 1
                    Exit Sub
                End If
                .Col = 2
                If .Text = "" Then
                    MsgBox "第 " + str(i) + " 号员工编号未填!" + vbCr + "请填充或删除该职工。", vbCritical, "出错"
                    Cancel = 1
                    Exit Sub
                End If
            Next i
            
            For i = 1 To .Rows - 1
                .Row = i
                .Col = 2
                strSQL = "insert into Employee (EID,EName) select '" + .Text + "','"
                .Col = 1
                strSQL = strSQL + .Text + "'"
                gdb.Execute (strSQL)
            Next i
            Cancel = 0
        End If
    End WithEnd SubPrivate Sub MSFlexGrid1_DblClick()
    
    Dim c As Integer, r As Integer
    With MSFlexGrid1
        r = .Row: c = .Col
        If c = 0 Then Exit Sub
        Text1.Left = .Left + .ColPos(c)
        Text1.Top = .Top + .RowPos(r)
        Text1.Width = .ColWidth(c)
        Text1.Height = .RowHeight(r)
        Text1 = .Text
        Text1.Visible = True
        Text1.SetFocus
    End With
    
End SubPrivate Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)    If KeyAscii = vbKeyReturn Then
        Call MSFlexGrid1_DblClick
    End If
    
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)    If KeyAscii = vbKeyEscape Then
        Text1.Visible = False
        MSFlexGrid1.SetFocus
        Exit Sub
    End If
    If KeyAscii = vbKeyReturn Then
        MSFlexGrid1.Text = Trim(Text1.Text)
        Text1.Visible = False
        MSFlexGrid1.SetFocus
    End If
    
End SubPrivate Sub Text1_LostFocus()    Text1.Visible = False
    MSFlexGrid1.SetFocus
    
End Sub