VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{F39CBE5C-2D1B-11D6-8D4A-0050043A2E60}#2.0#0"; "ProgBarProzent.ocx"
Object = "{0ECD9B60-23AA-11D0-B351-00A0C9055D8E}#6.0#0"; "MSHFLXGD.OCX"
Begin VB.Form Form_Reserva 
   BorderStyle     =   3  'Fixed Dialog
   Caption         =   "Reserva de Matrículas"
   ClientHeight    =   6855
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   10245
   Icon            =   "Form_reserva.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MDIChild        =   -1  'True
   ScaleHeight     =   6855
   ScaleWidth      =   10245
   ShowInTaskbar   =   0   'False
   Begin VB.CommandButton Command2 
      Caption         =   "Cartas Alumnos No Reservados"
      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            =   8400
      TabIndex        =   40
      Top             =   1200
      Width           =   1575
   End
   Begin VB.CommandButton cmd_Ver 
      Caption         =   "Ver Todos"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   735
      Left            =   6840
      TabIndex        =   35
      Top             =   600
      Visible         =   0   'False
      Width           =   975
   End
   Begin VB.Frame Frame6 
      Caption         =   "Acción"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00C00000&
      Height          =   1335
      Left            =   240
      TabIndex        =   24
      Top             =   240
      Width           =   1335
      Begin VB.OptionButton Option1 
         Caption         =   "Reportes"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   255
         Index           =   1
         Left            =   120
         TabIndex        =   26
         Top             =   720
         Width           =   975
      End
      Begin VB.OptionButton Option1 
         Caption         =   "Reservas"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   375
         Index           =   0
         Left            =   120
         TabIndex        =   25
         Top             =   360
         Width           =   1095
      End
   End
   Begin VB.Frame Frame5 
      Caption         =   "Reserva para:"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00C00000&
      Height          =   3375
      Left            =   2760
      TabIndex        =   15
      Top             =   2760
      Visible         =   0   'False
      Width           =   4575
      Begin VB.TextBox Text3 
         Appearance      =   0  'Flat
         BackColor       =   &H8000000F&
         BorderStyle     =   0  'None
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00C00000&
         Height          =   285
         Left            =   240
         Locked          =   -1  'True
         TabIndex        =   23
         Text            =   "Text3"
         Top             =   600
         Width           =   1935
      End
      Begin VB.TextBox Text2 
         Appearance      =   0  'Flat
         BackColor       =   &H8000000F&
         BorderStyle     =   0  'None
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H00C00000&
         Height          =   285
         Left            =   240
         Locked          =   -1  'True
         TabIndex        =   22
         Text            =   "Text2"
         Top             =   360
         Width           =   4095
      End
      Begin VB.CommandButton Command4 
         Caption         =   "Cancelar"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   495
         Left            =   2640
         TabIndex        =   21
         Top             =   2520
         Width           =   1455
      End
      Begin VB.CommandButton Command3 
         Caption         =   "Modificar"
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   495
         Left            =   480
         TabIndex        =   20
         Top             =   2520
         Width           =   1455
      End
      Begin VB.TextBox Text1 
         BeginProperty Font 
            Name            =   "Arial"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   735
         Left            =   240
         MultiLine       =   -1  'True
         ScrollBars      =   2  'Vertical
         TabIndex        =   18
         Text            =   "Form_reserva.frx":2A8B2
         Top             =   1680
         Width           =   4095
      End
      Begin MSComctlLib.ImageCombo ImageCombo1 
         Height          =   345
         Left            =   2400
         TabIndex        =   16
         Top             =   990
         Width           =   1935
         _ExtentX        =   3413
         _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
         Text            =   "ImageCombo1"
      End
      Begin VB.Label Label4 
         Caption         =   "Observación"
         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        =   19
         Top             =   1440
         Width           =   975
      End
      Begin VB.Label Label3 
         Caption         =   "Estado de la reserva"
         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        =   17
         Top             =   1080
         Width           =   1575
      End
   End
   Begin MSHierarchicalFlexGridLib.MSHFlexGrid MSHFlexGrid1 
      Height          =   3975
      Left            =   120
      TabIndex        =   14
      Top             =   2520
      Width           =   9975
      _ExtentX        =   17595
      _ExtentY        =   7011
      _Version        =   393216
      Cols            =   5
      FixedCols       =   3
      FocusRect       =   0
      FillStyle       =   1
      SelectionMode   =   1
      FormatString    =   "^RUN|<Nombre Alumno|^Teléfono|^Reserva|<Observación"
      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
      _NumberOfBands  =   1
      _Band(0).Cols   =   5
   End
   Begin ProgBarProzent.ProgBar ProgBar1 
      Height          =   375
      Left            =   8280
      TabIndex        =   10
      Top             =   2040
      Width           =   1815
      _ExtentX        =   3201
      _ExtentY        =   661
   End
   Begin VB.CommandButton Btn_Sal_Cap 
      Caption         =   "Salir"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   8400
      TabIndex        =   7
      Top             =   720
      Width           =   1575
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   8400
      TabIndex        =   0
      Top             =   240
      Visible         =   0   'False
      Width           =   1575
   End
   Begin MSComDlg.CommonDialog CmDialog1 
      Left            =   9600
      Top             =   480
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.Frame Frame1 
      ForeColor       =   &H00FF0000&
      Height          =   2295
      Left            =   120
      TabIndex        =   9
      Top             =   120
      Width           =   8055
      Begin VB.Frame frame_curso 
         Height          =   735
         Left            =   120
         TabIndex        =   27
         Top             =   1440
         Visible         =   0   'False
         Width           =   7815
         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            =   1560
            Style           =   2  'Dropdown List
            TabIndex        =   32
            Top             =   240
            Width           =   2475
         End
         Begin VB.ComboBox Cmb_Grado 
            Enabled         =   0   'False
            Height          =   315
            Left            =   4920
            Style           =   2  'Dropdown List
            TabIndex        =   30
            Top             =   240
            Width           =   615
         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            =   6960
            Style           =   2  'Dropdown List
            TabIndex        =   28
            Top             =   240
            Width           =   615
         End
         Begin VB.Label Tipo 
            Caption         =   "Tipo de Enseñanza"
            BeginProperty Font 
               Name            =   "Arial"
               Size            =   8.25
               Charset         =   0
               Weight          =   700
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            ForeColor       =   &H00C00000&
            Height          =   195
            Left            =   240
            TabIndex        =   33
            Top             =   240
            Width           =   1215
         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
            ForeColor       =   &H00000000&
            Height          =   195
            Left            =   4320
            TabIndex        =   31
            Top             =   240
            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
            ForeColor       =   &H00000000&
            Height          =   195
            Left            =   5880
            TabIndex        =   29
            Top             =   240
            Width           =   945
         End
      End
      Begin VB.Frame frame_reportes 
         Height          =   1335
         Left            =   1560
         TabIndex        =   11
         Top             =   120
         Visible         =   0   'False
         Width           =   6375
         Begin VB.Frame frame_pantalla 
            ForeColor       =   &H00FF0000&
            Height          =   1095
            Left            =   3720
            TabIndex        =   12
            Top             =   120
            Width           =   1335
            Begin VB.OptionButton OptDesImp 
               Caption         =   "Impresora"
               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        =   5
               Top             =   600
               Width           =   1095
            End
            Begin VB.OptionButton OptDesPan 
               Caption         =   "Pantalla"
               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        =   4
               Top             =   360
               Value           =   -1  'True
               Width           =   975
            End
         End
         Begin VB.CommandButton BtnGenRep 
            Caption         =   "Generar Reportes"
            BeginProperty Font 
               Name            =   "Arial"
               Size            =   8.25
               Charset         =   0
               Weight          =   400
               Underline       =   0   'False
               Italic          =   0   'False
               Strikethrough   =   0   'False
            EndProperty
            Height          =   735
            Left            =   5160
            TabIndex        =   6
            Top             =   360
            Visible         =   0   'False
            Width           =   975
         End
         Begin VB.Frame Frame2 
            ForeColor       =   &H00FF0000&
            Height          =   1095
            Left            =   120
            TabIndex        =   13
            Top             =   120
            Width           =   1575
            Begin VB.OptionButton OptTod 
               Caption         =   "Todos"
               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        =   2
               Top             =   480
               Width           =   855
            End
            Begin VB.OptionButton OptOptUno 
               Caption         =   "Un Curso"
               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        =   1
               Top             =   240
               Value           =   -1  'True
               Width           =   1095
            End
            Begin VB.OptionButton OptOptEst 
               Caption         =   "Estadistica"
               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        =   3
               Top             =   720
               Width           =   1095
            End
         End
         Begin VB.Frame Frame8 
            Height          =   1095
            Left            =   1800
            TabIndex        =   34
            Top             =   120
            Width           =   1815
            Begin VB.OptionButton opt_Scon 
               Caption         =   "Sin confirmar"
               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        =   39
               Top             =   120
               Width           =   1575
            End
            Begin VB.OptionButton opt_Todos 
               Caption         =   "Todos"
               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        =   38
               Top             =   840
               Width           =   1455
            End
            Begin VB.OptionButton opt_Cres 
               Caption         =   "Reserva"
               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        =   37
               Top             =   600
               Width           =   1455
            End
            Begin VB.OptionButton opt_Sres 
               Caption         =   "Renuncia"
               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        =   36
               Top             =   360
               Width           =   1575
            End
         End
      End
   End
   Begin VB.Label lbl_Num_Alu 
      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        =   8
      Top             =   6600
      Width           =   2055
   End
End
Attribute VB_Name = "Form_Reserva"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim Modificado As Boolean
Dim rs As ADODB.Recordset

Public Sub GenRep()
On Error GoTo controler
'**************************************
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
a = "select nom_est, dir_est, localidad_est from establecimientos"
rs.Open a, conn
If rs.RecordCount <> 0 Then
Form_main.CrystalReport.Formulas(0) = "NomCol = '" & rs(0) & "'"
Form_main.CrystalReport.Formulas(1) = "DirCol = '" & rs(1) & "'"
Form_main.CrystalReport.Formulas(2) = "ComunaCol = '" & rs(2) & "'"
End If
rs.Close
CodModalidad = devuelvecampo("codigos_ensenanza", "", "cod_ens", "des_ens = '" & Me.Cmb_Cod_ens.Text & "'", conn)
cod_des = devuelvecampo("codigos_ensenanza", "", "cod_des", "cod_ens ='" & CodModalidad & "'", conn)
Form_main.CrystalReport.WindowTitle = "Estadística de Reservas"
Form_main.CrystalReport.Formulas(3) = "TipEns = '" & Me.Cmb_Cod_ens & "'"

If cod_des = "K" Or cod_des = "PK" Then
    Form_main.CrystalReport.Formulas(4) = "Curso = '" & cod_des & " " & Me.Cmb_let_cur & "'"
Else
    Form_main.CrystalReport.Formulas(4) = "Curso = '" & Me.Cmb_Grado & " " & Me.Cmb_let_cur & "'"
End If
Form_main.CrystalReport.Formulas(5) = "Hora = '" & Time() & "'"
Form_main.CrystalReport.DataFiles(0) = DirAplicacion & "\rpt\GesTemp.mdb"
Form_main.CrystalReport.Action = 1
Call LimpiaFormulas(Form_main.CrystalReport, 30)
'******************************************
Exit Sub
controler:
    MsgBox Err.Number & vbCrLf & vbCrLf & Err.Description & vbCrLf & vbCrLf & Err.Source, vbExclamation, "ALERTA DE ANOMALIA"
    Resume Next
End Sub


Private Sub Btn_Sal_Cap_Click()
Unload Me
End Sub

Private Sub BtnGenRep_Click()

If Me.OptOptUno.Value = True Then
        Call GrabaMatriz
        Form_main.CrystalReport.ReportFileName = DirAplicacion & "\rpt\reservas.rpt"
        If Me.opt_Todos.Value = 1 Then
            Form_main.CrystalReport.ReplaceSelectionFormula ("")
        Else
            If Me.opt_Cres.Value = 1 Or Me.opt_Sres.Value = 1 Then
                If Me.opt_Cres.Value = 1 Then Form_main.CrystalReport.ReplaceSelectionFormula ("{RESERVAS.C4} = " & """" & "S" & """")
                If Me.opt_Sres.Value = 1 Then Form_main.CrystalReport.ReplaceSelectionFormula ("{RESERVAS.C4} = " & """" & "N" & """" & " or {RESERVAS.C4} = " & """" & "R" & """")
            Else
                Form_main.CrystalReport.ReplaceSelectionFormula ("")
            End If
        End If
        Call GenRep
End If

If Me.OptOptEst.Value = True Then
    Call GrabaMatrizEst
    Form_main.CrystalReport.ReportFileName = DirAplicacion & "\rpt\reservas_est.rpt"
    Form_main.CrystalReport.ReplaceSelectionFormula ("")
    Call GenRep
End If

End Sub

Private Sub Cmb_Cod_ens_Click()

If Len(Me.Cmb_Cod_ens) <> 0 Then
Setea_Grilla_Reserva
 nivel = devuelvecampo("codigos_ensenanza", "des_ens", "num_gra_max", "des_ens = " & "'" & Me.Cmb_Cod_ens.Text & "'", conn)
 Cmb_Grado.Enabled = True
 Cmb_Grado.Clear
 Me.ProgBar1.ShowProgress 0
 Me.BtnGenRep.Enabled = False
 For t = 1 To Val(nivel)
    Cmb_Grado.AddItem t
 Next t
End If
Command2.Enabled = False
End Sub

Private Sub Cmb_Grado_Click()
Setea_Grilla_Reserva
CodModalidad = devuelvecampo("codigos_ensenanza", "", "cod_ens", "des_ens = '" & Me.Cmb_Cod_ens.Text & "'", conn)
Ngrado = Me.Cmb_Grado.Text
Cmb_let_cur.Clear
Me.ProgBar1.ShowProgress 0
Me.BtnGenRep.Enabled = False
Call llenacombo(Cmb_let_cur, "cursos", "let_cur", "where cod_ens = '" & CodModalidad & "' and num_gra = '" & Ngrado & "' and agno=" & Anno, conn)
Me.Cmb_let_cur.AddItem "Todos", 0
Me.Cmb_let_cur.Enabled = True
Command2.Enabled = False
End Sub

Private Sub Cmb_let_cur_Click()
  Call Command1_Click
  Command2.Enabled = True
End Sub

Private Sub cmd_ver_Click()
    todos_los_cursos
End Sub

Private Sub Command1_Click()
Dim rs As ADODB.Recordset
Dim nombre As String
Dim Telefono As String
Dim Observacion As String
Dim Reserva, Filtro As String

Setea_Grilla_Reserva

CodModalidad = devuelvecampo("codigos_ensenanza", "", "cod_ens", "des_ens = '" & Me.Cmb_Cod_ens.Text & "'", conn)

Set rs = New ADODB.Recordset
If Option1(0).Value = True Then
a = "SELECT P.CI_ALU, P.NOM_ALU, P.APE_PAT_ALU, P.APE_MAT_ALU, AC.FEC_MAT, M.RESERVA, M.OBS_RES, P.FON_ALU, M.NUM_MAT " & _
    "FROM POSTULANTES P, ALU_X_CUR AC, MATRICULAS M " & _
    "WHERE P.CI_ALU = M.CI_ALU AND M.CI_ALU = AC.CI_ALU AND M.ACTIVO_ALU = 'SI' AND P.CI_ALU = AC.CI_ALU AND AC.COD_ENS ='" & CodModalidad & "' AND " & _
    "AC.NUM_GRA ='" & Me.Cmb_Grado & "' AND " & _
    "AC.ACTIVO_ALU ='SI' AND " & _
    "AC.fec_cam ='1111-11-11' AND " & _
    IIf(Me.Cmb_let_cur.Text = "Todos", "", "AC.LET_CUR ='" & Me.Cmb_let_cur & "' AND ") & _
    "AC.AGNO=" & Anno & " AND M.ANNO_MAT+0=AC.AGNO " & _
    "ORDER BY APE_PAT_ALU ASC, APE_MAT_ALU ASC, NOM_ALU ASC"
Else

If opt_Scon.Value = True Then
    Filtro = " and reserva = '0' "
End If
If opt_Cres.Value = True Then
    Filtro = " and reserva = '1' "
End If
If opt_Sres.Value = True Then
    Filtro = " and reserva = '2' "
End If
If opt_Todos.Value = True Then
    Filtro = " "
End If
If Filtro = "" Then
    MsgBox "Debe seleccionar una opción", vbInformation
    Exit Sub
End If
a = "SELECT P.CI_ALU, P.NOM_ALU, P.APE_PAT_ALU, P.APE_MAT_ALU, AC.FEC_MAT, M.RESERVA, M.OBS_RES, P.FON_ALU, M.NUM_MAT " & _
    "FROM POSTULANTES P, ALU_X_CUR AC, MATRICULAS M " & _
    "WHERE P.CI_ALU = M.CI_ALU AND M.CI_ALU = AC.CI_ALU AND M.ACTIVO_ALU = 'SI' AND P.CI_ALU = AC.CI_ALU AND AC.COD_ENS ='" & CodModalidad & "' AND " & _
    "AC.NUM_GRA ='" & Me.Cmb_Grado & "' AND " & _
    "AC.ACTIVO_ALU ='SI' AND " & _
    "AC.fec_cam ='1111-11-11' AND " & _
    IIf(Me.Cmb_let_cur.Text = "Todos", "", "AC.LET_CUR ='" & Me.Cmb_let_cur & "' AND ") & _
    "AC.AGNO=" & Anno & " AND M.ANNO_MAT+0=AC.AGNO" & Filtro & _
    "ORDER BY APE_PAT_ALU ASC, APE_MAT_ALU ASC, NOM_ALU ASC"
End If
   
rs.Open a, conn
'MsgBox rs.RecordCount
If rs.RecordCount > 0 Then

'***********************************************************************
'* inicializa la barra de progreso                                     *
'***********************************************************************
nmax = rs.RecordCount
  With ProgBar1
    .ShowProgress 0
    .MaxValue = nmax
    .Richtung = mDir
    .ProgressStyle = mStyle
    .ShowProzent = 1
    .Zusatztext = "%"
    .ShowProgress 0
  End With
'***********************************************************************
     rs.MoveFirst
     
     For i = 1 To rs.RecordCount
       nombre = rs("APE_PAT_ALU") & "  " & rs("APE_MAT_ALU") & "  " & rs("NOM_ALU")
       If IsNull(rs!fon_alu) Then
            Telefono = ""
            Else
            Telefono = rs!fon_alu
       End If
       
       If IsNull(rs!Reserva) Or rs!Reserva = 0 Then
            Reserva = "N"
       ElseIf rs!Reserva = 1 Then
            Reserva = "S"
       ElseIf rs!Reserva = 2 Then
            Reserva = "R"
       End If

       If IsNull(rs!obs_res) Then
            Observacion = ""
            Else
            Observacion = rs!obs_res
       End If
       
       Me.MSHFlexGrid1.AddItem rs!ci_alu & Chr(9) & nombre & Chr(9) & Telefono & Chr(9) & Reserva & Chr(9) & Observacion & Chr(9) & rs!Reserva & Chr(9) & rs!fec_mat & Chr(9) & rs!Num_MAT, i
       
       rs.MoveNext
       ProgBar1.ShowProgress i
       'refrescar la grilla
    Next i
    'ProgBar1.ShowProgress 100
    
Me.BtnGenRep.Enabled = True
Me.Btn_Sal_Cap.Enabled = True
Else
    MsgBox "La busqueda no arrojo resultados.", vbInformation, "Sin resultados"
End If

If Option1(0).Value = True Then
    Me.lbl_Num_Alu.Caption = "Alumnos en el curso :" & rs.RecordCount
Else
    Me.lbl_Num_Alu.Caption = "Total de resultados :" & rs.RecordCount
End If

rs.Close
End Sub

Private Sub Command2_Click()
    Form_main.CrystalReport.ReportFileName = App.Path & "\rpt\cartasinreserva.rpt"
    Form_main.CrystalReport.WindowTitle = "Cartas Alumnos No Reservados, No Matriculados."
    LimpiaFormulas Form_main.CrystalReport, 20
    Form_main.CrystalReport.WindowState = crptMaximized
    Form_main.CrystalReport.ReplaceSelectionFormula ("{matriculas.activo_alu}='SI' and {matriculas.reserva}=0 and {matriculas.ANNO_MAT}='" & Anno & "' and {alu_x_cur.COD_ENS}='" & RetornaCampo("codigos_ensenanza", "cod_ens", "des_ens='" & Trim(Cmb_Cod_ens.Text) & "'", conn) & "' and {alu_x_cur.NUM_GRA}='" & Trim(Cmb_Grado.Text) & IIf(Trim(Cmb_let_cur.Text) = "Todos", "'", "' and {alu_x_cur.LET_CUR}='" & Trim(Cmb_let_cur.Text) & "'"))
    Form_main.CrystalReport.Destination = crptToWindow
    Form_main.CrystalReport.Action = 1
End Sub

Private Sub Command3_Click()
    Dim MyRow As Integer
    MyRow = Me.MSHFlexGrid1.Row
    
    Me.MSHFlexGrid1.TextMatrix(MyRow, 3) = Me.ImageCombo1.SelectedItem.Key
    Me.MSHFlexGrid1.TextMatrix(MyRow, 5) = Val(Me.ImageCombo1.SelectedItem.Index) - 1
    Me.MSHFlexGrid1.TextMatrix(MyRow, 4) = Me.Text1.Text
    grabar_reservas
    Command4_Click
End Sub

Private Sub Command4_Click()
    Frame5.Visible = False
    Frame1.Enabled = True
    Btn_Sal_Cap.Enabled = True
End Sub

Private Sub Form_Load()

Setea_Grilla_Reserva
Llena_Combo_Estado_Reserva

Modificado = False

CentrarForma Me
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
'********* Llena combos ****************
Call llenacombo(Form_Reserva.Cmb_Cod_ens, "codigos_ensenanza", "des_ens", "order by ord_ens", conn)

End Sub

Private Sub Llena_Combo_Estado_Reserva()
    Me.ImageCombo1.ComboItems.Add 1, "N", "No Confirmado"
    Me.ImageCombo1.ComboItems.Add 2, "S", "Si Reserva"
    Me.ImageCombo1.ComboItems.Add 3, "R", "Renuncia"
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call menuhabilitado(True)
End Sub

Private Sub MSHFlexGrid1_DblClick()
    Dim fec1 As Long
    Dim fec2 As Long
    Dim objRS As ADODB.Recordset
    Set objRS = New ADODB.Recordset
    Dim MyRow As Integer
    Dim EstadoPago As Boolean
    Dim MiColegio As String
    If Option1(1).Value Then
        Exit Sub
    End If
    
    'If Not (Me.OptOptUno.Value = True) Then Exit Sub
    If Not (CodPerfil = "#ADM#" Or CodPerfil = "#BCA#" Or CodPerfil = "#BEC#" Or CorPerfil = "#SBD#") Then Exit Sub
    
    MyRow = Me.MSHFlexGrid1.Row
    'Si es vacio no hacer nada
    If MSHFlexGrid1.TextMatrix(MyRow, 0) = "" Then Exit Sub
    'Verificar si esta al día.
    
    'Verifico si estoy en la fecha de valida de reservas
    var_sql = "select * from cfg" _
            & " where param = 'GEN_RES_MAT_READY' and agno = " & Anno
    objRS.Open var_sql, conn
    fec1 = CLng(Format(Now, "yyyymmdd"))
    fec2 = CLng(Format(objRS("valor"), "yyyymmdd"))
    If fec1 > fec2 Then
        MsgBox "No se pueden realizar reservas, el plazo termino el " & objRS("valor"), vbInformation, "Reserva de matriculas"
        Exit Sub
    End If
    objRS.Close
    
    EstadoPago = verifica_deuda_apoderado(Trim(MSHFlexGrid1.TextMatrix(MyRow, 0)), CInt(Anno), CLng(5000))
    'EstadoPago = devuelvecampo("fnza_detalle_mensualidad", "", "estado_dtll", " run_alu='" & Trim(MSHFlexGrid1.TextMatrix(MyRow, 0)) & "' AND fech_venc<'" & Format(Date, "yyyy-mm-dd") & "' ORDER BY agno DESC, mes DESC LIMIT 1;", conn)
    MiColegio = devuelvecampo("establecimientos", "", "RDB", "1=1", conn)
    If (EstadoPago = False And Not (MiColegio = "25709-5")) Then
        MsgBox "Alumno con deuda en el sistema.", vbExclamation, "Alumno con deuda"
     '   Exit Sub
    End If
    
    'Si el alumno tiene cheques protestados devuelve verdadero
'    If verifica_cheques_protestados(Trim(MSHFlexGrid1.TextMatrix(MyRow, 0))) Then
'        MsgBox "El alumno tiene cheques protestados.", vbExclamation, "No Puede Reservar"
'        Exit Sub
'    End If
        
    ' Llenado de Frame de Edicion.
    Text1.Enabled = True
    ImageCombo1.Enabled = True
    Command3.Enabled = True
    Set ImageCombo1.SelectedItem = ImageCombo1.ComboItems.Item(Val(MSHFlexGrid1.TextMatrix(MyRow, 5)) + 1)
    Text1.Text = MSHFlexGrid1.TextMatrix(MyRow, 4)
    Text2.Text = MSHFlexGrid1.TextMatrix(MyRow, 1)
    Text3.Text = MSHFlexGrid1.TextMatrix(MyRow, 0)
    If (MSHFlexGrid1.TextMatrix(MyRow, 3) <> "N") And Not (CodPerfil = "#ADM#" Or CodPerfil = "#BEC#" Or CorPerfil = "#SBD#") Then
        Text1.Enabled = False
        ImageCombo1.Enabled = False
        Command3.Enabled = False
    End If
    Frame5.Visible = True
    Frame1.Enabled = False
    Btn_Sal_Cap.Enabled = False
End Sub

Private Sub Option1_Click(Index As Integer)
    limpia_grilla
    cmd_Ver.Visible = False
    If Option1(0).Value = True Then
        frame_curso.Visible = True
        frame_reportes.Visible = False
        'Cmb_Grado.Text = ""
        'Cmb_Let_Cur.Text = ""
        Cmb_Grado.Clear
        Cmb_let_cur.Clear
        Cmb_Cod_ens.Clear
        Call llenacombo(Form_Reserva.Cmb_Cod_ens, "codigos_ensenanza", "des_ens", "order by ord_ens", conn)
    Else
        Command2.Enabled = False
        frame_curso.Visible = False
        frame_reportes.Visible = True
        OptTod.Value = True
    End If
End Sub

Private Sub OptOptEst_Click()
    limpia_grilla
    activa_frames
    Frame8.Enabled = False
    BtnGenRep.Visible = True
    frame_pantalla.Visible = True
    estadistica
End Sub

Private Sub OptOptEst_GotFocus()
'************ SQL ESTADISTICA ***************
'* SELECT AC.COD_ENS, AC.NUM_GRA, AC.LET_CUR,MO.COD_DES, COUNT(M.RESERVA) AS MATRICULA, COUNT(M.RESERVA)-sum(M.RESERVA) AS SIN_RES, sum(M.RESERVA) AS CON_RES FROM POSTULANTES P, ALU_X_CUR AC, MATRICULAS M, CODIGOS_ENSENANZA MO WHERE P.CI_ALU = M.CI_ALU AND M.CI_ALU = AC.CI_ALU AND M.ACTIVO_ALU = 'SI' AND P.CI_ALU = AC.CI_ALU     AND AC.ACTIVO_ALU ='SI' AND AC.fec_cam ='1111-11-11' AND AC.COD_ENS=MO.COD_ENS   GROUP BY AC.COD_ENS, AC.NUM_GRA, AC.LET_CUR  ORDER BY AC.COD_ENS ASC, AC.NUM_GRA ASC, AC.LET_CUR ASC
'********************************************
If Me.OptOptEst.Value = True Then
    'Me.Cmb_Cod_ens.Enabled = False
    Me.Cmb_Grado.Enabled = False
    Me.Cmb_let_cur.Enabled = False
End If

End Sub

Private Sub OptOptUno_Click()
limpia_grilla
activa_frames
BtnGenRep.Visible = True
frame_curso.Visible = True
Frame8.Visible = True
frame_pantalla.Visible = True
opt_Scon.Visible = True
'Cmb_Grado.Text = ""
'Cmb_Let_Cur.Text = ""
Cmb_Grado.Clear
Cmb_let_cur.Clear
Cmb_Cod_ens.Clear
Call llenacombo(Form_Reserva.Cmb_Cod_ens, "codigos_ensenanza", "des_ens", "order by ord_ens", conn)

If Me.OptOptUno.Value = True Then
    
    Setea_Grilla_Reserva
End If

End Sub

Private Sub OptOptUno_GotFocus()
If Me.OptOptUno.Value = True Then
    Me.Cmb_Grado.Enabled = True
    Me.Cmb_let_cur.Enabled = True
End If
End Sub
Private Sub OptTod_Click()
    limpia_grilla
    activa_frames
    frame_pantalla.Enabled = False
    Frame8.Visible = True
    BtnGenRep.Enabled = False
    cmd_Ver.Visible = True
End Sub

Private Sub OptTod_DblClick()
    Call OptTod_Click
End Sub

Private Sub OptTod_GotFocus()
If Me.OptTod.Value = True Then
    'Me.Cmb_Cod_ens.Enabled = False
    Me.Cmb_Grado.Enabled = False
    Me.Cmb_let_cur.Enabled = False
End If

End Sub

Public Sub GrabaMatriz()
'***********************************************************************
'* Guarda matriz en base de datos temporal GesTem.mdb en el directorio *
'* de la aplicacion                                                    *
'***********************************************************************
Call CreaBD(DirAplicacion & "\rpt\GesTemp.mdb")
Call CreaTabla(DirAplicacion & "\rpt\GesTemp.mdb", "RESERVAS", str(Me.MSHFlexGrid1.Cols) & ",10")
Dim dbsTemp As Database
Dim rsTemp As Recordset
Dim CamposInsert, ValoresInsert As String
Set dbsTemp = OpenDatabase(DirAplicacion & "\rpt\GesTemp.mdb")
Set rsTemp = dbsTemp.OpenRecordset("RESERVAS", dbOpenDynaset)

ValoresInsert = ""
CamposInsert = ""


For i = 0 To Me.MSHFlexGrid1.Cols - 1
    CamposInsert = CamposInsert & "C" & i + 1 & ", "
Next i
CamposInsert = "(" & Mid(CamposInsert, 1, Len(CamposInsert) - 2) & ")"

For f = 1 To Me.MSHFlexGrid1.Rows - 1
        ValoresInsert = ""
        For i = 2 To Me.MSHFlexGrid1.Cols - 1
            If Len(Me.MSHFlexGrid1.TextMatrix(f, i)) = 0 Then
                contenido = "Null" & ", "
            Else
                contenido = "'" & Me.MSHFlexGrid1.TextMatrix(f, i) & "', "
            End If
            ValoresInsert = ValoresInsert & contenido
        Next i
        
        nombre = Me.MSHFlexGrid1.TextMatrix(f, 1)
        If nombre <> "" Then
            Pos = InStr(1, nombre, "'")
            If Pos <> 0 Then
                nombre1 = Mid(nombre, 1, Pos) & "'" & Mid(nombre, Pos + 1, Len(nombre))
                nombre = nombre1
            End If
        End If
        
        nombre1 = Replace(nombre, ",", " ")
        nombre1 = Replace(nombre, "  ", " ")
        nombre = nombre1
                  
        If Len(Me.MSHFlexGrid1.TextMatrix(f, 0)) > 0 Then
            rut = Me.MSHFlexGrid1.TextMatrix(f, 0)
        Else
            rut = "Null"
        End If
        
        ValoresInsert = "'" & rut & "', " & "'" & nombre & "', " & ValoresInsert
        ValoresInsert = "(" & Mid(ValoresInsert, 1, Len(ValoresInsert) - 2) & ")"
     
        dbsTemp.Execute "INSERT INTO RESERVAS" & CamposInsert & " values" & Trim(ValoresInsert)
Next f
rsTemp.Close
'Set rsTemp = Nothing
dbsTemp.Close
'Set dbsTemp = Nothing
End Sub

Public Sub GrabaMatrizEst()
'***********************************************************************
'* Guarda matriz en base de datos temporal GesTem.mdb en el directorio *
'* de la aplicacion                                                    *
'***********************************************************************
Call CreaBD(DirAplicacion & "\rpt\GesTemp.mdb")
Call CreaTabla(DirAplicacion & "\rpt\GesTemp.mdb", "RESERVAS_Est", "1,10-4,3")
Dim dbsTemp As Database
Dim rsTemp As Recordset
Dim CamposInsert, ValoresInsert As String
Set dbsTemp = OpenDatabase(DirAplicacion & "\rpt\GesTemp.mdb")
Set rsTemp = dbsTemp.OpenRecordset("RESERVAS_Est", dbOpenDynaset)

ValoresInsert = ""
CamposInsert = ""

For i = 0 To Me.MSHFlexGrid1.Cols - 1
    CamposInsert = CamposInsert & "C" & i + 1 & ", "
Next i
CamposInsert = "(" & Mid(CamposInsert, 1, Len(CamposInsert) - 2) & ")"
For f = 1 To Me.MSHFlexGrid1.Rows - 1
        ValoresInsert = ""

        For i = 0 To Me.MSHFlexGrid1.Cols - 1
            If Len(Me.MSHFlexGrid1.TextMatrix(f, i)) = 0 Then
                contenido = "Null" & ", "
            Else
                contenido = "'" & Me.MSHFlexGrid1.TextMatrix(f, i) & "', "
            End If
            ValoresInsert = ValoresInsert & contenido
        Next i
        
        ValoresInsert = "(" & Mid(ValoresInsert, 1, Len(ValoresInsert) - 2) & ")"
        
        dbsTemp.Execute "INSERT INTO RESERVAS_Est" & CamposInsert & " values" & Trim(ValoresInsert)
Next f
Set dbsTemp = Nothing
End Sub

Private Sub Setea_Grilla_Reserva()

MSHFlexGrid1.Clear
MSHFlexGrid1.Rows = 2
'******** Encabezado **********
MSHFlexGrid1.FormatString = "^RUN|<Nombre Alumno|^Teléfono|^Reserva|<Observación|>cod_reserva|>fec_mat|>num_mat"
MSHFlexGrid1.ColWidth(0) = 1000
MSHFlexGrid1.ColWidth(1) = 3700
MSHFlexGrid1.ColWidth(2) = 900
MSHFlexGrid1.ColWidth(4) = 4400
MSHFlexGrid1.ColWidth(5) = 0
MSHFlexGrid1.ColWidth(6) = 0
MSHFlexGrid1.ColWidth(7) = 0
'******** FIN Encabezado **********

End Sub

Private Sub estadistica()
Dim curso As String

If Me.OptOptEst.Value = True Then
  
With MSHFlexGrid1
    .Clear
    .Cols = 5
    .FormatString = "^Curso|^Matricula|^Sin Reserva|^Con Reserva|^Con Renuncia"
    .Rows = 2
    .FixedCols = 0
    .FixedRows = 1
    .ColWidth(0) = 1000 'Curso
    .ColWidth(1) = 1000 'Matricula
    .ColWidth(2) = 1500 's/n reserva
    .ColWidth(3) = 1500 'c/n reserva
    .ColWidth(4) = 1500 'c/n renuncia
End With

Set rs = New ADODB.Recordset

a = "SELECT AC.COD_ENS, AC.NUM_GRA, AC.LET_CUR, MO.COD_DES, COUNT(M.CI_ALU) AS MATRICULA, COUNT(M.CI_ALU)-sum(if((M.RESERVA=1 or M.RESERVA=2),1,0)) AS SIN_RES, sum(if(M.RESERVA=1,1,0)) AS CON_RES, sum(if(M.RESERVA=2,1,0)) AS CON_REN  " & _
    "FROM POSTULANTES P, ALU_X_CUR AC, MATRICULAS M, CODIGOS_ENSENANZA MO " & _
    "WHERE P.CI_ALU = M.CI_ALU AND M.CI_ALU = AC.CI_ALU AND M.ACTIVO_ALU = 'SI' AND " & _
    "P.CI_ALU = AC.CI_ALU AND AC.ACTIVO_ALU ='SI' AND AC.fec_cam ='1111-11-11' AND " & _
    "AC.COD_ENS=MO.COD_ENS AND AC.AGNO=M.ANNO_MAT and anno_mat = " & Anno & _
    " GROUP BY AC.COD_ENS, AC.NUM_GRA, AC.LET_CUR " & _
    "ORDER BY AC.COD_ENS ASC, AC.NUM_GRA ASC, AC.LET_CUR ASC"

rs.Open a, conn

If rs.RecordCount > 0 Then

'***********************************************************************
'* inicializa la barra de progreso                                     *
'***********************************************************************
nmax = rs.RecordCount
  With ProgBar1
    .ShowProgress 0
    .MaxValue = nmax
    .Richtung = mDir
    .ProgressStyle = mStyle
    .ShowProzent = 1
    .Zusatztext = "%"
    .ShowProgress 0
  End With
'***********************************************************************
   
     For i = 1 To rs.RecordCount
       If rs!cod_des = "K" Or rs!cod_des = "PK" Then
        curso = " " & rs!cod_des & " " & rs!Let_Cur
       Else
        curso = " " & rs!Num_Gra & " " & rs!Let_Cur & " - " & rs!cod_des
       End If
       
       Me.MSHFlexGrid1.AddItem curso & Chr(9) & rs!matricula & Chr(9) & rs!sin_res & Chr(9) & rs!con_res & Chr(9) & rs!con_ren, i
              
       rs.MoveNext
       ProgBar1.ShowProgress i
       DoEvents
    Next i
    
    ProgBar1.ShowProgress nmax
    
Me.BtnGenRep.Enabled = True
Me.Btn_Sal_Cap.Enabled = True

End If
rs.Close
End If
End Sub

Private Sub todos_los_cursos()
    Dim QRY As String
    Dim rs As ADODB.Recordset
    Dim curso As String

If Me.OptTod.Value = True Then
    
    With MSHFlexGrid1
        .Clear
        .Cols = 5
        .FormatString = ">Nº |^RUT|<Nombre|^Reserva|^ Curso |^Fecha Res.|<Observaciones"
        .Rows = 2
        .FixedCols = 0
        .FixedRows = 1
        .ColWidth(1) = 1000 'Rut
        .ColWidth(2) = 3000 'Nombre
        '.ColWidth(3) = 1000 'Reserva
        '.ColWidth(4) = 1000 'Curso Reservado
        .ColWidth(5) = 1000 'Fecha Reserva
        .ColWidth(6) = 4100 'Observaciones
    End With
    
    Set rs = New ADODB.Recordset
    If opt_Sres.Value = True Then
        QRY = "SELECT HR.*,concat(P.ape_pat_alu,' ',P.ape_mat_alu,' ',P.nom_alu) as nombre,  '' as cod_des  FROM hist_reservas as HR, postulantes as P WHERE HR.run_alu=P.ci_alu AND HR.estado='R' and agno = " & Anno & " ORDER BY nombre;"
    ElseIf opt_Cres = True Then
        QRY = "SELECT HR.*,concat(P.ape_pat_alu,' ',P.ape_mat_alu,' ',P.nom_alu) as nombre, CE.cod_des  FROM hist_reservas as HR, postulantes as P, codigos_ensenanza as CE WHERE HR.run_alu=P.ci_alu AND HR.cod_ens_reserva=CE.cod_ens and HR.estado = 'S' and agno = " & Anno & " ORDER BY CE.ord_ens, HR.num_gra_reserva, nombre;"
    Else
        If opt_Todos.Value = True Then
        QRY = "(SELECT HR.*,concat(P.ape_pat_alu,' ',P.ape_mat_alu,' ',P.nom_alu) as nombre, CE.cod_des  FROM hist_reservas as HR, postulantes as P, codigos_ensenanza as CE WHERE HR.run_alu=P.ci_alu AND HR.cod_ens_reserva=CE.cod_ens AND HR.estado='S' and agno = " & Anno & ") " & _
              "UNION " & _
              "(SELECT HR.*,concat(P.ape_pat_alu,' ',P.ape_mat_alu,' ',P.nom_alu) as nombre,  '' as cod_des  FROM hist_reservas as HR, postulantes as P WHERE HR.run_alu=P.ci_alu AND HR.estado='R' and agno = " & Anno & ") " & _
              "ORDER BY cod_ens_reserva, num_gra_reserva, nombre;"
        Else
            MsgBox "Debe seleccionar una opción", vbInformation
            Exit Sub
        End If
    End If
        
    rs.Open QRY, conn
    If rs.RecordCount = 0 Then
        MsgBox "La busqueda no arrojo resultados", vbInformation
    Else
        With ProgBar1
            .ShowProgress 0
            .MaxValue = rs.RecordCount
            .Richtung = mDir
            .ProgressStyle = mStyle
            .ShowProzent = 1
            .Zusatztext = "%"
            .ShowProgress 0
        End With
        
        For i = 1 To rs.RecordCount
            If rs!cod_des = "K" Or rs!cod_des = "PK" Then
                curso = " " & rs!cod_des
            Else
                curso = " " & rs!Num_Gra_reserva & "º " & rs!cod_des
            End If
            MSHFlexGrid1.AddItem i & Chr(9) & rs!run_alu & Chr(9) & rs!nombre & Chr(9) & rs!Estado & Chr(9) & curso & Chr(9) & rs!fecha_estado & Chr(9) & rs!Observacion, i
            
            ProgBar1.ShowProgress i
            rs.MoveNext
        Next i
        
        ProgBar1.ShowProgress rs.RecordCount
        rs.Close
    End If
End If
End Sub

Private Sub activa_frames()
    opt_Scon.Visible = False
    Frame8.Enabled = True
    frame_pantalla.Enabled = True
    BtnGenRep.Enabled = True
    frame_curso.Visible = False
    BtnGenRep.Visible = False
    cmd_Ver.Visible = False
    Frame8.Visible = False
    frame_pantalla.Visible = False
End Sub

Public Sub limpia_grilla()
    With MSHFlexGrid1
        .Clear
        .Cols = 5
        .FormatString = ">Nº |^RUT|<Nombre|^Reserva|^ Curso |^Fecha Res.|<Observaciones"
        .Rows = 2
        .FixedCols = 0
        .FixedRows = 1
        .ColWidth(1) = 1000 'Rut
        .ColWidth(2) = 3000 'Nombre
        '.ColWidth(3) = 1000 'Reserva
        '.ColWidth(4) = 1000 'Curso Reservado
        .ColWidth(5) = 1000 'Fecha Reserva
        .ColWidth(6) = 4100 'Observaciones
    End With
End Sub

Private Sub grabar_reservas()
On Error GoTo controler
Cod_Ens = devuelvecampo("CODIGOS_ENSENANZA", "", "COD_ENS", "DES_ENS ='" & Me.Cmb_Cod_ens & "'", conn)
Dim Reserva As Integer
Dim Affected As Integer
Dim CodEnsRes As String
Dim NumGraRes As Integer
Dim accion_realizada As String, var_sql As String
Dim log As Boolean
Dim objRS As ADODB.Recordset
Set objRS = New ADODB.Recordset

    With MSHFlexGrid1
                If .TextMatrix(.Row, 3) = "S" Then
                    Reserva = 1
                ElseIf .TextMatrix(.Row, 3) = "R" Then
                    Reserva = 2
                Else
                    Reserva = 0
                End If
                If .TextMatrix(.Row, 0) <> "" Then
                    conn.Execute "UPDATE MATRICULAS Set RESERVA =" & Reserva & ", OBS_RES ='" & .TextMatrix(.Row, 4) & "' WHERE CI_ALU ='" & .TextMatrix(.Row, 0) & "' AND FEC_MAT='" & Format(.TextMatrix(.Row, 6), "yyyy-mm-dd") & "';", Affected
                    If .TextMatrix(.Row, 3) <> "N" Then
                        If .TextMatrix(.Row, 3) = "R" Then
                            CodEnsRes = "none"
                            NumGraRes = 0
                        Else
                            Select Case Cod_Ens
                                Case "996"
                                        CodEnsRes = "010"
                                        NumGraRes = 1
                                Case "010"
                                        CodEnsRes = "110"
                                        NumGraRes = 1
                                Case "110"
                                        If Cmb_Grado.Text = "8" Then
                                            CodEnsRes = "310"
                                            NumGraRes = 1
                                        Else
                                            CodEnsRes = Cod_Ens
                                            NumGraRes = Val(Cmb_Grado.Text) + 1
                                        End If
                                Case "310"
                                        If Cmb_Grado.Text = "4" Then
                                            CodEnsRes = Cod_Ens
                                            NumGraRes = 4
                                        Else
                                            CodEnsRes = Cod_Ens
                                            NumGraRes = Val(Cmb_Grado.Text) + 1
                                        End If
                                Case Else
                                    Err.Raise vbObjectError + 1100, "Form_Reserva.Btn_Gra_cap_Click", "Tipo de enseñanza no valido o incorrecto"
                            End Select
                        End If
                        conn.Execute "REPLACE hist_reservas(run_alu,agno,agno_reserva,estado,fecha_estado,cod_ens_reserva,num_gra_reserva,observacion) VALUES ('" & Trim(.TextMatrix(.Row, 0)) & "','" & Year(.TextMatrix(.Row, 6)) & "','" & (Year(.TextMatrix(.Row, 6)) + 1) & "','" & Trim(.TextMatrix(.Row, 3)) & "',current_date,'" & CodEnsRes & "','" & NumGraRes & "','" & Trim(.TextMatrix(.Row, 4)) & "');", Affected
                    Else
                        'Inserta log en caso de que se pase a no confirmado
                        var_sql = "select * from hist_reservas where run_alu = '" & Trim(.TextMatrix(.Row, 0)) & "'"
                        objRS.Open var_sql, conn
                        accion_realizada = "La reserva para el alumno " & .TextMatrix(.Row, 1) _
                                & ", a sido cambiada al estado NO CONFIRMADO, para el año " & (CInt(Year(.TextMatrix(.Row, 6)) + 1))
                        If Not objRS.EOF Then
                            log = Inserta_Vitacora_Alumno(Trim(.TextMatrix(.Row, 0)), accion_realizada)
                        End If
                        objRS.Close
                        'Fin log
                        
                        conn.Execute "DELETE FROM hist_reservas WHERE run_alu='" & Trim(.TextMatrix(.Row, 0)) & "' AND agno=" & Year(.TextMatrix(.Row, 6)) & " LIMIT 1;", Affected
                        
                    End If
                End If
                ' Insertar Log de reservas.
'
                If .TextMatrix(.Row, 3) = "S" Then
                accion_realizada = "Se ha realizado una reserva para el alumno " & .TextMatrix(.Row, 1) _
                                & ", para el año " & (CInt(Year(.TextMatrix(.Row, 6)) + 1))
                End If
                If .TextMatrix(.Row, 3) = "R" Then
                accion_realizada = "El alumno " & .TextMatrix(.Row, 1) _
                                & ", a renunciado a la reserva de matricula," _
                                & " para el año " & (CInt(Year(.TextMatrix(.Row, 6)) + 1))
                End If
                                
                If .TextMatrix(.Row, 3) <> "N" Then
                    log = Inserta_Vitacora_Alumno(Trim(.TextMatrix(.Row, 0)), accion_realizada)
                End If
                ' Fin Log.
                
    End With
        MsgBox "Los datos han sido grabados satisfactoriamente", vbInformation + vbOKOnly, "Mensaje de Gescol"
        
        Me.BtnGenRep.Enabled = True
        Me.Btn_Sal_Cap.Enabled = True
        
Exit Sub
controler:
    MsgBox Err.Description & vbCrLf & vbCrLf & Err.Source, vbCritical + vbOKOnly, "Error número :" & Err.Number
    MsgBox "Los datos no fueron grabados.", vbCritical + vbOKOnly, "Mensaje de Gescol"
End Sub

Private Function verifica_pagos(rut As String, saldo As Long) As Boolean
    Dim objRS As ADODB.Recordset
    Set objRS = New ADODB.Recordset
    Dim var_sql As String
    Dim sw As Boolean
    
    'Verifico que el alumno no este en la lista negra
    var_sql = "SELECT * FROM fnza_lista_negra_alumnos" _
            & " where rut_alu = '" & rut & "'"
    objRS.Open var_sql, conn
    If Not objRS.EOF Then
        'MsgBox "Alumno esta en la lista negra", vbInformation
        verifica_pagos = False
        Exit Function
    End If
    objRS.Close
    
    'Verifico pagos de años anteriores
    var_sql = "select estado_dtll,mes" _
            & " From fnza_detalle_mensualidad" _
            & " where run_alu = '" & rut & "' and monto_tot_cuota > (monto_abono + " _
            & saldo & ") and agno < " & Anno
    objRS.Open var_sql, conn
    
    If Not objRS.EOF Then
        sw = False
    Else
        sw = True
    End If
    objRS.Close
    If sw = True Then
        var_sql = "select estado_dtll,mes" _
                & " From fnza_detalle_mensualidad" _
                & " where agno = " & Anno & " and run_alu = '" _
                & rut & "' and date_format(fech_venc,'%Y-%m-28') < date_format(current_date,'%Y-%m-%d')" _
                & " and monto_tot_cuota > (monto_abono + " & saldo & ")" _
                & " order by folio_dtll"

        objRS.Open var_sql, conn
        If Not objRS.EOF Then
            verifica_pagos = False
        Else
            verifica_pagos = True
        End If
        objRS.Close
    Else
        verifica_pagos = False
    End If
End Function

Private Function verifica_cheques_protestados(ci_alu As String) As Boolean
    'Si el alumno tiene cheques protestados devuelve verdadero
    Dim var_sql As String
    Dim Rut_apo As String
    Dim objRS As ADODB.Recordset
    Set objRS = New ADODB.Recordset
    var_sql = "select rut_apo from apo_x_alu where ci_alu = '" & ci_alu & "' limit 1"
    objRS.Open var_sql, conn
    Rut_apo = objRS("rut_apo")
    objRS.Close
    var_sql = "select * from fnza_cheques_recibidos" _
            & " where run_apo = '" & Rut_apo & "' and estado_chq = 'prto'"
    objRS.Open var_sql, conn
    If Not objRS.EOF Then
        verifica_cheques_protestados = True
    Else
        verifica_cheques_protestados = False
    End If
    objRS.Close
End Function

Private Function verifica_deuda_apoderado(rut As String, agno As Integer, saldo As Long) As Boolean
    Dim objRS As ADODB.Recordset
    Dim REC As ADODB.Recordset
    Dim var_sql As String
    Set objRS = New ADODB.Recordset
    Set REC = New ADODB.Recordset
    verifica_deuda_apoderado = True
    
    'Verifico que todos los alumnos del apoderado no tengan deuda
    var_sql = "select distinct axa.ci_alu,apo.rut_apo from" _
            & " (select * from apo_x_alu" _
            & " where ci_alu = '" & rut & "' and agno = " & agno & ")  as apo,apo_x_alu axa" _
            & " Where apo.Rut_apo = axa.Rut_apo"
    objRS.Open var_sql, conn
    If objRS.RecordCount > 0 Then
    'Verifico que el apoderado no este en la lista negra
    var_sql = "SELECT * FROM fnza_lista_negra_apoderados" _
            & " where rut_apo = '" & objRS("rut_apo") & "'"
    REC.Open var_sql, conn
    If Not REC.EOF Then
        'MsgBox "Apoderado esta en la lista negra", vbInformation
        verifica_deuda_apoderado = False
        Exit Function
    End If
    REC.Close
    
    While Not objRS.EOF
        If verifica_pagos(objRS("ci_alu"), saldo) Then
            verifica_deuda_apoderado = True
        Else
            verifica_deuda_apoderado = False
            Exit Function
        End If
        objRS.MoveNext
    Wend
    Else
        verifica_deuda_apoderado = True
    End If
    objRS.Close
End Function
