VERSION 5.00
Begin VB.Form frmConfNumeracion 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Inicializacion de Numeración de Comprobantes"
   ClientHeight    =   975
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   4605
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   975
   ScaleWidth      =   4605
   StartUpPosition =   1  'CenterOwner
   Begin VB.CommandButton cmdAceptar 
      Caption         =   "&Inicializar"
      Height          =   375
      Left            =   3135
      TabIndex        =   3
      Top             =   75
      Width           =   975
   End
   Begin VB.CommandButton cmdSalir 
      Cancel          =   -1  'True
      Caption         =   "&Salir"
      Height          =   360
      Left            =   3120
      TabIndex        =   2
      Top             =   510
      Width           =   975
   End
   Begin VB.Frame Frame1 
      Caption         =   "Número de Comprobante"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   825
      Left            =   60
      TabIndex        =   0
      Top             =   45
      Width           =   2730
      Begin VB.TextBox txtNumero 
         Height          =   375
         Left            =   930
         TabIndex        =   1
         Top             =   270
         Width           =   1080
      End
      Begin VB.Label Label1 
         Caption         =   "Número:"
         Height          =   255
         Left            =   165
         TabIndex        =   4
         Top             =   315
         Width           =   1620
      End
   End
End
Attribute VB_Name = "frmConfNumeracion"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdaceptar_Click()
    If EsValido = True Then
        InicializarNumeracion (val(Trim(Me.txtNumero.Text)))
    End If
End Sub

Private Function EsValido() As Boolean
    
   EsValido = True
    
   If Trim(Me.txtNumero.Text) = "" Then
        EsValido = False
        MsgBox "Debe de ingresar un número para inicializar el comprobante"
        Exit Function
   End If
   
   If val(Trim(Me.txtNumero.Text)) <= 0 Then
        EsValido = False
        MsgBox "El numero ingresado no puede ser menor o igual que cero"
        Exit Function
   End If
   
   
   If ExisteOperacion(Trim(Me.txtNumero.Text)) = True Then
        EsValido = False
        MsgBox "El número con el que desea inicializar es menor o igual que el maximo valor de los comprobantes existentes"
        Exit Function
   End If
    
    
End Function

Private Sub InicializarNumeracion(strCodigoOperacion As String)
 On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset
  Dim intCodigoMaximo As Long
  
  'Verificamos si existe el numero de comprobante anterior
  sSQL = " SELECT cOperCode " & _
         "   FROM Operacion " & _
         "  WHERE cOperCode = " & val(strCodigoOperacion) - 1
       
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  If ADORS.EOF Then     'No existe, entonces creamos uno ficticio
  
    sSQL = " INSERT INTO Operacion" & _
           "             (cOperCode)" & _
           "      VALUES ('" & val(strCodigoOperacion) - 1 & "')"
    If Not oDBAdmin.EjecutarSentencia(ADOCnn, sSQL) Then GoTo Salir
    
  End If
  
  MsgBox "Se inicializó la numeración de comprobantes en forma satisfactoria"
  
  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  Screen.MousePointer = vbDefault

Salir:
  Set ADORS = Nothing
  Screen.MousePointer = vbDefault

End Sub


Private Function ExisteOperacion(strCodigoOperacion As String) As Boolean
 On Error GoTo LineaError

  Screen.MousePointer = vbHourglass
  
  Dim sSQL As String
  Dim ADORS As ADODB.Recordset


  ExisteOperacion = False
  
  sSQL = " SELECT max(cOperCode) as Maximo" & _
         "   FROM Operacion "
  
  If Not oDBAdmin.EjecutarConsulta(ADOCnn, sSQL, ADORS) Then GoTo Salir
  
  If val("0" + LeerCampo(ADORS!Maximo)) >= val(strCodigoOperacion) Then
     ExisteOperacion = True
  
  End If
  
  GoTo Salir
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
  Screen.MousePointer = vbDefault

Salir:
  Set ADORS = Nothing
  Screen.MousePointer = vbDefault

End Function

Private Sub cmdsalir_Click()
    Unload Me
End Sub

Private Sub txtNumero_KeyPress(KeyAscii As Integer)
 On Error GoTo LineaError
  
  Call ValidKeyPress(KeyAscii, kVKP_NUMERIC_INTEGER_ONLY)
  Exit Sub
  
LineaError:
  MsgBox Err.Description, vbOKOnly + vbInformation
End Sub

