VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.2#0"; "MSCOMCTL.OCX"
Object = "{F39CBE5C-2D1B-11D6-8D4A-0050043A2E60}#2.0#0"; "ProgBarProzent.ocx"
Begin VB.Form Form_Notas 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Calificaciones de Asignaturas"
   ClientHeight    =   7860
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   17520
   ForeColor       =   &H8000000A&
   LinkTopic       =   "Form3"
   MaxButton       =   0   'False
   MDIChild        =   -1  'True
   MinButton       =   0   'False
   Moveable        =   0   'False
   ScaleHeight     =   7860
   ScaleWidth      =   17520
   Begin VB.CommandButton cmd_exportar 
      Caption         =   "Exportar Notas Excel"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   11520
      TabIndex        =   35
      Top             =   5520
      Width           =   2175
   End
   Begin VB.Frame Frame3 
      Caption         =   "Acciones Rápidas"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00C00000&
      Height          =   1215
      Left            =   120
      TabIndex        =   29
      Top             =   6480
      Width           =   17175
      Begin VB.Label Label11 
         Caption         =   "Columna Amarilla: Examen 20% o 30%, XP2: Promedio Segundo Semestre, XP: Promedio al 80% o 20%, XF: Promedio 100 (30% + 70%)"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   615
         Left            =   11880
         TabIndex        =   34
         Top             =   360
         Visible         =   0   'False
         Width           =   4815
      End
      Begin VB.Label Label10 
         Caption         =   "Presione la tecla 'Esc' de su teclado para borrar una casilla completa"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   615
         Left            =   8160
         TabIndex        =   32
         Top             =   360
         Width           =   3135
      End
      Begin VB.Label Label9 
         Caption         =   "Presione la letra 'f' de su teclado para borrar una fila completa por semestre"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   615
         Left            =   4200
         TabIndex        =   31
         Top             =   360
         Width           =   3255
      End
      Begin VB.Label Label8 
         Caption         =   "Presione la letra 'c' de su teclado para borrar una columna completa por semestre"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   615
         Left            =   360
         TabIndex        =   30
         Top             =   360
         Width           =   3255
      End
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Exp. Prom."
      Enabled         =   0   'False
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   15960
      TabIndex        =   15
      Top             =   4920
      Visible         =   0   'False
      Width           =   1215
   End
   Begin VB.CommandButton Command5 
      Caption         =   "Ranking"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   14640
      TabIndex        =   26
      Top             =   4920
      Visible         =   0   'False
      Width           =   1215
   End
   Begin VB.ComboBox Cmd_Per 
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   12240
      Style           =   2  'Dropdown List
      TabIndex        =   24
      Top             =   4920
      Visible         =   0   'False
      Width           =   2295
   End
   Begin VB.CommandButton Command2 
      Caption         =   "Command2"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   13920
      TabIndex        =   23
      Top             =   5520
      Visible         =   0   'False
      Width           =   1455
   End
   Begin ProgBarProzent.ProgBar ProgBar1 
      Height          =   375
      Left            =   120
      TabIndex        =   16
      Top             =   6000
      Width           =   17175
      _ExtentX        =   30295
      _ExtentY        =   661
   End
   Begin VB.CommandButton Btn_Can_Not 
      Caption         =   "Cancelar"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   855
      Left            =   16080
      Style           =   1  'Graphical
      TabIndex        =   14
      Top             =   240
      Width           =   1155
   End
   Begin VB.CommandButton Btn_Ace_Not 
      Caption         =   "Grabar"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   855
      Left            =   14760
      Style           =   1  'Graphical
      TabIndex        =   13
      Top             =   240
      Width           =   1155
   End
   Begin VB.Frame Frame2 
      Caption         =   "Asignatura"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00C00000&
      Height          =   1035
      Left            =   8280
      TabIndex        =   9
      Top             =   120
      Width           =   6255
      Begin VB.CommandButton cerrar_asignatura 
         Caption         =   "Cerrar Asignatura"
         Enabled         =   0   'False
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   615
         Left            =   5040
         TabIndex        =   33
         Top             =   240
         Visible         =   0   'False
         Width           =   1095
      End
      Begin MSComctlLib.ImageCombo Cmb_Sector 
         Height          =   345
         Left            =   960
         TabIndex        =   5
         Top             =   240
         Width           =   3855
         _ExtentX        =   6800
         _ExtentY        =   609
         _Version        =   393216
         ForeColor       =   -2147483640
         BackColor       =   -2147483643
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Locked          =   -1  'True
      End
      Begin VB.ComboBox Cmb_Asi 
         Enabled         =   0   'False
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   330
         Left            =   960
         Style           =   2  'Dropdown List
         TabIndex        =   10
         Top             =   600
         Width           =   3855
      End
      Begin VB.Label Label4 
         Caption         =   "Subsector"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   195
         Left            =   120
         TabIndex        =   12
         Top             =   720
         Width           =   975
      End
      Begin VB.Label Label3 
         Caption         =   "Sector"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   195
         Left            =   120
         TabIndex        =   11
         Top             =   360
         Width           =   735
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "Curso"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00C00000&
      Height          =   1035
      Left            =   120
      TabIndex        =   1
      Top             =   120
      Width           =   7995
      Begin VB.ComboBox cmb_agno 
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   330
         Left            =   2100
         Style           =   2  'Dropdown List
         TabIndex        =   27
         Top             =   240
         Width           =   1335
      End
      Begin VB.ComboBox Cmb_Let_Cur 
         Enabled         =   0   'False
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   330
         Left            =   6360
         Style           =   2  'Dropdown List
         TabIndex        =   4
         Top             =   600
         Width           =   1335
      End
      Begin VB.ComboBox Cmb_Grado 
         Enabled         =   0   'False
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   330
         Left            =   6360
         Style           =   2  'Dropdown List
         TabIndex        =   3
         Top             =   240
         Width           =   1335
      End
      Begin VB.ComboBox Cmb_Cod_ens 
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   330
         Left            =   2100
         Style           =   2  'Dropdown List
         TabIndex        =   2
         Top             =   600
         Width           =   2475
      End
      Begin VB.Label Label7 
         Caption         =   "Año Escolar"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   255
         Left            =   240
         TabIndex        =   28
         Top             =   360
         Width           =   975
      End
      Begin VB.Label Tipo 
         Caption         =   "Tipo de enseñanza"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   195
         Left            =   240
         TabIndex        =   8
         Top             =   720
         Width           =   1455
      End
      Begin VB.Label Label1 
         Caption         =   "Grado"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   195
         Left            =   4920
         TabIndex        =   7
         Top             =   360
         Width           =   615
      End
      Begin VB.Label Label2 
         Caption         =   "Letra Curso"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   195
         Left            =   4920
         TabIndex        =   6
         Top             =   720
         Width           =   1215
      End
   End
   Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1 
      Height          =   4095
      Left            =   0
      TabIndex        =   0
      Top             =   1320
      Width           =   17235
      _ExtentX        =   30401
      _ExtentY        =   7223
      _Version        =   393216
      Cols            =   3
      BackColor       =   16777215
      ForeColorSel    =   16777215
      BackColorBkg    =   16777215
      Enabled         =   -1  'True
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
   End
   Begin VB.Label Label6 
      Caption         =   "Período"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   5760
      TabIndex        =   25
      Top             =   5040
      Visible         =   0   'False
      Width           =   735
   End
   Begin VB.Label LbPromG 
      Caption         =   "0,0"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   16920
      TabIndex        =   22
      Top             =   5520
      Width           =   375
   End
   Begin VB.Label LbPromL 
      Caption         =   "0,0"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   1680
      TabIndex        =   21
      Top             =   5520
      Width           =   375
   End
   Begin VB.Label LbPromH 
      Caption         =   "0,0"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   4080
      TabIndex        =   20
      Top             =   5520
      Width           =   375
   End
   Begin VB.Label Lb2 
      Caption         =   "Promedio general :"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   15480
      TabIndex        =   19
      Top             =   5520
      Width           =   1335
   End
   Begin VB.Label Lb1 
      Caption         =   "Promedio más bajo :"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   120
      TabIndex        =   18
      Top             =   5520
      Width           =   1455
   End
   Begin VB.Label Label5 
      Caption         =   "Promedio más alto :"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   2520
      TabIndex        =   17
      Top             =   5520
      Width           =   1455
   End
End
Attribute VB_Name = "Form_Notas"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim PERIODOS()
Dim EVALUACIONES()
Dim NumNotas, EV, PE, X_ev, Celda  As Integer
Dim RowAct, ColAct As Long
Dim EntraColChange, GuardaPromedios As Boolean
Dim CodModalidad, Ngrado, Codsec, Codasi, txtCel, CierrePer As String
Dim RutUsuario As String
Dim cole As String
Dim faltante As Integer

Dim pn As Integer

Private Sub Btn_Ace_Not_Click()
Dim RutProfe As String
Dim objRS As ADODB.Recordset
Set objRS = New ADODB.Recordset
Dim var_sql As String
Dim accion_realizada As String
Dim grado As String
Dim curso As String
Dim log As Boolean

' Devuelve colegio
cole = DevuelveCampo("cfg", "", "valor", "param='CODBC'", conn)
'MsgBox Codsec & "-" & Codasi

If Cmb_Asi.Text = "" Then
    MsgBox "No es posible ingresar notas si no ha seleccionado el subsector correspondiente, por favor intentar nuevamente.", vbInformation
    Exit Sub
End If

' Valido que no guarden nota en Ciencias Naturales
'If cole = "FAR001" Or cole = "LON001" Or cole = "FLO001" Or cole = "SNM001" Then
'    If Codsec = "0011" And Codasi = 6 And CodModalidad = "310" And Cmb_Grado = 1 Then
'        MsgBox "No se pueden guardar notas en esta asignatura", vbInformation
'        Exit Sub
'    End If
'End If

' Valido que no guarden nota en Ciencias Naturales 2° Medio
If cole = "LON001" Then
    If Codsec = "0011" And Codasi = 6 And CodModalidad = "310" And Cmb_Grado = 2 Then
        MsgBox "No se pueden guardar notas en esta asignatura", vbInformation
        Exit Sub
    End If
End If

' Valido que no guarden nota en Biologia Quimica y fisica
If cole = "SNB001" Then
    If Codsec = "0011" And (Codasi = 3 Or Codasi = 8 Or Codasi = 7) And CodModalidad = "310" And (Cmb_Grado = 1 Or Cmb_Grado = 2) Then
        MsgBox "No se pueden guardar notas en esta asignatura, debe ingresarlas en Ciencias Naturales", vbInformation
        Exit Sub
    End If
End If


var_sql = "SELECT rut_prof FROM cursos" _
        & " where cod_ens = '" & CodModalidad & "' and num_gra = " & Cmb_Grado _
        & " and let_cur = '" & Cmb_Let_Cur & "' and agno = " & cmb_agno.Text
objRS.Open var_sql, conn
RutProfe = objRS("rut_prof")
objRS.Close

If Me.Cmb_Cod_ens.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Cod_ens.SetFocus: Exit Sub
If Me.Cmb_Grado.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Grado.SetFocus: Exit Sub
If Me.Cmb_Let_Cur.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Let_Cur.SetFocus: Exit Sub
If Me.Cmb_Sector.SelectedItem.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Sector.SetFocus: Exit Sub
If Me.Cmb_Asi.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Asi.SetFocus: Exit Sub

'Permiso grabar notas
permiso = val(DevuelveCampo("usuarios", "", "MODIFICAR_NOTAS", "aut_nombre_usuario = '" & Nom_usu_ses & "'", conn))
If permiso = 0 Then
     Select Case CodModalidad
        Case "996"
            grado = "PK"
        Case "010"
            grado = "K"
        Case "110"
            grado = "EB"
        Case "310"
            grado = "EM"
    End Select
    curso = CStr(Cmb_Grado) & "º " & Cmb_Let_Cur & " " & grado
    accion_realizada = "El usuario a consultado las notas del subsector " & Cmb_Asi.Text _
                     & " que pertenece al sector " & Cmb_Sector.Text _
                     & " para el curso " & curso & " del año " _
                     & cmb_agno.Text
    log = Inserta_Vitacora_Profesores(Trim(RutProfe), "Consulta de notas", Trim(accion_realizada))

    MsgBox "Ud. no tiene privilegios para modificar notas en este modulo", vbInformation
    Exit Sub
    
End If

Dim CelIniEva, CelIniPer As Integer
Dim CodRegOpe As String
Form_Notas.Enabled = False
Btn_Ace_Not.Enabled = False

Form_Progress.Label1.Caption = "Guardando ..."
Form_Progress.Show
Form_Progress.ProgBar1.MaxValue = Me.MSFlexGrid1.Rows - 1

For R = 1 To Me.MSFlexGrid1.Rows - 1
 X = 0
    For p = 0 To PE - 1
        CelIniPer = PERIODOS(p, 4)
        For e = 0 To EV - 1
            CelIniEva = PERIODOS(p, 4) - 2 + EVALUACIONES(e, 6)
                For I = CelIniEva To CelIniEva + EVALUACIONES(e, 3) - 1
                    If MSFlexGrid1.TextMatrix(R, I) <> "" Then
                        notas = notas & Left(MSFlexGrid1.TextMatrix(R, I), 1) & Right(MSFlexGrid1.TextMatrix(R, I), 1)
                    Else
                        notas = notas & "  "
                    End If
                Next I
            '*** graba evaluacion "e" en periodo "p"
            Rut_ALu = Left(LTrim(MSFlexGrid1.TextMatrix(R, 0)), 10)
            If Not ExistReg("NOTAS", "CI_ALU ='" & Rut_ALu & "' AND COD_ENS ='" & CodModalidad & "' AND NUM_GRA ='" & Cmb_Grado & "' AND LETRA_CURSO ='" & Cmb_Let_Cur & "' AND COD_SEC ='" & Codsec & "' AND COD_ASI ='" & Codasi & "' AND NUM_PER ='" & p & "' AND COD_EVA ='" & e & "' and agno = " & cmb_agno.Text, conn) Then
                conn.Execute "INSERT INTO NOTAS(COD_ENS, NUM_GRA, LETRA_CURSO, COD_SEC, COD_ASI, CI_ALU, NUM_PER, COD_EVA, NOTA,AGNO) values('" & CodModalidad & "', '" & Cmb_Grado & "', '" & Cmb_Let_Cur & "', '" & Codsec & "', '" & Codasi & "', '" & Rut_ALu & "', '" & p & "', '" & e & "', '" & notas & "'," & cmb_agno.Text & ")"
                CodRegOpe = "AGR"
            Else
                conn.Execute "UPDATE NOTAS Set NOTA = '" & notas & "' WHERE CI_ALU ='" & Rut_ALu & "' AND COD_ENS ='" & CodModalidad & "' AND NUM_GRA ='" & Cmb_Grado & "' AND LETRA_CURSO ='" & Cmb_Let_Cur & "' AND COD_SEC ='" & Codsec & "' AND COD_ASI ='" & Codasi & "' AND NUM_PER ='" & p & "' AND COD_EVA ='" & e & "' and agno = " & cmb_agno.Text
                CodRegOpe = "MOD"
            End If
            '*** FIN graba evaluacion "e" en periodo "p"
            notas = ""
        X = CelIniPer - 2 + EVALUACIONES(e, 6) + EVALUACIONES(e, 3) + 1
        Next e
        'bvc = bvc & X & "-"
        '################################# GUARDA EN TABLA PROMEDIOS ##################
        Dim PromBd As String
        If MSFlexGrid1.TextMatrix(R, X + 1) = "" Then
            PromBd = ""
        Else
            PromBd = Left(CStr(MSFlexGrid1.TextMatrix(R, X + 1)), 1) & "." & Right(CStr(MSFlexGrid1.TextMatrix(R, X + 1)), 1)
        End If
        
        If val(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 3)) > 0 Then
            PG = Replace(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 3), ",", ".")
        Else
            PG = "Null"
        End If
        
        If val(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 2)) > 0 Then
            NE = Replace(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 2), ",", ".")
        Else
            NE = "Null"
        End If
        
        If val(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 1)) > 0 Then
            nf = Replace(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 1), ",", ".")
        Else
            nf = "Null"
        End If
        
        
        Rut_ALu = Left(MSFlexGrid1.TextMatrix(R, 0), 10)
        If Not ExistReg("promedios", "ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "' and agno =" & cmb_agno.Text, conn) Then
            conn.Execute "INSERT INTO PROMEDIOS(CI_ALU, COD_ENS, NUM_GRA, LETRA_CURSO, COD_SEC, COD_ASI, P" & p + 1 & ", PG, NE, NF,agno) values('" & Rut_ALu & "','" & CodModalidad & "','" & Me.Cmb_Grado & "','" & Me.Cmb_Let_Cur & "','" & Codsec & "','" & Codasi & "','" & PromBd & "','" & PG & "','" & NE & "','" & nf & "', " & cmb_agno.Text & " )"
        Else
            conn.Execute "UPDATE PROMEDIOS Set P" & p + 1 & " = '" & PromBd & "', PG ='" & PG & "', NE ='" & NE & "', NF ='" & nf & "' where ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "' and agno =" & cmb_agno.Text
        End If
        '##############################################################################

    
    Next p
    Form_Progress.ProgBar1.ShowProgress R
    DoEvents
Next R

'Log de modifición de notas
    Select Case CodModalidad
        Case "996"
            grado = "PK"
        Case "010"
            grado = "K"
        Case "110"
            grado = "EB"
        Case "310"
            grado = "EM"
    End Select
    curso = CStr(Cmb_Grado) & "º " & Cmb_Let_Cur & " " & grado
    accion_realizada = "Se han modificado las notas del subsector " & Cmb_Asi.Text _
                     & " que pertenece al sector " & Cmb_Sector.Text _
                     & " para el curso " & curso & " del año " _
                     & cmb_agno.Text
    log = Inserta_Vitacora_Profesores(Trim(RutProfe), "Modificación de notas", Trim(accion_realizada))
'Fin de log

'********** Registro de operaciones ***********
Call RegOpe(CodRegOpe, "NOTAS@" & CodModalidad & Me.Cmb_Grado & Me.Cmb_Let_Cur & "@" & Me.Cmb_Sector.Text & "@" & Me.Cmb_Asi, conn)
'********** FIN Registro de operaciones ***********
Form_Progress.ProgBar1.ShowProgress 0
Unload Form_Progress
MsgBox "Las Calificaciones han sido grabadas exitosamente"
Form_Notas.Enabled = True
Btn_Ace_Not.Enabled = True
End Sub

Private Sub Btn_Can_Not_Click()
Unload Me
End Sub

Private Sub cerrar_asignatura_Click()

a = "SELECT count(cerrado) as totalce FROM notas n, alu_x_cur c where n.ci_alu = c.ci_alu and n.agno = c.agno "
a = a & "and n.letra_curso = c.let_cur and n.COD_ENS ='" & CodModalidad & "' AND n.NUM_GRA ='" & Cmb_Grado & "'"
a = a & "AND n.LETRA_CURSO ='" & Cmb_Let_Cur & "' AND n.COD_SEC ='" & Codsec & "' AND n.COD_ASI ='" & Codasi & "' "
a = a & "AND n.NUM_PER ='0' and n.agno = '" & cmb_agno.Text & "' and c.activo_alu = 'si'"
rs.Open a, conn, adOpenKeyset

If rs("totalce") > 0 Then 'pregunta por primer semestre todos con nota en ramo
    periodoc = 0 'trabajamos sobre el primer semestre
    rs.Close
    
    a = "SELECT count(nota) as totalno FROM notas n, alu_x_cur c where n.ci_alu = c.ci_alu and n.agno = c.agno "
    a = a & "and n.letra_curso = c.let_cur and n.COD_ENS ='" & CodModalidad & "' AND n.NUM_GRA ='" & Cmb_Grado & "'"
    a = a & "AND n.LETRA_CURSO ='" & Cmb_Let_Cur & "' AND n.COD_SEC ='" & Codsec & "' AND n.COD_ASI ='" & Codasi & "' "
    a = a & "AND n.NUM_PER ='0' and n.agno = '" & cmb_agno.Text & "' and c.activo_alu = 'si' and n.nota = ''"
    rs.Open a, conn, adOpenKeyset

    If rs("totalno") > 0 Then 'pregunta por primer semestre todos con nota en ramo
        MsgBox "Todos los estudiantes deben tener a lo menos 1 nota"
        Exit Sub
    Else 'pregunta por segundo semestre todos con nota en ramo
        'cierra ramo dejando cerrado en 1
        rs.Close
        
        a = "update notas n, alu_x_cur c set cerrado = 1 where n.ci_alu = c.ci_alu and n.agno = c.agno "
        a = a & "and n.letra_curso = c.let_cur and n.COD_ENS ='" & CodModalidad & "' AND n.NUM_GRA ='" & Cmb_Grado & "'"
        a = a & "AND n.LETRA_CURSO ='" & Cmb_Let_Cur & "' AND n.COD_SEC ='" & Codsec & "' AND n.COD_ASI ='" & Codasi & "' "
        a = a & "AND n.NUM_PER ='0' and n.agno = '" & cmb_agno.Text & "' and c.activo_alu = 'si'"
        rs.Open a, conn, adOpenKeyset
    End If

Else 'pregunta otro semestre
    periodoc = 1 'trabajamos sobre el segundo semestre
    rs.Close
    
    a = "SELECT count(cerrado) as totalce FROM notas n, alu_x_cur c where n.ci_alu = c.ci_alu and n.agno = c.agno "
    a = a & "and n.letra_curso = c.let_cur and n.COD_ENS ='" & CodModalidad & "' AND n.NUM_GRA ='" & Cmb_Grado & "'"
    a = a & "AND n.LETRA_CURSO ='" & Cmb_Let_Cur & "' AND n.COD_SEC ='" & Codsec & "' AND n.COD_ASI ='" & Codasi & "' "
    a = a & "AND n.NUM_PER ='1' and n.agno = '" & cmb_agno.Text & "' and c.activo_alu = 'si'"
    rs.Open a, conn, adOpenKeyset
    
    If rs("totalno") > 0 Then 'pregunta por primer semestre todos con nota en ramo
        MsgBox "Todos los estudiantes deben tener a lo menos 1 nota"
        Exit Sub
    Else 'pregunta por segundo semestre todos con nota en ramo
        'cierra ramo dejando cerrado en 1
        rs.Close
        
        a = "update notas n, alu_x_cur c set cerrado = 1 where n.ci_alu = c.ci_alu and n.agno = c.agno "
        a = a & "and n.letra_curso = c.let_cur and n.COD_ENS ='" & CodModalidad & "' AND n.NUM_GRA ='" & Cmb_Grado & "'"
        a = a & "AND n.LETRA_CURSO ='" & Cmb_Let_Cur & "' AND n.COD_SEC ='" & Codsec & "' AND n.COD_ASI ='" & Codasi & "' "
        a = a & "AND n.NUM_PER ='0' and n.agno = '" & cmb_agno.Text & "' and c.activo_alu = 'si'"
        rs.Open a, conn, adOpenKeyset
    End If
End If

End Sub

Private Sub cmb_agno_Click()
    LimpiaNotas
    If Cmb_Let_Cur.Text <> "" Then Cmb_Grado_Click
End Sub

Private Sub Cmb_Asi_Click()
Dim RutProfe As String
Dim objRS As ADODB.Recordset
Set objRS = New ADODB.Recordset
Dim CelIniEva, CelIniPer As Integer
Dim I As Long
Dim log As Boolean

Btn_Ace_Not.Enabled = False
'******** Limpia grgilla
Call LimpiaNotas
'******** FIN Limpia grgilla

'se agrega 16-05-2022
CargaAlumnosAsignatura

Codasi = DevuelveCampo("subsectores", "", "cod_asi", "nom_asi = '" & Me.Cmb_Asi & "' and cod_ens ='" & CodModalidad & "' and num_gra ='" & Me.Cmb_Grado & "' and cod_sec ='" & Codsec & "'", conn)
pn = DevuelveCampo("subsectores", "", "pn", "nom_asi = '" & Me.Cmb_Asi & "' and cod_ens ='" & CodModalidad & "' and num_gra ='" & Me.Cmb_Grado & "' and cod_sec ='" & Codsec & "'", conn)

If Cmb_Asi.Text <> "" Then
Form_Progress.Label1.Caption = "Cargando ..."
Form_Progress.Show
Form_Progress.ProgBar1.MaxValue = Me.MSFlexGrid1.Rows - 1
I = 0
notas = ""
    
    cole = DevuelveCampo("cfg", "", "valor", "param='CODBC'", conn)
    MSFlexGrid1.ColWidth(31) = 450
    
    For R = 1 To Me.MSFlexGrid1.Rows - 1
        MSFlexGrid1.Row = R
        If Not (cole = "HUE001" Or cole = "ALT001" Or cole = "SNB001") Then
            MSFlexGrid1.Col = 12
            If pn = 1 Then
                Me.MSFlexGrid1.CellBackColor = &HC0FFC0
            Else
                Me.MSFlexGrid1.CellBackColor = &HFFFFFF
            End If
            MSFlexGrid1.Col = 28
            If pn = 1 Then
                Me.MSFlexGrid1.CellBackColor = &HC0FFC0
            Else
                Me.MSFlexGrid1.CellBackColor = &HFFFFFF
            End If
        End If
        If cole = "HUE001" Or cole = "ALT001" Then
            If cole = "HUE001" Then
                '''''Prueba Examen Huechuraba
''''''''                If Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 4 And CodModalidad = "110" And cmb_agno.Text >= 2014 Then
''''''''                      MSFlexGrid1.ColWidth(31) = 0
''''''''                End If
        
                MSFlexGrid1.Col = 12
                If pn = 1 Then
                    Me.MSFlexGrid1.CellBackColor = &HC0FFC0
                Else
                    Me.MSFlexGrid1.CellBackColor = &HFFFFFF
                End If
            
                If Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 4 And CodModalidad = "110" And cmb_agno.Text >= 2014 Then
            
                    MSFlexGrid1.Col = 29
                    If pn = 1 Then
                        Me.MSFlexGrid1.CellBackColor = &HC0FFC0
                    Else
                        Me.MSFlexGrid1.CellBackColor = &HFFFFFF
                    End If
            
                End If
            Else
                        '''''Prueba Examen Macul
                If Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 6 And CodModalidad = "110" And cmb_agno.Text >= 2014 Then
                      MSFlexGrid1.ColWidth(31) = 0
                End If
        
                MSFlexGrid1.Col = 12
                If pn = 1 Then
                    Me.MSFlexGrid1.CellBackColor = &HC0FFC0
                Else
                    Me.MSFlexGrid1.CellBackColor = &HFFFFFF
                End If
            
                If Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 6 And CodModalidad = "110" And cmb_agno.Text >= 2014 Then
            
                    MSFlexGrid1.Col = 29
                    If pn = 1 Then
                        Me.MSFlexGrid1.CellBackColor = &HC0FFC0
                    Else
                        Me.MSFlexGrid1.CellBackColor = &HFFFFFF
                    End If
            
                End If
            End If
        End If
        
        If cole = "SNB001" Then
                '''''Prueba Examen San Bernardo
'''''                If Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 6 And CodModalidad = "110" And cmb_agno.Text >= 2014 Then
'''''                      MSFlexGrid1.ColWidth(15) = 0
'''''                      MSFlexGrid1.ColWidth(16) = 0
'''''                      MSFlexGrid1.ColWidth(14) = 0
''''''                      MSFlexGrid1.ColWidth(32) = 0
'''''
'''''                      MSFlexGrid1.ColWidth(17) = 0
'''''                      MSFlexGrid1.ColWidth(18) = 0
'''''                      'MSFlexGrid1.ColWidth(36) = 0
'''''                     ' MSFlexGrid1.ColWidth(16) = 0
'''''                      MSFlexGrid1.ColWidth(36) = 0
'''''                      MSFlexGrid1.ColWidth(37) = 0
'''''                      MSFlexGrid1.ColWidth(38) = 450
'''''                      MSFlexGrid1.ColWidth(39) = 450
'''''                      MSFlexGrid1.ColWidth(40) = 0
'''''                      MSFlexGrid1.ColWidth(41) = 0
'''''                      MSFlexGrid1.ColWidth(34) = 0
'''''                      MSFlexGrid1.ColWidth(35) = 0
'''''                      MSFlexGrid1.ColWidth(33) = 0
'''''                Else
                      
                      MSFlexGrid1.ColWidth(33) = 450
                      MSFlexGrid1.ColWidth(34) = 0
                      MSFlexGrid1.ColWidth(35) = 450
                      MSFlexGrid1.ColWidth(36) = 0
                      MSFlexGrid1.ColWidth(37) = 0
                      MSFlexGrid1.ColWidth(38) = 450
                      MSFlexGrid1.ColWidth(39) = 450
                      MSFlexGrid1.ColWidth(40) = 0
                      MSFlexGrid1.ColWidth(41) = 0
                      MSFlexGrid1.ColWidth(15) = 0
                      
                    
                      MSFlexGrid1.ColWidth(14) = 450
                      'MSFlexGrid1.ColWidth(15) = 450
                      
                      MSFlexGrid1.ColWidth(16) = 450
                      
                     
                      'MSFlexGrid1.ColWidth(34) = 450
                      
                      
                      
'                      MSFlexGrid1.ColWidth(15) = 450
'                      MSFlexGrid1.ColWidth(33) = 450
'                      MSFlexGrid1.ColWidth(14) = 450
'                      MSFlexGrid1.ColWidth(32) = 450
'''''                End If
        
                MSFlexGrid1.Col = 12
                If pn = 1 Then
                    Me.MSFlexGrid1.CellBackColor = &HC0FFC0
                Else
                    Me.MSFlexGrid1.CellBackColor = &HFFFFFF
                End If
            
                '''If Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 4 And CodModalidad = "110" And cmb_agno.Text >= 2014 Then
            
                    MSFlexGrid1.Col = 29
                    If pn = 1 Then
                        Me.MSFlexGrid1.CellBackColor = &HC0FFC0
                    Else
                        Me.MSFlexGrid1.CellBackColor = &HFFFFFF
                    End If
            
                '''End If
            
            
        End If
        
    Next R
    
  
    

    For R = 1 To Me.MSFlexGrid1.Rows - 1
        'MSFlexGrid1.TextMatrix(R, 12) = Me.MSFlexGrid1.CellBackColor = &HC0FFC0
    
        '**** Extrtae el registro con notas
            Rut_ALu = Left(LTrim(MSFlexGrid1.TextMatrix(R, 0)), 10)
            Set rs = New ADODB.Recordset
            rs.CursorLocation = adUseClient
        '**** FIN Extrtae el registro con notas
        For p = 0 To PE - 1
            CelIniPer = PERIODOS(p, 4)
            For e = 0 To EV - 1
                a = "select nota from notas where CI_ALU ='" & Rut_ALu & "' AND COD_ENS ='" & CodModalidad & "' AND NUM_GRA ='" & Cmb_Grado & "' AND LETRA_CURSO ='" & Cmb_Let_Cur & "' AND COD_SEC ='" & Codsec & "' AND COD_ASI ='" & Codasi & "' AND NUM_PER ='" & p & "' AND COD_EVA ='" & e & "' and agno = " & cmb_agno.Text
                rs.Open a, conn, adOpenKeyset
                
'                ERROR AL PONER NOTAS!!
'                If rs("nota") <> "" Then
'                    faltante = 1
'                End If
' NOTA P
                
                If rs.RecordCount > 0 Then
                    If val(Trim(rs(0))) > 0 Then
                    notas = rs(0)
                    n = 1
                    CelIniEva = PERIODOS(p, 4) - 2 + EVALUACIONES(e, 6)
                        For I = CelIniEva To CelIniEva + EVALUACIONES(e, 3) - 1
                            If Trim(Mid(notas, n, 2)) <> "" Then
                                nota1 = val(Mid(notas, n, 1)) & "," & val(Mid(notas, n + 1, 1))
                                
                                '************ Rojos o Azules ***************
                                MSFlexGrid1.Row = R
                                MSFlexGrid1.Col = I
                                If (nota1) < 4 Then
                                    MSFlexGrid1.CellForeColor = &HFF&
                                Else
                                    MSFlexGrid1.CellForeColor = &HFF0000
                                End If
                                '************ FIN Rojos o Azules ***************
                                
                                If CDbl(nota1) > 7 Then
                                    nota1 = "7,0"
                                End If
                                If val(nota1) < 1 Then
                                    If Mid(notas, n, 2) = " P" Or Mid(notas, n, 2) = "P " Or Mid(notas, n, 2) = "PP" Then
                                      nota1 = Mid(notas, n, 2)
                                    Else
                                      nota1 = ""
                                    End If
                                End If
                                MSFlexGrid1.TextMatrix(R, I) = nota1
                            End If
                            n = n + 2
                            nota1 = ""
                        Next I
                         Call CalcProm(I, R)
                    End If ' viene del RS
                End If ' viene del recordcount
                rs.Close
            Next e
             If notas <> "" Then Call CalcPromPer(I, R)
        Next p

        If notas <> "" Then
            Call CalcPromProm(I, R)
        End If
        If UsoDeExamen Then
        
         a = "select NE from promedios where CI_ALU ='" & Rut_ALu & "' AND COD_ENS ='" & CodModalidad & "' AND NUM_GRA ='" & Cmb_Grado & "' AND LETRA_CURSO ='" & Cmb_Let_Cur & "' AND COD_SEC ='" & Codsec & "' AND COD_ASI ='" & Codasi & "' and agno = " & cmb_agno.Text
         rs.Open a, conn, adOpenKeyset
         If rs.RecordCount > 0 Then
            If Not IsNull(rs(0)) Then
                If val(Trim(rs(0))) > 0 Then
                    NE = rs(0)
                    '************ Prpmedios Rojos o Azules ***************
                    Me.MSFlexGrid1.Col = MSFlexGrid1.Cols - 2
                    Me.MSFlexGrid1.Row = R
                    MSFlexGrid1.CellBackColor = &H80000004
                    If val(NE) < 4 Then
                        MSFlexGrid1.CellForeColor = &HFF&
                    Else
                        MSFlexGrid1.CellForeColor = &HFF0000
                    End If
                    '************ FIN Promedios Rojos o Azules ***************
                    If Len(Trim(NE)) < 3 And val(NE) > 0 Then
                        MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 2) = Format(CDbl(NE), "#.0")
                    Else
                        Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 2) = NE
                    End If
                    Call CalcPromFin(MSFlexGrid1.Cols - 2, R)
                End If
            End If
         End If
        rs.Close
        'FIN Trae nota de examen
        Else
            Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 2) = "EX"
            Call CalcPromFin(MSFlexGrid1.Cols - 2, R)
        End If
    Form_Progress.ProgBar1.ShowProgress R
    Form_Progress.SetFocus
    DoEvents
    Next R
    
    Form_Progress.ProgBar1.ShowProgress 0
    Unload Form_Progress
    Form_Notas.Enabled = True
    Btn_Ace_Not.Enabled = True
    MSFlexGrid1.Enabled = False

    If cmb_agno.Text = Anno Then
        MSFlexGrid1.Enabled = True
    End If

    Me.Cmd_Per.ListIndex = 0
    Me.cerrar_asignatura.Enabled = True
''poner boton cerrar asignatura
'    'consulta campo base datos
'    a = "select cerrado from notas where CI_ALU ='" & Rut_ALu & "' AND COD_ENS ='" & CodModalidad & "' AND NUM_GRA ='" & Cmb_Grado & "' AND LETRA_CURSO ='" & Cmb_Let_Cur & "' AND COD_SEC ='" & Codsec & "' AND COD_ASI ='" & Codasi & "' AND NUM_PER ='0' and agno = " & cmb_agno.Text
'    rs.Open a, conn, adOpenKeyset
'
'    If rs("cerrado") = 0 Then
'        Per = 0
'        Me.cerrar_asignatura.Enabled = True
'    Else
'        If rs("cerrado") = 1 Then
'            rs.Close
'            a = "select cerrado from notas where CI_ALU ='" & Rut_ALu & "' AND COD_ENS ='" & CodModalidad & "' AND NUM_GRA ='" & Cmb_Grado & "' AND LETRA_CURSO ='" & Cmb_Let_Cur & "' AND COD_SEC ='" & Codsec & "' AND COD_ASI ='" & Codasi & "' AND NUM_PER ='1' and agno = " & cmb_agno.Text
'            rs.Open a, conn, adOpenKeyset
'
'            If rs("cerrado") = 0 Then
'                Per = 1
'                Me.cerrar_asignatura.Enabled = True
'            End If
'        End If
'    End If
'
'    rs.Close

End If
'********
' Log de nnotas

var_sql = "SELECT rut_prof FROM cursos" _
        & " where cod_ens = '" & CodModalidad & "' and num_gra = " & Cmb_Grado _
        & " and let_cur = '" & Cmb_Let_Cur & "' and agno = " & cmb_agno.Text
objRS.Open var_sql, conn
RutProfe = objRS("rut_prof")
objRS.Close

MSFlexGrid1.Enabled = True

     Select Case CodModalidad
        Case "996"
            grado = "PK"
        Case "010"
            grado = "K"
        Case "110"
            grado = "EB"
        Case "310"
            grado = "EM"
    End Select
    curso = CStr(Cmb_Grado) & "º " & Cmb_Let_Cur & " " & grado
    accion_realizada = "El usuario a consultado las notas del subsector " & Cmb_Asi.Text _
                     & " que pertenece al sector " & Cmb_Sector.Text _
                     & " para el curso " & curso & " del año " _
                     & cmb_agno.Text
    log = Inserta_Vitacora_Profesores(Trim(RutProfe), "Consulta de notas", Trim(accion_realizada))

End Sub
Private Sub Cmb_Asi_LostFocus()
If Cmb_Asi.Text <> "" Then Me.MSFlexGrid1.Enabled = True
End Sub
Private Sub Cmb_Cod_ens_Click()
If Len(Me.Cmb_Cod_ens) <> 0 Then
 CodModalidad = DevuelveCampo("codigos_ensenanza", "", "cod_ens", "des_ens = '" & Me.Cmb_Cod_ens.Text & "'", conn)
 Cmb_Grado.Enabled = True
 Cmb_Grado.Clear

If CodPerfil = "#UTP#" Or CodPerfil = "#ADM#" Or CodPerfil = "#GES03#" Then
    If Len(Me.Cmb_Cod_ens.Text) <> 0 Then
        nivel = DevuelveCampo("codigos_ensenanza", "des_ens", "num_gra_max", "des_ens = " & "'" & Me.Cmb_Cod_ens.Text & "'", conn)
        Me.Cmb_Grado.Enabled = True
        Me.Cmb_Grado.Clear
    For t = 1 To val(nivel)
        Me.Cmb_Grado.AddItem t
    Next t
 End If

Else
    'mahu mira aca ###############
    Call LlenaCombo(Form_Notas.Cmb_Grado, "asi_x_cur_prof AP", "DISTINCT AP.NUM_GRA", "where AP.cod_ens = '" & CodModalidad & "' and AP.rut_prof ='" & RutUsuario & "' and ap.agno = " & cmb_agno.Text, conn)
End If

 
 Cmb_Let_Cur.Clear

End If

'******** Limpia grilla
Call LimpiaNotas
'******** FIN Limpia grgilla

End Sub

Private Sub Cmb_Grado_Click()
If cmb_agno.Text = "" Then MsgBox "Debe ingresar el año a procesar": Cmb_Grado.Text = "": Exit Sub
Ngrado = Me.Cmb_Grado.Text
Cmb_Let_Cur.Clear

If CodPerfil = "#UTP#" Or CodPerfil = "#ADM#" Or CodPerfil = "#GES03#" Then
    Call LlenaCombo(Me.Cmb_Let_Cur, "cursos", "let_cur", "where cod_ens = '" & CodModalidad & "' and num_gra = '" & Ngrado & "' and agno = " & cmb_agno.Text, conn)
Else
    Call LlenaCombo(Cmb_Let_Cur, "asi_x_cur_prof ap", "distinct ap.let_cur", "where ap.cod_ens = '" & CodModalidad & "' and ap.num_gra = '" & Ngrado & "' and ap.rut_prof ='" & RutUsuario & "' and agno = " & cmb_agno.Text, conn)
End If

Me.Cmb_Let_Cur.Enabled = True
Cmb_Asi.Clear
If Me.Cmb_Sector.Text <> "" Then
    Call LlenaCombo(Form_Notas.Cmb_Asi, "subsectores ss, sectores s", "NOM_ASI", "where ss.cod_sec = s.cod_sec and ss.cod_ens ='" & CodModalidad & "' and num_gra ='" & Ngrado & "' and ss.cod_sec ='" & Codsec & "'", conn)
End If

'******** Limpia grgilla
Call LimpiaNotas
'******** FIN Limpia grgilla
End Sub

Private Sub Cmb_let_cur_Click()
Dim NEva As Integer
Dim celPnivel() As Long 'Dim celPnivel() As Long
Dim numCElpnivel As Integer
MSFlexGrid1.Enabled = False

If cmb_agno.Text = "" Then MsgBox "Debe seleccionar Año escolar": Exit Sub
'* ## identifica celdas para la prueba de nivel (ultima celda del ultimo tipo de evALUACION) ##
'* ## EXTRATADO DE LA FUNCION CALPROMPROM

'Se trae el Nº de evaluaciones para replicar la columna de la PN
NEva = val(DevuelveCampo("evaluaciones", "", "count(cod_eva)", "cod_eva <> ''", conn))
NEva = IIf(NEva = 0, 1, NEva) ' Si no hay evaluaciones ingresadas queda como 1 para no afectar la multiplicación de más abajo

ReDim celPnivel(PE * NEva)  ' Indica Tipos de Evaluaciones por Periodo

numCElpnivel = 0
For I = 1 To Me.MSFlexGrid1.Cols - 1
    If Me.MSFlexGrid1.TextMatrix(0, I) = "X" And ((InStr(Me.MSFlexGrid1.TextMatrix(0, I - 1), "APR") <> 0) Or (InStr(Me.MSFlexGrid1.TextMatrix(0, I - 1), "PRO") <> 0)) Then
        If cole = "HUE001" Or cole = "ALT001" Or cole = "SNB001" Then
            celPnivel(numCElpnivel) = I - 2
            numCElpnivel = numCElpnivel + 1
        Else
            celPnivel(numCElpnivel) = I - 1
            numCElpnivel = numCElpnivel + 1
        End If
    End If
    
Next I

alumno = 0
a = "select p.ape_pat_alu, p.ape_mat_alu, p.nom_alu, p.ci_alu from postulantes p, alu_x_cur a, matriculas m where p.ci_alu = a.ci_alu and a.ci_alu=m.ci_alu and a.fec_mat=m.fec_mat and a.activo_alu='SI' and a.cod_ens = '" & CodModalidad & " ' and a.num_gra = '" & Ngrado & "' and a.let_cur ='" & Me.Cmb_Let_Cur & "' and a.agno = " & cmb_agno.Text & " and a.agno=m.anno_mat ORDER BY orden_libro,a.fec_ing_curso, m.num_mat, p.ape_pat_alu, p.ape_mat_alu, p.nom_alu"
    MSFlexGrid1.Enabled = True
    Set rs = New ADODB.Recordset
    'rs.CursorLocation = adUseClientc

rs.Open a, conn
Me.MSFlexGrid1.Rows = 1
If rs.RecordCount > 0 Then

For alumno = 1 To rs.RecordCount
    Me.MSFlexGrid1.AddItem ""
    For I = 0 To (PE * NEva) - 1 Step 2
        If cole = "HUE001" Or cole = "ALT001" Then
            Me.MSFlexGrid1.Col = celPnivel(I) + 1
            Me.MSFlexGrid1.Row = alumno
            Me.MSFlexGrid1.CellBackColor = &HC0FFFF
            Me.MSFlexGrid1.Col = celPnivel(I) - 1
            Me.MSFlexGrid1.Row = alumno
            'Me.MSFlexGrid1.CellBackColor = &HC0FFC0
        Else
        ' modificando prueba nivel
            If cole = "SNB001" Then
                Me.MSFlexGrid1.Col = celPnivel(I) - 1 '+ 1 -1
                Me.MSFlexGrid1.Row = alumno
                Me.MSFlexGrid1.CellBackColor = &HC0FFFF
                Me.MSFlexGrid1.Col = celPnivel(I) + 1
                Me.MSFlexGrid1.Row = alumno
                Me.MSFlexGrid1.CellBackColor = &HFFFF80
            Else
                Me.MSFlexGrid1.Col = celPnivel(I)
                Me.MSFlexGrid1.Row = alumno
'                Me.MSFlexGrid1.CellBackColor = &HC0FFFF
                Me.MSFlexGrid1.Col = celPnivel(I) - 1
                Me.MSFlexGrid1.Row = alumno
'               Me.MSFlexGrid1.CellBackColor = &HC0FFC0
            End If
        End If
    Next I
    For I = 1 To (PE * NEva) - 1 Step 2
        If cole = "HUE001" Or cole = "ALT001" Then
            Me.MSFlexGrid1.Col = celPnivel(I) - 1
            Me.MSFlexGrid1.Row = alumno
            'Me.MSFlexGrid1.CellBackColor = &HC0FFC0
        
            Me.MSFlexGrid1.Col = celPnivel(I) + 1    ' + 2
            Me.MSFlexGrid1.Row = alumno
            ''''''''''''''''''Me.MSFlexGrid1.CellBackColor = &HC0FFFF ''''' Huechuraba Examen
        Else
        'modificando prueba de nivel
            If cole = "SNB001" Then
                Me.MSFlexGrid1.Col = celPnivel(I) + 1
                Me.MSFlexGrid1.Row = alumno
                Me.MSFlexGrid1.CellBackColor = &HFFFF80
        
                Me.MSFlexGrid1.Col = celPnivel(I) - 1 ' + 1    ' + 2
                Me.MSFlexGrid1.Row = alumno
                Me.MSFlexGrid1.CellBackColor = &HC0FFFF
            Else
                Me.MSFlexGrid1.Col = celPnivel(I) - 1
                Me.MSFlexGrid1.Row = alumno
'               Me.MSFlexGrid1.CellBackColor = &HC0FFC0
            
                Me.MSFlexGrid1.Col = celPnivel(I)
                Me.MSFlexGrid1.Row = alumno
'               Me.MSFlexGrid1.CellBackColor = &HC0FFFF
            End If
        End If
    Next I
    
        If cole = "FLO001" And cmb_agno.Text = 2021 Then
            For z = 34 To 45
                Me.MSFlexGrid1.Col = z
                Me.MSFlexGrid1.Row = alumno
                Me.MSFlexGrid1.CellBackColor = &HC0FFFF
            Next z
        End If
    
    MSFlexGrid1.Col = 0
    MSFlexGrid1.Row = alumno
    MSFlexGrid1.CellAlignment = 1

    MSFlexGrid1.TextMatrix(alumno, 0) = rs(3) & " | " & Format(alumno, "00") & " | " & rs(0) & " " & rs(1) & " " & rs(2)
    rs.MoveNext
Next alumno
End If
rs.Close


If Me.Cmb_Let_Cur <> "" Then
    Cmb_Sector.ComboItems.Clear
    Cmb_Asi.Clear
        
    If CodPerfil = "#PJE#" Then
    rs.Open "SELECT DISTINCT s.nom_sec, ap.cod_sec" _
          & " FROM sectores s, asi_x_cur_prof ap, cursos c" _
          & " WHERE S.COD_SEC = AP.COD_SEC AND AP.cod_ens = '" & CodModalidad _
          & "' and AP.num_gra = '" & Me.Cmb_Grado & "' and AP.let_cur ='" _
          & Me.Cmb_Let_Cur & "' and ap.agno = " & cmb_agno.Text _
          & " and ap.agno = c.agno and ap.num_gra = c.num_gra and" _
          & " ap.let_cur = c.let_cur and ap.cod_ens = c.cod_ens and" _
          & " c.agno = " & cmb_agno.Text & " and c.rut_prof = '" & RutUsuario & "' union " _
          & "SELECT DISTINCT s.NOM_SEC, ap.COD_SEC FROM SECTORES S, asi_x_cur_prof AP WHERE S.COD_SEC = AP.COD_SEC AND AP.cod_ens = '" & CodModalidad & "' and AP.num_gra = '" & Me.Cmb_Grado & "' and AP.let_cur ='" & Me.Cmb_Let_Cur & "' and AP.rut_prof ='" & RutUsuario & "' and ap.agno = " & cmb_agno.Text, conn
    Else
        If CodPerfil = "#UTP#" Or CodPerfil = "#ADM#" Or CodPerfil = "#GES03#" Then
            rs.Open "SELECT DISTINCT s.nom_sec, ap.cod_sec FROM sectores s, asi_x_cur_prof ap,subsectores sub" _
            & " WHERE S.COD_SEC = sub.COD_SEC AND ap.cod_ens = sub.cod_ens AND ap.num_gra = sub.num_gra AND ap.cod_sec = sub.cod_sec AND ap.cod_asi = sub.cod_asi AND AP.cod_ens = '" & CodModalidad & "' and AP.num_gra = '" & Me.Cmb_Grado & "' and AP.let_cur ='" & Me.Cmb_Let_Cur & "' and ap.agno = " & cmb_agno.Text, conn
        Else
            rs.Open "SELECT DISTINCT s.NOM_SEC, ap.COD_SEC FROM SECTORES S, asi_x_cur_prof AP WHERE S.COD_SEC = AP.COD_SEC AND AP.cod_ens = '" & CodModalidad & "' and AP.num_gra = '" & Me.Cmb_Grado & "' and AP.let_cur ='" & Me.Cmb_Let_Cur & "' and AP.rut_prof ='" & RutUsuario & "' and ap.agno = " & cmb_agno.Text, conn
        End If
    End If
    
    If rs.RecordCount > 0 Then
        rs.MoveFirst
        For I = 1 To rs.RecordCount
            Cmb_Sector.ComboItems.Add I, "S" & rs!Cod_Sec, rs!nom_sec
            rs.MoveNext
        Next I
    End If
End If

'******** Limpia grilla
Call LimpiaNotas
'******** FIN Limpia grilla
End Sub

Private Sub Cmb_Sector_Change()
    Cmb_Asi.Clear
End Sub

Private Sub Cmb_Sector_Click()
'******** Limpia grgilla
Call LimpiaNotas
'******** FIN Limpia grgilla
If Len(Me.Cmb_Cod_ens) <> 0 Then
 Codsec = Trim(Right(Cmb_Sector.SelectedItem.Key, Len(Cmb_Sector.SelectedItem.Key) - 1))
 Cmb_Asi.Clear
 
 If CodPerfil = "#PJE#" Then
    Call LlenaCombo(Form_Notas.Cmb_Asi, "subsectores ss, asi_x_cur_prof ap, cursos c", "NOM_ASI", "where ss.cod_sec = ap.cod_sec and ss.cod_asi = ap.cod_asi and ss.cod_ens = ap.cod_ens and ss.num_gra = ap.num_gra and ap.cod_ens ='" & CodModalidad & "' and ap.num_gra ='" & Me.Cmb_Grado & "' and ap.cod_sec ='" & Codsec & "' and ap.let_cur ='" & Me.Cmb_Let_Cur & "' and ap.agno = " & cmb_agno.Text & " and ap.agno = c.agno and ap.num_gra = c.num_gra and ap.let_cur = c.let_cur and ap.cod_ens = c.cod_ens and c.agno = " & cmb_agno.Text & " and c.rut_prof = '" & RutUsuario & "'", conn)
    condicion = "agno = " & cmb_agno.Text & " AND cod_ens = '" & CodModalidad & "' AND num_gra = " & Me.Cmb_Grado & " AND let_cur = '" & Me.Cmb_Let_Cur & "' and rut_prof = '" & RutUsuario & "'"
    cole = DevuelveCampo("cursos", "", "rut_prof", "" & condicion & "", conn)
    If Len(cole) > 0 Then
    Else
        Call LlenaCombo(Form_Notas.Cmb_Asi, "subsectores ss, asi_x_cur_prof ap", "NOM_ASI", "where ss.cod_sec = ap.cod_sec and ss.cod_asi = ap.cod_asi and ss.cod_ens = ap.cod_ens and ss.num_gra = ap.num_gra and ap.cod_ens ='" & CodModalidad & "' and ap.num_gra ='" & Me.Cmb_Grado & "' and ap.cod_sec ='" & Codsec & "' and ap.let_cur ='" & Me.Cmb_Let_Cur & "' and ap.rut_prof ='" & RutUsuario & "' and ap.agno = " & cmb_agno.Text, conn)
    End If
 Else
 If CodPerfil = "#UTP#" Or CodPerfil = "#ADM#" Or CodPerfil = "#GES03#" Then
    Call LlenaCombo(Form_Notas.Cmb_Asi, "subsectores ss, asi_x_cur_prof ap", "NOM_ASI", "where ss.cod_sec = ap.cod_sec and ss.cod_asi = ap.cod_asi and ss.cod_ens = ap.cod_ens and ss.num_gra = ap.num_gra and ap.cod_ens ='" & CodModalidad & "' and ap.num_gra ='" & Me.Cmb_Grado & "' and ap.cod_sec ='" & Codsec & "' and ap.let_cur ='" & Me.Cmb_Let_Cur & "' and ap.agno = " & cmb_agno.Text, conn)
 Else
    Call LlenaCombo(Form_Notas.Cmb_Asi, "subsectores ss, asi_x_cur_prof ap", "NOM_ASI", "where ss.cod_sec = ap.cod_sec and ss.cod_asi = ap.cod_asi and ss.cod_ens = ap.cod_ens and ss.num_gra = ap.num_gra and ap.cod_ens ='" & CodModalidad & "' and ap.num_gra ='" & Me.Cmb_Grado & "' and ap.cod_sec ='" & Codsec & "' and ap.let_cur ='" & Me.Cmb_Let_Cur & "' and ap.rut_prof ='" & RutUsuario & "' and ap.agno = " & cmb_agno.Text, conn)
 End If
 End If
 
 Cmb_Asi.Enabled = True
End If

End Sub


Private Sub cmd_exportar_Click()

    Dim ApExcel As Variant
    Set ApExcel = CreateObject("Excel.application")
    ApExcel.Workbooks.Add
          
    For I = 1 To Me.MSFlexGrid1.Cols - 1
        ApExcel.Cells(1, I + 1).Font.Size = 12
        ApExcel.Cells(1, I + 1).Font.Bold = True
        ApExcel.Cells(1, I + 1).Formula = Me.MSFlexGrid1.TextMatrix(0, I)
    Next I
    ProgBar1.MinValue = 1
    ProgBar1.MaxValue = MSFlexGrid1.Rows
    ProgBar1.ShowProgress ProgBar1.MinValue
    For I = 1 To MSFlexGrid1.Rows - 1

         For j = 0 To MSFlexGrid1.Cols - 1
            ApExcel.Cells((I + 1), (j + 1)).Formula = IIf(MSFlexGrid1.TextMatrix(I, j) = "Null", "", MSFlexGrid1.TextMatrix(I, j))
        
         ProgBar1.ShowProgress I
        Next j
    Next I
    ProgBar1.ShowProgress ProgBar1.MaxValue
            
    ApExcel.Visible = True
    'ApExcel.Range("a1:x1").Borders.color = RGB(255, 0, 0)
    Set ApExcel = Nothing

End Sub

Private Sub Cmd_Per_Click()
 
Min_max_gen = PromMinMaxGen("PROMEDIOS", "P" & Left(Me.Cmd_Per.Text, 1), "cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "'", conn)
Me.LbPromL.Caption = Left(Min_max_gen, 3)
Me.LbPromH.Caption = Mid(Min_max_gen, 4, 3)
Me.LbPromG.Caption = Right(Min_max_gen, 3)


End Sub

Private Sub Command1_Click()
On Error GoTo controler
GuardaPromedios = True
nmax = CuentaReg("asi_x_cur_prof", "agno = " & cmb_agno.Text, conn)
  With ProgBar1
    .MaxValue = nmax
    .Richtung = mDir
    .ProgressStyle = mStyle
    .ShowProzent = 1
    .Zusatztext = "%"
    .ShowProgress 0
  End With

n = 0
For a = 0 To Me.Cmb_Cod_ens.ListCount - 1
    Cmb_Cod_ens.ListIndex = a
    For b = 0 To Me.Cmb_Grado.ListCount - 1
        Cmb_Grado.ListIndex = b
        For c = 0 To Me.Cmb_Let_Cur.ListCount - 1
            Cmb_Let_Cur.ListIndex = c
            For d = 0 To Me.Cmb_Sector.ComboItems.count - 1
                Cmb_Sector.SelectedItem.Index = d
                For e = 0 To Me.Cmb_Asi.ListCount - 1
                    Cmb_Asi.ListIndex = e
                    n = n + 1
                    ProgBar1.ShowProgress n
                Next e
            Next d
        Next c
    Next b
Next a
ProgBar1.ShowProgress 0

Exit Sub
controler:
GuardaPromedios = False
End Sub

Private Sub Command2_Click()
Dim CelIniEva, CelIniPer As Integer
Dim PromPer As Single
Dim PromBd As String

X = 0
xp = ""
For p = 0 To PE - 1
    CelIniPer = PERIODOS(p, 4)
    For e = 0 To EV - 1
        CelIniEva = PERIODOS(p, 4) - 2 + EVALUACIONES(e, 6)
        X = CelIniPer - 2 + EVALUACIONES(e, 6) + EVALUACIONES(e, 3) + 1
    Next e
    xp = xp & X & "-"
Next p
xp = Mid(xp, 1, Len(xp) - 1)
End Sub


Private Sub Command4_Click()

End Sub

Private Sub Command5_Click()
On Error GoTo controler
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient

MkDir "C:\Ranking"
nmax = CuentaReg("cursos", "", conn)

  With ProgBar1
    .MaxValue = nmax
    .Richtung = mDir
    .ProgressStyle = mStyle
    .ShowProzent = 1
    .Zusatztext = "%"
    .ShowProgress 0
  End With

n = 0
For a = 0 To Me.Cmb_Cod_ens.ListCount - 1
    Cmb_Cod_ens.ListIndex = a
    For b = 0 To Me.Cmb_Grado.ListCount - 1
        Cmb_Grado.ListIndex = b
        For c = 0 To Me.Cmb_Let_Cur.ListCount - 1
            Cmb_Let_Cur.ListIndex = c
                    n = n + 1
                    Sql = "select p.ape_pat_alu, p.ape_mat_alu, p.nom_alu, p.ci_alu, avg(p1) as rp from promedios pr, postulantes p where  p.ci_alu = pr.ci_alu and pr.cod_ens ='" & CodModalidad & "' and pr.num_gra ='" & Me.Cmb_Grado & "' and pr.letra_curso ='" & Me.Cmb_Let_Cur & "' group by p.ci_alu order by rp  desc"
                    rs.Open Sql, conn, adOpenKeyset
                    
                    If rs.RecordCount > 0 Then
                        '****************************************
                        'Crea txt
                        '****************************************
                        Open "C:\Ranking\" & CodModalidad & "-" & Me.Cmb_Grado & Me.Cmb_Let_Cur & ".txt" For Output As #1
                        While Not rs.EOF
                            prom_rank = Left(rs(4) & "0000", 7)
                            Print #1, prom_rank & " - " & rs(0) & " " & rs(1) & " " & rs(2) & " " & rs(3)
                            rs.MoveNext
                        Wend
                        Close #1
                    Else
                    End If
                    rs.Close
                    ProgBar1.ShowProgress n
        Next c
    Next b
Next a

ProgBar1.ShowProgress 0
Exit Sub
controler:
If Err.Number = 32755 Then
    R = MsgBox("¿Desea cancelar la emisión del Registro de Estudiantes de Chile?", vbQuestion + vbYesNo, "Mensaje de confirmación")
    If R = 7 Then
        Resume
    End If
End If
If Err.Number = 75 Then Resume Next
End Sub

Private Sub Form_Load()
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient

'Rescato colegio
cole = DevuelveCampo("cfg", "", "valor", "param='CODBC'", conn)

If cole = "HUE001" Or cole = "ALT001" Then
    Label11.Visible = True
End If

UsoDeExamen = False
GuardaPromedios = False

If CodPerfil = "#ADM#" Or CodPerfil = "#GES03#" Then
    Command1.Enabled = True
Else
    Command1.Enabled = False
End If

EntraColChange = False
CentrarForma Me
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
'********* Llena combos ****************
LlenaCombo cmb_agno, "matriculas", "anno_mat", " group by anno_mat order by anno_mat DESC", conn
cmb_agno.Text = Anno
RutUsuario = DevuelveCampo("usuarios", "", "rut_usu", "aut_nombre_usuario='" & Nom_usu_ses & "'", conn)
If CodPerfil = "#UTP#" Or CodPerfil = "#ADM#" Or CodPerfil = "#GES03#" Then
    Call LlenaCombo(Form_Notas.Cmb_Cod_ens, "codigos_ensenanza ce", "distinct ce.des_ens", "", conn)
Else
    Call LlenaCombo(Form_Notas.Cmb_Cod_ens, "codigos_ensenanza ce, asi_x_cur_prof ap", "distinct ce.des_ens", "where ce.cod_ens = ap.cod_ens and ap.rut_prof ='" & RutUsuario & "' and ap.agno = " & cmb_agno.Text, conn)
End If
Celda = 2
NumNotas = 0
'*********** PERIODOS *************
rs.Open "SELECT * FROM PERIODOS order by num_per", conn, adOpenKeyset
If rs.RecordCount > 0 Then

    PE = rs.RecordCount

    ReDim PERIODOS(PE, 5)
    Me.Cmd_Per.AddItem "Seleccione Periodo"
    CierrePer = ""
    For I = 0 To PE - 1
        CierrePer = CierrePer & Abs(CInt(Not (CBool(rs!per_cerrado))))
        PERIODOS(I, 0) = rs!Num_Per
        PERIODOS(I, 1) = rs!des_per
        PERIODOS(I, 2) = rs!FEC_INI_PER
        PERIODOS(I, 3) = rs!FEC_TER_PER
        Me.Cmd_Per.AddItem I + 1 & "-" & rs!des_per
        rs.MoveNext
    Next I
    Me.Cmd_Per.ListIndex = 0
Else
    PE = 0
End If
rs.Close

'*********** EVALUACIONES *************
rs.CursorLocation = adUseClient
rs.Open "SELECT * FROM EVALUACIONES ORDER BY COD_EVA", conn, adOpenKeyset
If rs.RecordCount > 0 Then
    EV = rs.RecordCount

    ReDim EVALUACIONES(EV, 7)
    For I = 0 To EV - 1
        EVALUACIONES(I, 0) = rs!cod_eva
        EVALUACIONES(I, 1) = rs!DES_EVA
        EVALUACIONES(I, 2) = rs!PORCENTAJE_EVA
        EVALUACIONES(I, 3) = rs!notas_eva
        EVALUACIONES(I, 4) = rs!COLOR_FONDO_EVA
        EVALUACIONES(I, 5) = rs!COLOR_TEXTO_EVA
        NumNotas = NumNotas + val(rs!notas_eva)
        rs.MoveNext
    Next I
Else
    EV = 0
End If
rs.Close
'******* CONSTRUIR MATRICES **********

'******* Ms FlexGrid **********
    X_ev = EV * 2 * PE ' Más las ponderaciones
        
    MSFlexGrid1.Cols = NumNotas * PE + (PE * 2) + 1 + X_ev + 3
    MSFlexGrid1.Row = 0
    
    MSFlexGrid1.MergeCells = flexMergeRestrictRows
    MSFlexGrid1.MergeRow(0) = True
    
        

    For p = 0 To PE - 1
        PERIODOS(p, 4) = Celda
        MSFlexGrid1.ColWidth(Celda - 1) = 200
        For e = 0 To EV - 1
            If p = 0 Then EVALUACIONES(e, 6) = Celda
            For I = Celda To Celda + EVALUACIONES(e, 3) - 1
                With MSFlexGrid1
                    .Col = I
                    .CellBackColor = IIf((e = 0), &HC0C0C0, IIf((e = 1), &HFFFF00, &HFFC0FF))       ' Val(EVALUACIONES(e, 4))
                    .CellForeColor = &H0
                    .CellAlignment = 4
                    .Text = Left$(EVALUACIONES(e, 1), 3) & " " & EVALUACIONES(e, 2) & " %"
                    .ColWidth(I) = 450
                  '****** promedio y ponderación de evaluación
                    With MSFlexGrid1
'''''                        .Col = i ''
'''''                        .CellBackColor = &H80000004 ''&HFFFFFF
'''''                        .CellForeColor = &H0 ''
'''''                        .Text = "X" ''
                    
                        .Col = I + 1
                        .CellBackColor = &H80000004 '&HFFFFFF
                        .CellForeColor = &H0
                        .Text = "X"
                        .ColWidth(I + 1) = 450
                        .Col = I + 2
                        .CellBackColor = &H80000004 '&HFFFFFF
                        .CellForeColor = &H0
                        .Text = "P"
                        .ColWidth(I + 2) = 450
                    End With
                  '****** FIN promedio y ponderación de evaluación
                End With
            Next I
            Celda = MSFlexGrid1.Col + 1
        Next e
    '****** promedio del período
        With MSFlexGrid1
            .Col = Celda
            .CellBackColor = &H80000004
            .CellForeColor = &H0
            .Text = "X P" & p + 1
            .ColWidth(Celda) = 450
        End With
    '****** FIN promedio del período
        Celda = Celda + 2
    Next p

    Celda = Celda - 2
    '****** Promedio entre periodos, examen y promedio  anual
    With MSFlexGrid1
        Celda = Celda + 1
        .Col = Celda
        .CellBackColor = &H80000004
        .CellForeColor = &H0
        .Text = "XP"
        .ColWidth(Celda) = 450
    
        Celda = Celda + 1
        .Col = Celda
        .CellBackColor = &H80000004
        .CellForeColor = &H0
        .Text = "NE"
        .ColWidth(Celda) = 450
    
        Celda = Celda + 1
        .Col = Celda
        .CellBackColor = &H80000004
        .CellForeColor = &H0
        .Text = "XF"
        .ColWidth(Celda) = 450
    End With
    '****** Promedio entre periodos, examen y promedio  anual
    MSFlexGrid1.Col = 0
    MSFlexGrid1.Row = 0
    MSFlexGrid1.BackColorFixed = &HFFFFFF
    MSFlexGrid1.ColWidth(0) = 4000
    MSFlexGrid1.CellAlignment = 4
    MSFlexGrid1.CellBackColor = &H80000004
    MSFlexGrid1.TextMatrix(0, 0) = "ALUMNOS"
    MSFlexGrid1.Row = 1
    MSFlexGrid1.CellBackColor = &HFFFFFF
    '******* FIN Ms FlexGrid **********
    
    MSFlexGrid1.Col = 2
    MSFlexGrid1.Row = 1
    ColAct = 2
    RowAct = 1
    
    EntraColChange = True

    X = EV
    X = PE
    For I = 0 To EV - 1
        X = EVALUACIONES(I, 6)
    Next I
    
    For I = 0 To PE - 1
        X = PERIODOS(I, 4)
    Next I
    
    If cole = "HUE001" Or cole = "ALT001" Then
'''''        MSFlexGrid1.ColWidth(15) = 0
'''''        MSFlexGrid1.ColWidth(16) = 0
'''''        MSFlexGrid1.ColWidth(17) = 0
'''''        'MSFlexGrid1.ColWidth(33) = 0
'''''        MSFlexGrid1.ColWidth(34) = 0
'''''        MSFlexGrid1.ColWidth(35) = 0
'''''        'MSFlexGrid1.ColWidth(37) = 0
'''''        MSFlexGrid1.ColWidth(38) = 0
        
        MSFlexGrid1.ColWidth(14) = 0 '15
        MSFlexGrid1.ColWidth(15) = 0    '16
        MSFlexGrid1.ColWidth(16) = 0    '17
        'MSFlexGrid1.ColWidth(33) = 0
''''        '''''Prueba Examen
''''        If Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 4 And CodModalidad = "110" And cmb_agno.Text >= 2014 Then
''''            MSFlexGrid1.ColWidth(31) = 0
''''        End If
        
        
        MSFlexGrid1.ColWidth(30) = 0    '''''''34 columna amarilla amarilla
        MSFlexGrid1.ColWidth(31) = 0    '''''''34 columna amarilla
        MSFlexGrid1.ColWidth(32) = 0    '34
        MSFlexGrid1.ColWidth(33) = 0    '35
        'MSFlexGrid1.ColWidth(37) = 0
        MSFlexGrid1.ColWidth(36) = 0    '38
    End If
    
    If cole = "SNB001" Then
        MSFlexGrid1.ColWidth(15) = 0    '16
'        MSFlexGrid1.ColWidth(16) = 0    '16
        MSFlexGrid1.ColWidth(17) = 0    '17
        MSFlexGrid1.ColWidth(18) = 0    '18
        'MSFlexGrid1.ColWidth(19) = 0    '19

        MSFlexGrid1.ColWidth(34) = 0    '34
        MSFlexGrid1.ColWidth(36) = 0    '35
        MSFlexGrid1.ColWidth(37) = 0    '34
        'MSFlexGrid1.ColWidth(38) = 0    '35

       'MSFlexGrid1.ColWidth(39) = 0    '38
        MSFlexGrid1.ColWidth(40) = 0
        MSFlexGrid1.ColWidth(41) = 0    '38
       ' MSFlexGrid1.ColWidth(42) = 0
    End If
End Sub


Private Sub Form_Unload(Cancel As Integer)

If MsgBox("¿Desea guardar antes de salir?", vbQuestion + vbYesNo) = vbYes Then
    
Dim RutProfe As String
Dim objRS As ADODB.Recordset
Set objRS = New ADODB.Recordset
Dim var_sql As String
Dim accion_realizada As String
Dim grado As String
Dim curso As String
Dim log As Boolean
var_sql = "SELECT rut_prof FROM cursos" _
        & " where cod_ens = '" & CodModalidad & "' and num_gra = " & Cmb_Grado _
        & " and let_cur = '" & Cmb_Let_Cur & "' and agno = " & cmb_agno.Text
objRS.Open var_sql, conn
RutProfe = objRS("rut_prof")
objRS.Close

If Me.Cmb_Cod_ens.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Cod_ens.SetFocus: Exit Sub
If Me.Cmb_Grado.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Grado.SetFocus: Exit Sub
If Me.Cmb_Let_Cur.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Let_Cur.SetFocus: Exit Sub
If Me.Cmb_Sector.SelectedItem.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Sector.SetFocus: Exit Sub
If Me.Cmb_Asi.Text = "" Then MsgBox "Falta información requerida.", vbExclamation: Cmb_Asi.SetFocus: Exit Sub

Dim CelIniEva, CelIniPer As Integer
Dim CodRegOpe As String
Form_Notas.Enabled = False
Btn_Ace_Not.Enabled = False

Form_Progress.Label1.Caption = "Guardando ..."
Form_Progress.Show
Form_Progress.ProgBar1.MaxValue = Me.MSFlexGrid1.Rows - 1

For R = 1 To Me.MSFlexGrid1.Rows - 1
 X = 0
    For p = 0 To PE - 1
        CelIniPer = PERIODOS(p, 4)
        For e = 0 To EV - 1
            CelIniEva = PERIODOS(p, 4) - 2 + EVALUACIONES(e, 6)
                For I = CelIniEva To CelIniEva + EVALUACIONES(e, 3) - 1
                    If MSFlexGrid1.TextMatrix(R, I) <> "" Then
                        notas = notas & Left(MSFlexGrid1.TextMatrix(R, I), 1) & Right(MSFlexGrid1.TextMatrix(R, I), 1)
                    Else
                        notas = notas & "  "
                    End If
                Next I
            '*** graba evaluacion "e" en periodo "p"
            Rut_ALu = Left(LTrim(MSFlexGrid1.TextMatrix(R, 0)), 10)
            If Not ExistReg("NOTAS", "CI_ALU ='" & Rut_ALu & "' AND COD_ENS ='" & CodModalidad & "' AND NUM_GRA ='" & Cmb_Grado & "' AND LETRA_CURSO ='" & Cmb_Let_Cur & "' AND COD_SEC ='" & Codsec & "' AND COD_ASI ='" & Codasi & "' AND NUM_PER ='" & p & "' AND COD_EVA ='" & e & "' and agno = " & cmb_agno.Text, conn) Then
                conn.Execute "INSERT INTO NOTAS(COD_ENS, NUM_GRA, LETRA_CURSO, COD_SEC, COD_ASI, CI_ALU, NUM_PER, COD_EVA, NOTA,AGNO) values('" & CodModalidad & "', '" & Cmb_Grado & "', '" & Cmb_Let_Cur & "', '" & Codsec & "', '" & Codasi & "', '" & Rut_ALu & "', '" & p & "', '" & e & "', '" & notas & "'," & cmb_agno.Text & ")"
                CodRegOpe = "AGR"
            Else
                conn.Execute "UPDATE NOTAS Set NOTA = '" & notas & "' WHERE CI_ALU ='" & Rut_ALu & "' AND COD_ENS ='" & CodModalidad & "' AND NUM_GRA ='" & Cmb_Grado & "' AND LETRA_CURSO ='" & Cmb_Let_Cur & "' AND COD_SEC ='" & Codsec & "' AND COD_ASI ='" & Codasi & "' AND NUM_PER ='" & p & "' AND COD_EVA ='" & e & "' and agno = " & cmb_agno.Text
                CodRegOpe = "MOD"
            End If
            '*** FIN graba evaluacion "e" en periodo "p"
            notas = ""
        X = CelIniPer - 2 + EVALUACIONES(e, 6) + EVALUACIONES(e, 3) + 1
        Next e
        '################################# GUARDA EN TABLA PROMEDIOS ##################
        Dim PromBd As String
        If MSFlexGrid1.TextMatrix(R, X + 1) = "" Then
            PromBd = ""
        Else
            PromBd = Left(CStr(MSFlexGrid1.TextMatrix(R, X + 1)), 1) & "." & Right(CStr(MSFlexGrid1.TextMatrix(R, X + 1)), 1)
        End If
        
        If val(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 3)) > 0 Then
            PG = Replace(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 3), ",", ".")
        Else
            PG = "Null"
        End If
        
        If val(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 2)) > 0 Then
            NE = Replace(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 2), ",", ".")
        Else
            NE = "Null"
        End If
        
        If val(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 1)) > 0 Then
            nf = Replace(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 1), ",", ".")
        Else
            nf = "Null"
        End If
        
        
        Rut_ALu = Left(MSFlexGrid1.TextMatrix(R, 0), 10)
        If Not ExistReg("promedios", "ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "' and agno =" & cmb_agno.Text, conn) Then
            conn.Execute "INSERT INTO PROMEDIOS(CI_ALU, COD_ENS, NUM_GRA, LETRA_CURSO, COD_SEC, COD_ASI, P" & p + 1 & ", PG, NE, NF,agno) values('" & Rut_ALu & "','" & CodModalidad & "','" & Me.Cmb_Grado & "','" & Me.Cmb_Let_Cur & "','" & Codsec & "','" & Codasi & "','" & PromBd & "','" & PG & "','" & NE & "','" & nf & "', " & cmb_agno.Text & " )"
        Else
            conn.Execute "UPDATE PROMEDIOS Set P" & p + 1 & " = '" & PromBd & "', PG ='" & PG & "', NE ='" & NE & "', NF ='" & nf & "' where ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "' and agno =" & cmb_agno.Text
        End If
        '##############################################################################

    
    Next p
    Form_Progress.ProgBar1.ShowProgress R
    DoEvents
Next R

'Log de modifición de notas
    Select Case CodModalidad
        Case "996"
            grado = "PK"
        Case "010"
            grado = "K"
        Case "110"
            grado = "EB"
        Case "310"
            grado = "EM"
    End Select
    curso = CStr(Cmb_Grado) & "º " & Cmb_Let_Cur & " " & grado
    accion_realizada = "Se han modificado las notas del subsector " & Cmb_Asi.Text _
                     & " que pertenece al sector " & Cmb_Sector.Text _
                     & " para el curso " & curso & " del año " _
                     & cmb_agno.Text
    log = Inserta_Vitacora_Profesores(Trim(RutProfe), "Modificación de notas", Trim(accion_realizada))
'Fin de log

'********** Registro de operaciones ***********
Call RegOpe(CodRegOpe, "NOTAS@" & CodModalidad & Me.Cmb_Grado & Me.Cmb_Let_Cur & "@" & Me.Cmb_Sector.Text & "@" & Me.Cmb_Asi, conn)
'********** FIN Registro de operaciones ***********
Form_Progress.ProgBar1.ShowProgress 0
Unload Form_Progress
MsgBox "Las Calificaciones han sido grabadas exitosamente"
Form_Notas.Enabled = True
Btn_Ace_Not.Enabled = True

Else
   
End If

Call menuhabilitado(True)
End Sub

Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
Dim Nota As Double
If CInt(cmb_agno.Text) < Anno Then KeyAscii = 0: Exit Sub

cole = DevuelveCampo("cfg", "", "valor", "param='CODBC'", conn)
If cole = "SNB001" And (MSFlexGrid1.Col = 15 Or MSFlexGrid1.Col = 16 Or MSFlexGrid1.Col = 34 Or MSFlexGrid1.Col = 35) Then
    Exit Sub
End If

modificarnotas = DevuelveCampo("cfg", "", "valor", "param='modificarnotas'", conn)
' ***********
' ****** Validacion primer semestre notas San Martin
If cole = "SNM001" And MSFlexGrid1.Col > 1 And MSFlexGrid1.Col < 14 And modificarnotas = 0 Then
    MsgBox "No se pueden modificar notas del primer semestre", vbInformation
   ''  MsgBox MSFlexGrid1.Col
    Exit Sub
End If

If EntraColChange Then
    Dim RowOld As Long
    RowOld = MSFlexGrid1.Row
    MSFlexGrid1.Row = 0
    
    If Right(MSFlexGrid1.Text, 1) <> "%" Then
        If MSFlexGrid1.Text <> "NE" Then
           KeyAscii = 0
           MSFlexGrid1.Row = RowOld
           Exit Sub
        End If
    End If
    
    If MSFlexGrid1.Col = (MSFlexGrid1.Cols - 1) - 1 And Not UsoDeExamen Then KeyAscii = 0
    
    If MSFlexGrid1.Col <= MSFlexGrid1.Cols - 6 - 1 Then
    '*********************************************
    '************** Cierre periodo ***************
    '*********************************************
    Dim cx As Long
    cx = MSFlexGrid1.Col
    Dim xx() As Long
    xx = PeEv(cx)
    
    For I = 1 To Len(CierrePer)
        If xx(0) = I - 1 Then
            If Mid(CierrePer, I, 1) = "0" Then
                MsgBox "El Período está cerrado. Solo el departamento de UTP puede modificar las calificaciones.", vbExclamation + vbOKOnly
                MSFlexGrid1.SetFocus
                MSFlexGrid1.Row = RowOld
                Exit Sub
            Else
                Exit For
            End If
        End If
    Next I
    '*********************************************
  End If
    MSFlexGrid1.Row = RowOld
        
End If
Columna = Me.MSFlexGrid1.Col
Fila = Me.MSFlexGrid1.Row

'Permite Borrar
If KeyAscii = 8 Then
    If MSFlexGrid1.Text <> "" Then
        MSFlexGrid1.Text = Mid(MSFlexGrid1.Text, 1, Len(MSFlexGrid1.Text) - 1)
             If MSFlexGrid1.Text = "" Then
                
                '********* Funcion promedio *****
                Call CalcProm(MSFlexGrid1.Col, MSFlexGrid1.Row)
                Call CalcPromPer(MSFlexGrid1.Col, MSFlexGrid1.Row)
                Call CalcPromProm(MSFlexGrid1.Col, MSFlexGrid1.Row)
                Call CalcPromFin(MSFlexGrid1.Col, MSFlexGrid1.Row)
                '********* Fin Funcion promedio *
             End If
        Exit Sub
    End If
End If
'Fin Borrar

'Permite Suprimir tecla ESC
If KeyAscii = 27 Then
    If MSFlexGrid1.Text <> "" Then
        MSFlexGrid1.Text = ""
                '********* Funcion promedio *****
                Call CalcProm(MSFlexGrid1.Col, MSFlexGrid1.Row)
                Call CalcPromPer(MSFlexGrid1.Col, MSFlexGrid1.Row)
                Call CalcPromProm(MSFlexGrid1.Col, MSFlexGrid1.Row)
                Call CalcPromFin(MSFlexGrid1.Col, MSFlexGrid1.Row)
                '********* Fin Funcion promedio *
        Exit Sub
    End If
End If
'Fin Suprimir

'Permite Borrar Fila Letra f
If KeyAscii = 102 Then
    If MsgBox("¿Esta seguro de que desea borrar todas las notas de la fila actual?", vbCritical + vbYesNo) = vbYes Then
        
        'borra fila primer semestre
        If MSFlexGrid1.Col > 1 And MSFlexGrid1.Col < 12 Then 'primer semestre
        For I = 1 To 11
            MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, I) = ""
        Next I
        End If
        'borra fila
        
        'borra fila segundo semestre
        If MSFlexGrid1.Col > 17 And MSFlexGrid1.Col < 29 Then 'segundo semestre
        For I = 18 To 28
            MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, I) = ""
        Next I
        End If
        'borra fila
    Else
        Exit Sub
    End If
               '********* Funcion promedio *****
                Call CalcProm(MSFlexGrid1.Col, MSFlexGrid1.Row)
                Call CalcPromPer(MSFlexGrid1.Col, MSFlexGrid1.Row)
                Call CalcPromProm(MSFlexGrid1.Col, MSFlexGrid1.Row)
                Call CalcPromFin(MSFlexGrid1.Col, MSFlexGrid1.Row)
                '********* Fin Funcion promedio *
End If
'Fin Borrar Fila

'Permite Borrar Columna Letra c
If KeyAscii = 99 Then
    If MsgBox("¿Esta seguro de que desea borrar todas las notas de la columna actual?", vbCritical + vbYesNo) = vbYes Then
        
        'borrar columna
        For I = 1 To (MSFlexGrid1.Rows - 1)
            If MSFlexGrid1.Col = 11 Or MSFlexGrid1.Col = 12 Or MSFlexGrid1.Col = 13 Or MSFlexGrid1.Col = 28 Or MSFlexGrid1.Col = 29 Then 'borra filas independientes
                    MSFlexGrid1.TextMatrix(I, MSFlexGrid1.Col) = ""
            Else
            
            a = MSFlexGrid1.Col
            a2 = a
            
            'primer semestre
            If MSFlexGrid1.Col > 1 And MSFlexGrid1.Col < 11 Then
            MSFlexGrid1.Row = I
                While a < 11
                    MSFlexGrid1.TextMatrix(I, a) = MSFlexGrid1.TextMatrix(I, a + 1) 'copia nota de al lado
                    MSFlexGrid1.Col = a
    'Color Notas
    q = Mid(MSFlexGrid1.Text, 1, 1)
    w = Mid(MSFlexGrid1.Text, 3, 1)
    nota1 = q & w
    
    If nota1 <> "" Then
    If (CDec(nota1) < 40) Then
        MSFlexGrid1.CellForeColor = &HFF&
    Else
        MSFlexGrid1.CellForeColor = &HFF0000
    End If
    End If
    'Color Notas
                    a = a + 1
                    If a = 11 Then
                        MSFlexGrid1.TextMatrix(I, a) = ""
                    End If
                Wend
            End If
           'fin primer semestre
            
           'fin segundo semestre
            If MSFlexGrid1.Col > 17 And MSFlexGrid1.Col < 28 Then
            MSFlexGrid1.Row = I
                While a < 28
                    MSFlexGrid1.TextMatrix(I, a) = MSFlexGrid1.TextMatrix(I, a + 1) 'copia nota de al lado
                    MSFlexGrid1.Col = a
                    'Color Notas
    q = Mid(MSFlexGrid1.Text, 1, 1)
    w = Mid(MSFlexGrid1.Text, 3, 1)
    nota1 = q & w
    
    If nota1 <> "" Then
    If (CDec(nota1) < 40) Then
        MSFlexGrid1.CellForeColor = &HFF&
    Else
        MSFlexGrid1.CellForeColor = &HFF0000
    End If
    End If
    'Color Notas
                    a = a + 1
                    If a = 28 Then
                        MSFlexGrid1.TextMatrix(I, a) = ""
                    End If
                Wend
            End If
            'fin primer semestre
            
            MSFlexGrid1.Col = a2
            
            End If
            
            '********* Funcion promedio *****
                Call CalcProm(MSFlexGrid1.Col, I)
                Call CalcPromPer(MSFlexGrid1.Col, I)
                Call CalcPromProm(MSFlexGrid1.Col, I)
                Call CalcPromFin(MSFlexGrid1.Col, I)
            '********* Fin Funcion promedio *
        Next I
        'borrar columna
    Else
        Exit Sub
    End If
End If
'Fin Borrar columna c

'permite ingresar un caracter si esta entre 0 y 9 i si los digitos son menores que 3 y NOTA P
If (Chr(KeyAscii) >= "0" And Chr(KeyAscii) <= "9" And Len(MSFlexGrid1.Text) < 3) Or ((Chr(KeyAscii) = "p" Or Chr(KeyAscii) = "P") And Len(MSFlexGrid1.Text) < 1) Then

    'si el largo es igual a 0 y el caracter a ingresar es igual a 0 no permite
    If Len(Trim(MSFlexGrid1.Text)) = 0 And Chr(KeyAscii) = "0" Then KeyAscii = 0: Exit Sub
    
    'concadena texto anterior con ingresado
    If (Chr(KeyAscii) = "p" Or Chr(KeyAscii) = "P") Then
        MSFlexGrid1.Text = " P"
    Else
        MSFlexGrid1.Text = UCase(MSFlexGrid1.Text & Chr(KeyAscii))
    End If
    
    'asigna una variable que no se ke hace
    txtCel = MSFlexGrid1.Text
    
    'Color Notas y NOTA P
    If Chr(KeyAscii) <> "p" And Chr(KeyAscii) <> "P" Then
        If (CDec(MSFlexGrid1.Text) > 0 And CDec(MSFlexGrid1.Text) < 4) Or (CDec(MSFlexGrid1.Text) > 9 And CDec(MSFlexGrid1.Text) < 40) Or (CDec(MSFlexGrid1.Text) > 99 And CDec(MSFlexGrid1.Text) < 400) Then
            MSFlexGrid1.CellForeColor = &HFF&
        Else
            MSFlexGrid1.CellForeColor = &HFF0000
        End If
    End If
    'Color Notas
    
    'Mayor 7
    If Chr(KeyAscii) = "p" Or Chr(KeyAscii) = "P" Then
         MSFlexGrid1.CellForeColor = &HFF&
         Me.MSFlexGrid1.SetFocus
         Me.MSFlexGrid1.Col = Columna
         Me.MSFlexGrid1.Row = Fila
         Exit Sub
    Else
        If Len(Trim(MSFlexGrid1.Text)) = 1 And CDec(MSFlexGrid1.Text) > 7 Or Len(Trim(MSFlexGrid1.Text)) = 2 And CDec(MSFlexGrid1.Text) > 70 Then
            MsgBox "La nota ingresada no es válida. El valor a debe estar entre 1.0 y 7.0", vbInformation
            MSFlexGrid1.Text = Mid(MSFlexGrid1.Text, 1, Len(MSFlexGrid1.Text) - 1)
            Exit Sub
        Else
            Me.MSFlexGrid1.SetFocus
            Me.MSFlexGrid1.Col = Columna
            Me.MSFlexGrid1.Row = Fila
            Exit Sub
        End If
    End If
    'Mayor 7
   
    Exit Sub
End If
KeyAscii = 0
End Sub
Public Function PeEv(CeldaActual As Long) As Long()
Dim Matriz(1) As Long
Celda = 2
MSFlexGrid1.Col = 2
For p = 0 To PE - 1
    For e = 0 To EV - 1
        For I = Celda To Celda + EVALUACIONES(e, 3) - 1 + 2
             If I = CeldaActual Then
                Matriz(0) = p
                Matriz(1) = e
                PeEv = Matriz
                Me.MSFlexGrid1.Col = CeldaActual
                Exit Function
             End If
        Next I
        Celda = I
    Next e
Celda = Celda + 2
Next p


End Function

Private Sub MSFlexGrid1_LostFocus()
Call MSFlexGrid1_SelChange
End Sub



Private Sub MSFlexGrid1_SelChange()

Dim Col2, Row2 As Integer
  Col2 = MSFlexGrid1.Col
  Row2 = MSFlexGrid1.Row
   
  MSFlexGrid1.Col = ColAct
  MSFlexGrid1.Row = RowAct
  txtCel = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, MSFlexGrid1.Col)
 
If Len(txtCel) = 1 And val(Trim(txtCel)) > 0 Then
    MSFlexGrid1.Text = Format(val(MSFlexGrid1.Text), "#.0")
    
    '********* Funcion promedio *****
    If Left(Trim(MSFlexGrid1.TextMatrix(0, ColAct)), 1) <> "X" And MSFlexGrid1.TextMatrix(0, ColAct) <> "P" Then
        Call CalcProm(ColAct, RowAct)
        Call CalcPromPer(MSFlexGrid1.Col, MSFlexGrid1.Row)
        Call CalcPromProm(MSFlexGrid1.Col, MSFlexGrid1.Row)
        Call CalcPromFin(MSFlexGrid1.Col, MSFlexGrid1.Row)
    End If
    '********* Fin Funcion promedio *
End If

If (Len(txtCel) = 2) And val(Trim(txtCel)) > 0 Then
    n = Mid(txtCel, 1, 1)
    d = Mid(txtCel, 2, 1)
    MSFlexGrid1.Text = Format(val(n), "." & val(d))
    
    '********* Funcion promedio *****
    If Left(Trim(MSFlexGrid1.TextMatrix(0, ColAct)), 1) <> "X" And MSFlexGrid1.TextMatrix(0, ColAct) <> "P" Then
        Call CalcProm(ColAct, RowAct)
        Call CalcPromPer(MSFlexGrid1.Col, MSFlexGrid1.Row)
        Call CalcPromProm(MSFlexGrid1.Col, MSFlexGrid1.Row)
        Call CalcPromFin(MSFlexGrid1.Col, MSFlexGrid1.Row)
    End If
    '********* Fin Funcion promedio *
End If

If (Len(txtCel) = 3) And val(Trim(txtCel)) > 0 Then
bc = InStr(txtCel, ",")
If bc = 0 Then 'no tiene coma
    n = Mid(txtCel, 1, 1)
    d = Mid(txtCel, 2, 1)
    MSFlexGrid1.Text = Format(val(n), "." & val(d))
    
    '********* Funcion promedio *****
    If Left(Trim(MSFlexGrid1.TextMatrix(0, ColAct)), 1) <> "X" And MSFlexGrid1.TextMatrix(0, ColAct) <> "P" Then
        Call CalcProm(ColAct, RowAct)
        Call CalcPromPer(MSFlexGrid1.Col, MSFlexGrid1.Row)
        Call CalcPromProm(MSFlexGrid1.Col, MSFlexGrid1.Row)
        Call CalcPromFin(MSFlexGrid1.Col, MSFlexGrid1.Row)
    End If
    '********* Fin Funcion promedio *
End If
End If

  MSFlexGrid1.Col = Col2
  MSFlexGrid1.Row = Row2

If val(txtCel) < 1 And txtCel <> "" Then

End If
    ColAct = MSFlexGrid1.Col
    RowAct = MSFlexGrid1.Row
End Sub

Public Sub CalcProm(ColNot As Long, RowNot)
If ColNot >= MSFlexGrid1.Cols - 2 Then Exit Sub
Dim CelIniEva, CelIniPer, Divisor As Integer
Dim SumPro As Single
Dim cole As String
Dim prom As Single
Dim prom_ant As Single
Dim xx() As Long

Dim por70 As Single
Dim por30 As Single
Dim porce70 As Single
Dim porce30 As Single

Divisor = 0
xx = PeEv(ColNot)
CelIniPer = PERIODOS(xx(0), 4)
CelIniEva = PERIODOS(xx(0), 4) - 2 + EVALUACIONES(xx(1), 6)

porce70 = 0.6
porce30 = 0.4

'revisa huech
pn = DevuelveCampo("subsectores", "", "pn", "nom_asi = '" & Me.Cmb_Asi & "' and cod_ens ='" & CodModalidad & "' and num_gra ='" & Me.Cmb_Grado & "' and cod_sec ='" & Codsec & "'", conn)
cole = DevuelveCampo("cfg", "", "valor", "param = 'codbc'", conn)

For I = CelIniEva To CelIniEva + EVALUACIONES(xx(1), 3) - 1
If cole = "FLO001" Then
    If val(MSFlexGrid1.TextMatrix(RowNot, I)) > 0 And I <> 14 And I <> 15 And I <> 16 And I <> 33 Then
        SumPro = SumPro + CDbl(MSFlexGrid1.TextMatrix(RowNot, I))
        Divisor = Divisor + 1
    End If
Else
    If val(MSFlexGrid1.TextMatrix(RowNot, I)) > 0 And I <> 14 And I <> 15 And I <> 16 And I <> 33 And I <> 34 And I <> 35 Then
        SumPro = SumPro + CDbl(MSFlexGrid1.TextMatrix(RowNot, I))
        Divisor = Divisor + 1
    End If

End If
Next I

If pn = 1 And CelIniPer = 2 Then 'primer semestre
    If val(MSFlexGrid1.TextMatrix(RowNot, 12)) > 0 Then
    SumPro = SumPro + CDbl(MSFlexGrid1.TextMatrix(RowNot, 12))
    Divisor = Divisor + 1
    End If
End If

If pn = 1 And (CelIniPer = 18 Or CelIniPer = 19) Then 'segundo semestre
    
    If cole = "HUE001" Or cole = "ALT001" Then
        If cole = "HUE001" Then
            If CodModalidad = "110" And Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 4 And cmb_agno.Text >= 2014 Then
                If val(MSFlexGrid1.TextMatrix(RowNot, 29)) > 0 Then
                    SumPro = SumPro + CDbl(MSFlexGrid1.TextMatrix(RowNot, 29))
                    Divisor = Divisor + 1
                End If
            End If
        Else
            If CodModalidad = "110" And Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 6 And cmb_agno.Text >= 2014 Then
                If val(MSFlexGrid1.TextMatrix(RowNot, 29)) > 0 Then
                    SumPro = SumPro + CDbl(MSFlexGrid1.TextMatrix(RowNot, 29))
                    Divisor = Divisor + 1
                End If
            End If
        End If
    Else
        If cole = "SNB001" Then
            If CodModalidad = "110" And Me.Cmb_Grado >= 1 And Me.Cmb_Grado <= 6 And cmb_agno.Text >= 2014 Then
'''''                If val(MSFlexGrid1.TextMatrix(RowNot, 29)) > 0 Then
'''''                    SumPro = SumPro + CDbl(MSFlexGrid1.TextMatrix(RowNot, 29))
'''''                    Divisor = Divisor + 1
'''''                End If
            If val(MSFlexGrid1.TextMatrix(RowNot, 28)) > 0 Then
                SumPro = SumPro + CDbl(MSFlexGrid1.TextMatrix(RowNot, 28))
                Divisor = Divisor + 1
            End If

            End If
        Else
            If val(MSFlexGrid1.TextMatrix(RowNot, 28)) > 0 Then
                SumPro = SumPro + CDbl(MSFlexGrid1.TextMatrix(RowNot, 28))
                Divisor = Divisor + 1
            End If
        End If
    End If
    
End If


    MSFlexGrid1.Row = RowNot
    MSFlexGrid1.Col = I + 1
    MSFlexGrid1.CellBackColor = &H80000004
    MSFlexGrid1.Col = I
    MSFlexGrid1.CellBackColor = &H80000004
    

If Divisor > 0 Then
  '  prom = Format(Int(Round((SumPro / Divisor) * 10, 1)) / 10, "0.0")
     
     If cole = "LIN001" Then
            'prom = Format(val(Round((SumPro / Divisor) * 10, 1) + 0.05) / 10, "0.0")
            prom = Format(Int(Round(((SumPro / Divisor) * 10) + 0.05)) / 10, "0.0")
     Else
'        prom = Format(Int(Round((SumPro / Divisor) * 10, 1)) / 10, "0.0")
        If cole = "HUE001" Or cole = "ALT001" Or cole = "FLO001" Then
            'prom = Format((Round((SumPro / Divisor) * 10)) / 10, "0.0") ' huechuraba pide que se aproxime los promedios semestrales
            'prom = Format(Int(Round(((SumPro / Divisor) * 10) + 0.1)) / 10, "0.0") ' huechuraba pide que se aproxime los promedios semestrales
            prom = Format(Int(Round(((SumPro / Divisor) * 10) + 0.04)) / 10, "0.0") ' huechuraba pide que se aproxime los promedios semestrales
        Else
            If cole = "SNB001" Then
                'prom = Format(Int(Round(((SumPro / Divisor) * 10) + 0.04)) / 10, "0.0")
                prom = Format(Int(Round((SumPro / Divisor) * 10, 1)) / 10, "0.0")
            Else
                prom = Format(Int(Round((SumPro / Divisor) * 10, 1)) / 10, "0.0")
            End If
            prom_ant = prom
            'MsgBox MSFlexGrid1.TextMatrix(RowNot, 14)
            If cole = "SNB001" And (MSFlexGrid1.TextMatrix(RowNot, 14) <> "" Or MSFlexGrid1.TextMatrix(RowNot, 33) <> "") Then
                If I > 30 Then
                    'segundo semestre
                    prom = Format(Int(Round(((SumPro / Divisor) * 10) + 0.04)) / 10, "0.0")
                    ' prom = Format(Int(Round((SumPro / Divisor) * 10, 1)) / 10, "0.0")
                    prom_ant = prom
                    If MSFlexGrid1.TextMatrix(RowNot, 33) <> "" Then
                    'por70 = CSng(MSFlexGrid1.TextMatrix(RowNot, MSFlexGrid1.Cols - 4)) * porce70
                    por70 = Format((((prom_ant * porce70) * 10)) / 10, "0.0")
                    'por70 = prom_ant * porce70
                    If MSFlexGrid1.TextMatrix(RowProm, 33) <> "" Then
                        'por30 = CSng(MSFlexGrid1.TextMatrix(RowNot, 32)) * porce30
                        'por30 = Format(Int(Round((CSng(MSFlexGrid1.TextMatrix(RowNot, 33)) * porce30) * 10, 1)) / 10, "0.0")
                        por30 = Format((((CSng(MSFlexGrid1.TextMatrix(RowNot, 33)) * porce30))), "0.00")
                        prom = por70 + por30
                        prom = Round(prom, 1)
                    Else
                    ' PromFin = CSng(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3))
                    End If
                    End If
                Else
                    'primer semestre
                    prom = Format(Int(Round(((SumPro / Divisor) * 10) + 0.04)) / 10, "0.0")
                    'prom = Format(Int(Round((SumPro / Divisor) * 10, 1)) / 10, "0.0")
                    prom_ant = prom
                    If MSFlexGrid1.TextMatrix(RowNot, 14) <> "" Then
                    'por70 = CSng(MSFlexGrid1.TextMatrix(RowNot, 17)) * porce70
                    ' Aproximar
                    'por70 = prom_ant * porce70
                    por70 = Format((((prom_ant * porce70) * 10)) / 10, "0.0")
                    If MSFlexGrid1.TextMatrix(RowProm, 14) <> "" Then
                        'por30 = CSng(MSFlexGrid1.TextMatrix(RowNot, 14)) * porce30
                        'por30 = Format(Int(Round((CSng(MSFlexGrid1.TextMatrix(RowNot, 14)) * porce30) * 10, 1)) / 10, "0.0")
                        por30 = Format(((((MSFlexGrid1.TextMatrix(RowNot, 14)) * porce30))), "0.00")
                        prom = por70 + por30
                        prom = Round(prom, 1)
                    Else
                    ' PromFin = CSng(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3))
                    End If
                    End If
                End If
            Else
               ' prom = Format(Int(Round((SumPro / Divisor) * 10, 1)) / 10, "0.0")
            End If
        End If
     End If
     
    'Asigna el X
    If cole = "SNB001" Then
        If Len(CStr(prom_ant)) < 3 And val(Trim(prom_ant)) > 0 Then
            MSFlexGrid1.TextMatrix(RowNot, I - 2) = Format(val(prom_ant), "#.0") 'Format(Int((SumPro / Divisor) * 10) / 10, ".0")
        Else
            MSFlexGrid1.TextMatrix(RowNot, I - 2) = prom_ant
        End If
    End If
    If cole = "SNB001" Then
        If Len(CStr(por70)) < 3 And val(Trim(por70)) > 0 Then
            MSFlexGrid1.TextMatrix(RowNot, I - 1) = Format(val(por70), "#.0") 'Format(Int((SumPro / Divisor) * 10) / 10, ".0")
        Else
            If por70 <> 0 Then
                MSFlexGrid1.TextMatrix(RowNot, I - 1) = por70
            End If
        End If
    End If
    
    
    If Len(CStr(prom)) < 3 And val(Trim(prom)) > 0 Then
        MSFlexGrid1.TextMatrix(RowNot, I) = Format(val(prom), "#.0") 'Format(Int((SumPro / Divisor) * 10) / 10, ".0")
    Else
        MSFlexGrid1.TextMatrix(RowNot, I) = prom
    End If
    MSFlexGrid1.TextMatrix(RowNot, I + 1) = Format(prom * val(EVALUACIONES(xx(1), 2)) / 100, "#0.00")
    '************ Rojos o Azules ***************
    MSFlexGrid1.CellBackColor = &H80000004
    If CDec(MSFlexGrid1.TextMatrix(RowNot, I)) < 4 Then
        MSFlexGrid1.CellForeColor = &HFF&
    Else
        MSFlexGrid1.CellForeColor = &HFF0000
    End If
    '************ FIN Rojos o Azules ***************
Else
    MSFlexGrid1.TextMatrix(RowNot, I) = ""
    MSFlexGrid1.TextMatrix(RowNot, I + 1) = ""
    MSFlexGrid1.CellBackColor = &HFFFFFF
End If
MSFlexGrid1.Col = ColNot
MSFlexGrid1.Row = RowNot
End Sub

Public Sub CalcPromPer(ColProm As Long, RowProm)
If ColProm >= MSFlexGrid1.Cols - 2 Then Exit Sub
Dim CelIniEva, CelIniPer As Integer
Dim PromPer As Single
Dim xx() As Long
xx = PeEv(ColProm)
CelIniPer = PERIODOS(xx(0), 4)
CelIniEva = PERIODOS(xx(0), 4) - 2 + EVALUACIONES(xx(1), 6)
'*
X = 0
For I = 0 To EV - 1
X = CelIniPer - 2 + EVALUACIONES(I, 6) + EVALUACIONES(I, 3) + 1
If Format(MSFlexGrid1.TextMatrix(RowProm, X), "#0.00") <> "" Then
   PromPer = PromPer + Format(MSFlexGrid1.TextMatrix(RowProm, X), "#0.00")
   Divisor = Divisor + 1
End If
Next I
    
    MSFlexGrid1.Col = X + 1
    MSFlexGrid1.Row = RowProm

If Format(Int(Round((PromPer) * 10, 1)) / 10, "0.0") > 0 Then
    MSFlexGrid1.TextMatrix(RowProm, X + 1) = Format(Int(Round((PromPer) * 10, 1)) / 10, "0.0") 'Format(Int(PromPer * 10) / 10, "0.0")

'************ Promedios Rojos o Azules ***************
    MSFlexGrid1.CellBackColor = &H80000004
    If CDec(MSFlexGrid1.TextMatrix(RowProm, X + 1)) < 4 Then
        MSFlexGrid1.CellForeColor = &HFF&
    Else
        MSFlexGrid1.CellForeColor = &HFF0000
    End If
'************ FIN Prpmedios Rojos o Azules ***************
If GuardaPromedios Then
'################################# GUARDA EN TABLA TODOS LOS PROMEDIOS ##################
    Dim PromBd, CampoProm As String
    CampoProm = "P" & Trim(str(val(xx(0)) + 1))
    PromBd = Left(CStr(MSFlexGrid1.TextMatrix(RowProm, X + 1)), 1) & "." & Right(CStr(MSFlexGrid1.TextMatrix(RowProm, X + 1)), 1)
    Rut_ALu = Left(MSFlexGrid1.TextMatrix(RowProm, 0), 10)
        If Trim(PromBd) = "" Then
            PromBd = ""
        Else
            PromBd = PromBd
        End If
        '*
    If val(PromBd) > 7 Or val(PromBd) < 0 Then
        MsgBox "El promedio de período " & CampoProm & " " & PromBd & " NO es válido. No será guardado" & Chr(13) & Chr(13) & "Alumno " & Me.MSFlexGrid1.TextMatrix(RowProm, 0) & Chr(13) & "Sector " & Me.Cmb_Sector & Chr(13) & "Subsector " & Me.Cmb_Asi
        
        Exit Sub
    End If
    If Not ExistReg("promedios", "ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "'", conn) Then
       conn.Execute "INSERT INTO PROMEDIOS(CI_ALU, COD_ENS, NUM_GRA, LETRA_CURSO, COD_SEC, COD_ASI," & CampoProm & ",agno) values('" & Rut_ALu & "','" & CodModalidad & "','" & Me.Cmb_Grado & "','" & Me.Cmb_Let_Cur & "','" & Codsec & "','" & Codasi & "','" & PromBd & "'," & cmb_agno.Text & ")"
    Else
        conn.Execute "UPDATE PROMEDIOS Set " & CampoProm & " = '" & PromBd & "' where ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "' and agno = " & cmb_agno.Text
    End If
'########################################################################################
End If
Else
    MSFlexGrid1.TextMatrix(RowProm, X + 1) = ""
    MSFlexGrid1.CellBackColor = &HFFFFFF
End If

MSFlexGrid1.Col = ColProm
MSFlexGrid1.Row = RowProm
'MSFlexGrid1.SetFocus
End Sub
Public Sub CalcPromProm(ColProm As Long, RowProm)
If ColProm >= MSFlexGrid1.Cols - 2 Then Exit Sub
Dim CelIniEva, CelIniPer, Divisor As Integer
Dim PromProm As Single
Dim xx() As Long
xx = PeEv(ColProm)
CelIniPer = PERIODOS(xx(0), 4)
CelIniEva = PERIODOS(xx(0), 4) - 2 + EVALUACIONES(xx(1), 6)
'*
X = 0
Divisor = 0
For I = 0 To PE - 1
X = PERIODOS(I, 4) + NumNotas + (EV * 2)
    If Format(MSFlexGrid1.TextMatrix(RowProm, X), "#0.0") <> "" Then
        PromProm = PromProm + Format(MSFlexGrid1.TextMatrix(RowProm, X), "#0.0")
        Divisor = Divisor + 1
    End If
Next I
'*
    
    MSFlexGrid1.Col = MSFlexGrid1.Cols - 3
    MSFlexGrid1.Row = RowProm

If Divisor > 0 Then
    fraccion = val(Right(Format(PromProm / Divisor, "#0.00"), 2))
    If Anno > 2004 And DevuelveCampo("establecimientos", "", "rdb", " rdb <> '' ", conn) <> "9782-9" Then
        PromProm = Format(PromProm / Divisor, "#0.0")
        If fraccion >= 90 Then
            'MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3) = PromProm 'Format(Format(PromProm, "."), "0.0")
            MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3) = Format(PromProm, "0.0")
        Else
            MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3) = Format(Int(Round((PromProm) * 10, 1)) / 10, "0.0") 'Format(Int(PromProm * 10) / 10, "0.0")
        End If
    Else
        ''Aqui lincoln
        If (PromProm > 7.6 And PromProm < 8#) Then
            PromProm = 4#
        Else
            PromProm = Format(PromProm / Divisor, "#0.00")
        End If
        If fraccion >= 90 Then
            MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3) = Format(Format(PromProm, "."), "0.0")
        Else
            MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3) = Format(Int(Round((PromProm) * 10, 1)) / 10, "0.0")
        End If
        
        'Format(Int(PromProm * 10) / 10, "0.0")
    End If
    '************ Prpmedios Rojos o Azules ***************
    MSFlexGrid1.CellBackColor = &H80000004
    If CDec(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3)) < 4 Then
        MSFlexGrid1.CellForeColor = &HFF&
    Else
        MSFlexGrid1.CellForeColor = &HFF0000
    End If
    '************ FIN Prpmedios Rojos o Azules ***************
    If GuardaPromedios Then
        '########################### GUARDA EN TABLA TODOS LOS PROMEDIOS GENERALES #############
        Dim PromBd, CampoProm As String
        CampoProm = "PG"
        PromBd = Left(CStr(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3)), 1) & "." & Right(CStr(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3)), 1)
      
        If val(PromBd) > 0 Then
            PromBd = PromBd
        Else
            PromBd = "Null"
        End If
        
        If val(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 2)) > 0 Then
            NE = Replace(Me.MSFlexGrid1.TextMatrix(R, MSFlexGrid1.Cols - 2), ",", ".")
        Else
            NE = "Null"
        End If

        
        '*
        If val(PromBd) > 7 Or val(PromBd) < 0 Then
            MsgBox "El promedio de los períodos PG" & PromBd & " NO es válido. No será guardado"
            Exit Sub
        End If
        Rut_ALu = Left(MSFlexGrid1.TextMatrix(RowProm, 0), 10)
        If Not ExistReg("promedios", "ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "'", conn) Then
            'No hace nada el registro debe existir Promper lo creo
        Else
            conn.Execute "UPDATE PROMEDIOS Set " & CampoProm & " = '" & PromBd & "', NE  = '" & NE & "' where ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "' and agno = " & cmb_agno.Text
        End If
        '########################################################################################
    End If
    
    
    
Else
    MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3) = ""
    MSFlexGrid1.CellBackColor = &HFFFFFF
End If
MSFlexGrid1.Col = ColProm
MSFlexGrid1.Row = RowProm

End Sub
Public Sub CalcPromFin(ColProm As Long, RowProm)
    Dim por70 As Single
    Dim por30 As Single
    Dim porce70 As Single
    Dim porce30 As Single
       
'    If CodModalidad = "110" And Me.Cmb_Grado < 7 Then
        porce70 = 0.8
        porce30 = 0.2
'    Else
'        porce70 = 0.7
'        porce30 = 0.3
'    End If
    
    
    MSFlexGrid1.Row = RowProm
    MSFlexGrid1.Col = MSFlexGrid1.Cols - 1
If ColProm < MSFlexGrid1.Cols - 2 Or val(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 2)) = 0 Or val(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3)) = 0 Then
    MSFlexGrid1.CellBackColor = &HFFFFFF
    If MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 2) = "EX" Then
        Dim PromFin As Single
        If Trim(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3)) <> "" Then
''''''            If cole = "HUE001" Or cole = "ALT001" Then
''''''                por70 = CSng(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3)) * porce70
''''''                If MSFlexGrid1.TextMatrix(RowProm, 31) <> "" Then
''''''                    por30 = CSng(MSFlexGrid1.TextMatrix(RowProm, 31)) * porce30
''''''                    PromFin = por70 + por30
''''''                    PromFin = Round(PromFin, 1)
''''''                Else
''''''                    PromFin = CSng(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3))
''''''                End If
''''''            Else
                PromFin = CSng(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3))
''''''            End If
        Else
            PromFin = 0
        End If
        '************ Promedios Rojos o Azules ***************
        MSFlexGrid1.CellBackColor = &H80000004
        If PromFin > 0 Then
            If PromFin < 4 Then
                MSFlexGrid1.CellForeColor = &HFF&
            Else
                MSFlexGrid1.CellForeColor = &HFF0000
            End If
        End If
        '************ FIN Prpmedios Rojos o Azules ***************
        If GuardaPromedios Then
            '########################### GUARDA EN TABLA TODAS LAS NOTAS FINALES ###################
            Dim PromBd, CampoProm As String
            CampoProm = "NF"
            PromBd = CStr(PromFin) 'Left(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 1), 1) & "." & Right(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 1), 1)
            PromBd = Replace(PromBd, ",", ".")
            Rut_ALu = Left(MSFlexGrid1.TextMatrix(RowProm, 0), 10)
            '*
            If val(PromBd) > 0 Then
                PromBd = PromBd
            Else
                PromBd = "Null"
            End If
            '*
            If val(PromBd) > 7 Or val(PromBd) < 0 Then
                MsgBox "La calificación fial NF " & PromBd & " NO es válida. No será guardada"
                Exit Sub
            End If
            If Not ExistReg("promedios", "ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "'", conn) Then
                'No hace nada el registro debe existir Promper lo creo
            Else
                conn.Execute "UPDATE PROMEDIOS Set " & CampoProm & " = '" & PromBd & "' where ci_alu ='" & Rut_ALu & "' AND cod_ens ='" & CodModalidad & "' AND num_gra ='" & Me.Cmb_Grado & "' AND letra_curso ='" & Me.Cmb_Let_Cur & "' AND cod_sec ='" & Codsec & "' AND cod_asi ='" & Codasi & "' and agno = " & cmb_agno.Text
            End If
            '########################################################################################
        End If
        
        If Len(Trim(str(PromFin))) < 3 And PromFin > 0 Then
            MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 1) = Format(PromFin, "#.0")
        Else
            If Len(Trim(str(PromFin))) = 3 And PromFin > 0 Then
                PromFin = PromFin
                MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 1) = PromFin
            Else
                MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 1) = ""
            End If
        End If
    Else
        MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 1) = ""
    End If
Else
    
    PromFin = CDbl(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 3)) * 0.7 + CDbl(MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 2)) * 0.3
    '************ Prpmedios Rojos o Azules ***************
    MSFlexGrid1.CellBackColor = &H80000004
    If PromFin < 4 Then
        MSFlexGrid1.CellForeColor = &HFF&
    Else
        MSFlexGrid1.CellForeColor = &HFF0000
    End If
    '************ FIN Prpmedios Rojos o Azules ***************
    If Len(Trim(str(PromFin))) < 3 And PromFin > 0 Then
        MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 1) = Format(PromFin, "#.0")
    Else
        MSFlexGrid1.TextMatrix(RowProm, MSFlexGrid1.Cols - 1) = Format(PromFin, "#.0")
    End If
End If

MSFlexGrid1.Col = ColProm
MSFlexGrid1.Row = RowProm
End Sub

Public Sub LimpiaNotas()
'******** Limpia grilla
Form_Progress.Label1.Caption = "Limpiando ..."
Form_Progress.Show
Form_Progress.ProgBar1.MaxValue = Me.MSFlexGrid1.Rows - 1
For xf = 1 To MSFlexGrid1.Rows - 1
    For Xc = 1 To MSFlexGrid1.Cols - 1
        MSFlexGrid1.TextMatrix(xf, Xc) = ""
    Next Xc
Form_Progress.ProgBar1.ShowProgress xf
DoEvents
Next xf
Form_Progress.ProgBar1.ShowProgress 0
Unload Form_Progress
'******** FIN Limpia grilla
End Sub

'se agrega 16-05-2022
Private Sub CargaAlumnosAsignatura()
Dim NEva As Integer
Dim celPnivel() As Long 'Dim celPnivel() As Long
Dim numCElpnivel As Integer
Dim cod_asi As String


MSFlexGrid1.Enabled = False

If cmb_agno.Text = "" Then MsgBox "Debe seleccionar Año escolar": Exit Sub
'* ## identifica celdas para la prueba de nivel (ultima celda del ultimo tipo de evALUACION) ##
'* ## EXTRATADO DE LA FUNCION CALPROMPROM

'Se trae el Nº de evaluaciones para replicar la columna de la PN
NEva = val(DevuelveCampo("evaluaciones", "", "count(cod_eva)", "cod_eva <> ''", conn))
NEva = IIf(NEva = 0, 1, NEva) ' Si no hay evaluaciones ingresadas queda como 1 para no afectar la multiplicación de más abajo

ReDim celPnivel(PE * NEva)  ' Indica Tipos de Evaluaciones por Periodo

numCElpnivel = 0
For I = 1 To Me.MSFlexGrid1.Cols - 1
    If Me.MSFlexGrid1.TextMatrix(0, I) = "X" And ((InStr(Me.MSFlexGrid1.TextMatrix(0, I - 1), "APR") <> 0) Or (InStr(Me.MSFlexGrid1.TextMatrix(0, I - 1), "PRO") <> 0)) Then
        If cole = "HUE001" Or cole = "ALT001" Or cole = "SNB001" Then
            celPnivel(numCElpnivel) = I - 2
            numCElpnivel = numCElpnivel + 1
        Else
            celPnivel(numCElpnivel) = I - 1
            numCElpnivel = numCElpnivel + 1
        End If
    End If
    
Next I

alumno = 0

Sql = ""
Sql = " SELECT asi_elec, cod_asi,  Cod_Sec  FROM subsectores WHERE nom_asi = '" & Trim(Cmb_Asi.Text) & "'"
Sql = Sql & " AND COD_ENS = '" & CodModalidad & "'  AND NUM_GRA = '" & Ngrado & "' "
Set Rst = New ADODB.Recordset
Rst.Open Sql, conn
If Not Rst.EOF Then
    'IIf(MSFlexGrid1.TextMatrix(i, j) = "Null", "", Format(MSFlexGrid1.TextMatrix(i, j), "yyyy/mm/dd"))
    ASIG_ELEC = Rst!asi_elec
    cod_asi = Rst!cod_asi
    Cod_Sec = Rst!Cod_Sec
End If
Rst.Close


a = "select p.ape_pat_alu, p.ape_mat_alu, p.nom_alu, p.ci_alu from postulantes p, alu_x_cur a, matriculas m "
If ASIG_ELEC = "S" Then
    a = a & " JOIN asignaturas_electivas AE"
    a = a & " ON  AE.CI_ALU = P.CI_ALU"
    a = a & " AND AE.COD_ENS = A.COD_ENS"
    a = a & " AND AE.NUM_GRA = A.NUM_GRA"
    a = a & " AND AE.AGNO = A.AGNO"
    a = a & " AND AE.COD_ASI = '" & cod_asi & "'"
    a = a & " AND AE.COD_SEC = '" & Cod_Sec & "'"
End If
a = a & " where p.ci_alu = a.ci_alu and a.ci_alu=m.ci_alu and a.fec_mat=m.fec_mat and a.activo_alu='SI' and a.cod_ens = '" & CodModalidad & " ' "
a = a & " and a.num_gra = '" & Ngrado & "' and a.let_cur ='" & Me.Cmb_Let_Cur & "' and a.agno = " & cmb_agno.Text & " and a.agno=m.anno_mat "
If ASIG_ELEC = "S" Then
    a = a & " ORDER BY  p.ape_pat_alu, p.ape_mat_alu, p.nom_alu"
Else
    a = a & " ORDER BY orden_libro,a.fec_ing_curso, m.num_mat, p.ape_pat_alu, p.ape_mat_alu, p.nom_alu"
End If
    MSFlexGrid1.Enabled = True
    Set rs = New ADODB.Recordset
    'rs.CursorLocation = adUseClientc

rs.Open a, conn
Me.MSFlexGrid1.Rows = 1
If rs.RecordCount > 0 Then

For alumno = 1 To rs.RecordCount
    Me.MSFlexGrid1.AddItem ""
    For I = 0 To (PE * NEva) - 1 Step 2
        If cole = "HUE001" Or cole = "ALT001" Then
            Me.MSFlexGrid1.Col = celPnivel(I) + 1
            Me.MSFlexGrid1.Row = alumno
            Me.MSFlexGrid1.CellBackColor = &HC0FFFF
            Me.MSFlexGrid1.Col = celPnivel(I) - 1
            Me.MSFlexGrid1.Row = alumno
            'Me.MSFlexGrid1.CellBackColor = &HC0FFC0
        Else
        ' modificando prueba nivel
            If cole = "SNB001" Then
                Me.MSFlexGrid1.Col = celPnivel(I) - 1 '+ 1 -1
                Me.MSFlexGrid1.Row = alumno
                Me.MSFlexGrid1.CellBackColor = &HC0FFFF
                Me.MSFlexGrid1.Col = celPnivel(I) + 1
                Me.MSFlexGrid1.Row = alumno
                Me.MSFlexGrid1.CellBackColor = &HFFFF80
            Else
                Me.MSFlexGrid1.Col = celPnivel(I)
                Me.MSFlexGrid1.Row = alumno
'                Me.MSFlexGrid1.CellBackColor = &HC0FFFF
                Me.MSFlexGrid1.Col = celPnivel(I) - 1
                Me.MSFlexGrid1.Row = alumno
'               Me.MSFlexGrid1.CellBackColor = &HC0FFC0
            End If
        End If
    Next I
    For I = 1 To (PE * NEva) - 1 Step 2
        If cole = "HUE001" Or cole = "ALT001" Then
            Me.MSFlexGrid1.Col = celPnivel(I) - 1
            Me.MSFlexGrid1.Row = alumno
            'Me.MSFlexGrid1.CellBackColor = &HC0FFC0
        
            Me.MSFlexGrid1.Col = celPnivel(I) + 1    ' + 2
            Me.MSFlexGrid1.Row = alumno
            ''''''''''''''''''Me.MSFlexGrid1.CellBackColor = &HC0FFFF ''''' Huechuraba Examen
        Else
        'modificando prueba de nivel
            If cole = "SNB001" Then
                Me.MSFlexGrid1.Col = celPnivel(I) + 1
                Me.MSFlexGrid1.Row = alumno
                Me.MSFlexGrid1.CellBackColor = &HFFFF80
        
                Me.MSFlexGrid1.Col = celPnivel(I) - 1 ' + 1    ' + 2
                Me.MSFlexGrid1.Row = alumno
                Me.MSFlexGrid1.CellBackColor = &HC0FFFF
            Else
                Me.MSFlexGrid1.Col = celPnivel(I) - 1
                Me.MSFlexGrid1.Row = alumno
'               Me.MSFlexGrid1.CellBackColor = &HC0FFC0
            
                Me.MSFlexGrid1.Col = celPnivel(I)
                Me.MSFlexGrid1.Row = alumno
'               Me.MSFlexGrid1.CellBackColor = &HC0FFFF
            End If
        End If
        
    Next I
    MSFlexGrid1.Col = 0
    MSFlexGrid1.Row = alumno
    MSFlexGrid1.CellAlignment = 1

    MSFlexGrid1.TextMatrix(alumno, 0) = rs(3) & " | " & Format(alumno, "00") & " | " & rs(0) & " " & rs(1) & " " & rs(2)
    rs.MoveNext
Next alumno
End If
rs.Close

'
'If Me.Cmb_Let_Cur <> "" Then
'    Cmb_Sector.ComboItems.Clear
'    Cmb_Asi.Clear
'
'    If CodPerfil = "#PJE#" Then
'    rs.Open "SELECT DISTINCT s.nom_sec, ap.cod_sec" _
'          & " FROM sectores s, asi_x_cur_prof ap, cursos c" _
'          & " WHERE S.COD_SEC = AP.COD_SEC AND AP.cod_ens = '" & CodModalidad _
'          & "' and AP.num_gra = '" & Me.Cmb_Grado & "' and AP.let_cur ='" _
'          & Me.Cmb_Let_Cur & "' and ap.agno = " & cmb_agno.Text _
'          & " and ap.agno = c.agno and ap.num_gra = c.num_gra and" _
'          & " ap.let_cur = c.let_cur and ap.cod_ens = c.cod_ens and" _
'          & " c.agno = " & cmb_agno.Text & " and c.rut_prof = '" & RutUsuario & "' union " _
'          & "SELECT DISTINCT s.NOM_SEC, ap.COD_SEC FROM SECTORES S, asi_x_cur_prof AP WHERE S.COD_SEC = AP.COD_SEC AND AP.cod_ens = '" & CodModalidad & "' and AP.num_gra = '" & Me.Cmb_Grado & "' and AP.let_cur ='" & Me.Cmb_Let_Cur & "' and AP.rut_prof ='" & RutUsuario & "' and ap.agno = " & cmb_agno.Text, conn
'    Else
'        If CodPerfil = "#UTP#" Or CodPerfil = "#ADM#" Or CodPerfil = "#GES03#" Then
'            rs.Open "SELECT DISTINCT s.nom_sec, ap.cod_sec FROM sectores s, asi_x_cur_prof ap,subsectores sub" _
'            & " WHERE S.COD_SEC = sub.COD_SEC AND ap.cod_ens = sub.cod_ens AND ap.num_gra = sub.num_gra AND ap.cod_sec = sub.cod_sec AND ap.cod_asi = sub.cod_asi AND AP.cod_ens = '" & CodModalidad & "' and AP.num_gra = '" & Me.Cmb_Grado & "' and AP.let_cur ='" & Me.Cmb_Let_Cur & "' and ap.agno = " & cmb_agno.Text, conn
'        Else
'            rs.Open "SELECT DISTINCT s.NOM_SEC, ap.COD_SEC FROM SECTORES S, asi_x_cur_prof AP WHERE S.COD_SEC = AP.COD_SEC AND AP.cod_ens = '" & CodModalidad & "' and AP.num_gra = '" & Me.Cmb_Grado & "' and AP.let_cur ='" & Me.Cmb_Let_Cur & "' and AP.rut_prof ='" & RutUsuario & "' and ap.agno = " & cmb_agno.Text, conn
'        End If
'    End If
'
'End If
''******** Limpia grilla
'Call LimpiaNotas
''******** FIN Limpia grilla
'
'

End Sub


