sábado, 25 de febrero de 2017

registro de datos con VBA



Diseño del formulario


Diseño en la hoja1



Private Sub CmdNuevo_Click()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
ComboBox1.Text = ""
Me.OptionButton1.Value = True
TextBox1.SetFocus
End Sub

Private Sub CmdGrabar_Click()
Dim xfil As Integer

If Len(TextBox1.Text) <> 11 Then
    MsgBox "Verificar el Nº de Ruc ", vbInformation, "Aviso !!!!"
    TextBox1.SetFocus
    Exit Sub
End If


Range("A6").Activate
xfil = ActiveCell.CurrentRegion.Rows.Count
ActiveCell.Offset(xfil, 0) = TextBox1.Text
ActiveCell.Offset(xfil, 1) = TextBox2.Text
ActiveCell.Offset(xfil, 2) = TextBox3.Text
ActiveCell.Offset(xfil, 3) = ComboBox1.Text
If CheckBox1.Value = True Then
   ActiveCell.Offset(xfil, 4) = "Lima"
Else
   ActiveCell.Offset(xfil, 4) = "Provincia"
End If

If OptionButton1.Value = True Then
   ActiveCell.Offset(xfil, 5) = "Bueno"
Else
   ActiveCell.Offset(xfil, 5) = "Excelente"
End If

ActiveCell.Offset(xfil, 6) = TextBox4.Text

End Sub

Private Sub CmdSalir_Click()
Unload Me
End Sub

Private Sub UserForm_Activate()
ComboBox1.RowSource = "ListaDistritos"
End Sub

domingo, 29 de enero de 2017

Programación Orientado a objetos con Visual Basic - Parte 2



Capa Presentación

Imports System.Data.SqlClient
Public Class UCvendedor
 
    Private Sub UCvendedor_Load(sender As Object, e As EventArgs) Handles Me.Load
        llenarCboVendedor()
    End Sub
    Private Sub llenarCbovendedor()
        Dim dtab As New DataTable
        Dim obj As New CapaNegocio.ClsCNmantenimiento
        dtab = obj.p_listaEmpleadoxcargo("03") ' 01 es el codigo de cargo de vendedor
        mostrarCbo(dtab, CboVendedor)
    End Sub
    Public ReadOnly Property idven() As String
        Get
            Return Me.CboVendedor.SelectedValue.ToString
        End Get
    End Property

End Class

Capa negocio

Public Function p_listaEmpleadoxcargo(wcar) As DataTable
        Return obj.p_listaempleadoxcargo(wcar)
End Function

Capa Data

Public Function p_listaempleadoxcargo(ByVal wcodcar As String) As DataTable
        Dim da As New SqlDataAdapter("Usp_lisEmpleadoxcargo", cnx.cn)
        da.SelectCommand.CommandType = CommandType.StoredProcedure
        'Parámetro para CodCargo
        Dim par As New SqlParameter("@codcar", SqlDbType.Char, 2)
        par.Direction = ParameterDirection.Input
        da.SelectCommand.Parameters.Add(par)
        par.Value = wcodcar
        'Rellena el DataTable dt
        da.Fill(dt)
        Return dt
  End Function

ClsConexion
Imports System.Data.SqlClient
Public Class ClsConexion
    Private _cn As New SqlConnection("Server=(Local);DataBase=BDElectro;Integrated security =False; user=sa;pwd=123qwe$;")
    'Propiedad de solo lectura
    Public ReadOnly Property cn() As SqlConnection
        Get
            Return _cn
        End Get
    End Property

End Class


jueves, 26 de enero de 2017

creando juego con programacion vba



Programando el evento KeyUp del UserForm1(Formulario)

Private Sub UserForm_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

'Select Case - End Select : Una estructura de programación
'if Then - Else End If : Estructura de prog. condicional

Select Case KeyCode
    Case 36 'Tecla Inicio
        Image3.Top = 113.25
        Image3.Left = 206.3
    Case 38 ' Tecla flecha Arriba
        If Image3.Top <= 0.05 Then
           Image3.Top = 0.05
        Else
           Image3.Top = Image3.Top - 10
        End If
    Case 40 'Tecla flecha abajo
        If Image3.Top >= 231.75 Then
           Image3.Top = 231.75
        Else
           Image3.Top = Image3.Top + 10
        End If

    Case 37 'Izquierda
        Image3.Left = Image3.Left - 10
    Case 39 'Derecha
        Image3.Left = Image3.Left + 10
End Select

End Sub