Sie sind auf Seite 1von 5

Mantenimiento clientes(frmManteClientes.

vb)

Imports System.Data.SqlClient
Public Class frmManteClientes
'Declaracion de variables para el manejo de Tablas
Public oConexion As SqlConnection
Public oDataAdapter As SqlDataAdapter
Public oDataSet As DataSet
Public oCommand As SqlCommand
Public StrSql As String
Dim Rpta As Integer
Dim Conta As Integer = 0
Public Operacion As String
Public Sub ConectarBase()
Try
'Crear la Conexion
oConexion = New SqlConnection
oConexion.ConnectionString = "Data Source=(local);Initial
Catalog=bdBancos;User ID=sa;Password=clavesql"
oConexion.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End
End Try
oConexion.Close()
End Sub

Public Sub LimpiarTextos()


'Recorremos todos los controles del formulario que enviamos
For Each control As Control In Me.Controls
'Filtramos solo aquellos de tipo TextBox
If TypeOf control Is TextBox Then
control.Text = ""
End If

Next
End Sub

Docente: Victor M. Chapilliquen Estrada vchapilliquen@iappiura.edu.pe Pág. 1


Public Sub LimpiarTextosGroup()
' recorrer todos los controles del formulario indicado
For Each caja As Control In Me.grbDatos.Controls
If TypeOf caja Is TextBox Then
caja.Text = ""
End If
Next
End Sub

Public Sub EstadoBotones(ByVal Estado As Boolean)


btnNuevo.Enabled = Estado
btnGrabar.Enabled = Not Estado
btnCancelar.Enabled = Not Estado
btnEditar.Enabled = Estado
btnEliminar.Enabled = Estado
btnSalir.Enabled = Estado

End Sub

Public Sub EstadoTextos(ByVal Estado As Boolean)


' recorrer todos los controles del formulario indicado
For Each caja As Control In Me.grbDatos.Controls
If TypeOf caja Is TextBox Then
caja.Enabled = Estado
End If

If TypeOf caja Is ComboBox Then


caja.Enabled = Estado
End If

Next
grbBuscar.Enabled = Not Estado
End Sub

Private Sub frmManteClientes_Load(sender As Object, e As EventArgs) Handles MyBase.Load


EstadoBotones(True)
EstadoTextos(False)
LimpiarTextosGroup()

End Sub

Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnNuevo.Click
Operacion = "N"
LimpiarTextosGroup()
EstadoBotones(False)
EstadoTextos(True)
txtBus.Enabled = False
txtCod.Focus()
End Sub

Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnEditar.Click
Operacion = "M"
EstadoBotones(False)
EstadoTextos(True)
txtCod.Enabled = False
txtBus.Enabled = False
txtApeNom.Focus()
End Sub

Docente: Victor M. Chapilliquen Estrada vchapilliquen@iappiura.edu.pe Pág. 2


Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnCancelar.Click
Dim Rpta As Integer
Rpta = MessageBox.Show("Deseas Cancelar la Operacion", "Registro Clientes",
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2)
If Rpta = vbYes Then
EstadoBotones(True)
EstadoTextos(False)
LimpiarTextosGroup()
txtBus.Enabled = True
txtBus.Text = ""
End If
End Sub

Private Sub btnSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnSalir.Click
Dim Rpta As Integer
Rpta = MessageBox.Show("Deseas Salir", "Registro Clientes", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
If Rpta = vbYes Then
Me.Close()
End If
End Sub

Private Sub btnGrabar_Click(sender As Object, e As EventArgs) Handles btnGrabar.Click


Dim Rpta, N As Integer
Dim EstaCli As String = "A"
ConectarBase()
Try
If oConexion.State = ConnectionState.Open Then
oConexion.Close()
End If
Rpta = MessageBox.Show("Deseas guardar..?", "Registro Clientes",
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1)
If Rpta = vbYes Then
oConexion.Open()
If Operacion = "M" Then
StrSql = "Update Clientes Set " & _
"cli_Nombre='" & txtApeNom.Text & "'," & _
"cli_TelFijo='" & txtFijo.Text & "'," & _
"cli_TelMovil='" & txtMovil.Text & "'," & _
"cli_RucDNI='" & txtCod.Text & "'," & _
"cli_Direccion='" & txtDir.Text & "'," & _
"cli_ClaveAcceso='" & txtClaAcce.Text & "'," & _
"cli_ClaveDigital='" & txtClaDigi.Text & "'," & _
"cli_Email='" & txtEmail.Text & "'," & _
"cli_Rpm='" & txtRpm.Text & "'," & _
"cli_Rpc='" & txtRpc.Text & "'" & _
" where cli_RucDNI='" & txtCod.Text & "' and cli_Esta='A'"

End If

Docente: Victor M. Chapilliquen Estrada vchapilliquen@iappiura.edu.pe Pág. 3


If Operacion = "N" Then
StrSql = "Insert Into Clientes values(" & _
"'" & txtApeNom.Text & "'," & _
"'" & txtFijo.Text & "'," & _
"'" & txtMovil.Text & "'," & _
"'" & txtCod.Text & "'," & _
"'" & txtDir.Text & "'," & _
"'" & txtClaAcce.Text & "'," & _
"'" & txtClaDigi.Text & "'," & _
"'" & txtEmail.Text & "'," & _
"'" & txtRpm.Text & "'," & _
"'" & txtRpc.Text & "'," & _
"''," & _
"''," & _
"''," & _
"'" & EstaCli & "')"

End If
oCommand = New SqlCommand(StrSql, oConexion)
N = oCommand.ExecuteNonQuery()
If N > 0 Then
MsgBox("Registro Grabado", MsgBoxStyle.Information, "Registro Clientes")
EstadoBotones(True)
EstadoTextos(False)
txtBus.Enabled = True

End If
oConexion.Close()
End If

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub btnEliminar_Click(sender As Object, e As EventArgs) Handles btnEliminar.Click


Dim Rpta As Integer
Dim xCod, Ver As String
Dim N As Integer
ConectarBase()
Ver = Trim(txtApeNom.Text)
xCod = Trim(txtCod.Text)
Rpta = MessageBox.Show("Deseas Elimimnar a: " & Ver & " Seleccionado..?",
"Registro Clientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2)
If Rpta = vbYes Then
If oConexion.State = ConnectionState.Open Then
oConexion.Close()
End If
oConexion.Open()
StrSql = "Delete from Clientes Where Cli_RucDni='" & xCod & "' and cli_Esta='A'"
'StrSql = "Update Clientes Set cli_Esta='D' Where cli_RucDni='" & xCod & "'"
oCommand = New SqlCommand(StrSql, oConexion)
N = oCommand.ExecuteNonQuery()
If N > 0 Then
MsgBox("Registro Eliminado", MsgBoxStyle.Information, "Registro Clientes")
EstadoBotones(True)
LimpiarTextosGroup()
txtBus.Text = ""
End If
End If
End Sub

Docente: Victor M. Chapilliquen Estrada vchapilliquen@iappiura.edu.pe Pág. 4


Private Sub txtBus_TextChanged(sender As Object, e As EventArgs) Handles
txtBus.TextChanged
'Para Marcar el Valor enviado
Dim can As Integer
Dim xBus As String

xBus = Trim(txtBus.Text)
If Len(xBus) > 0 Then

If rbBus1.Checked = True Then


StrSql = "Select * from Clientes Where cli_Nombre like '" & xBus & "%' and
cli_Esta='A'"
ElseIf rbBus2.Checked = True Then
StrSql = "Select * from Clientes Where cli_RucDni like '" & xBus & "%' and
cli_Esta='A'"
End If
Else
StrSql = ""
End If
If StrSql = "" Then
LimpiarTextosGroup()
Else
ConectarBase()
oCommand = New SqlCommand(StrSql, oConexion)
oDataAdapter = New SqlDataAdapter(oCommand)
oDataSet = New DataSet
oDataAdapter.Fill(oDataSet)
can = oDataSet.Tables(0).Rows.Count
If can > 0 Then
txtCod.Text = oDataSet.Tables(0).Rows(0)("cli_RucDNI").ToString()
txtApeNom.Text = oDataSet.Tables(0).Rows(0)("cli_Nombre").ToString()
txtDir.Text = oDataSet.Tables(0).Rows(0)("cli_Direccion").ToString()
txtFijo.Text = oDataSet.Tables(0).Rows(0)("cli_TelFijo").ToString()
txtMovil.Text = oDataSet.Tables(0).Rows(0)("cli_TelMovil").ToString()
txtRpm.Text = oDataSet.Tables(0).Rows(0)("cli_Rpm").ToString()
txtRpc.Text = oDataSet.Tables(0).Rows(0)("cli_Rpc").ToString()
txtClaAcce.Text = oDataSet.Tables(0).Rows(0)("cli_ClaveAcceso").ToString()
txtClaDigi.Text = oDataSet.Tables(0).Rows(0)("cli_ClaveDigital").ToString()
txtEmail.Text = oDataSet.Tables(0).Rows(0)("cli_Email").ToString()
Else
LimpiarTextosGroup()
End If

End If
End Sub

Docente: Victor M. Chapilliquen Estrada vchapilliquen@iappiura.edu.pe Pág. 5

Das könnte Ihnen auch gefallen