You are on page 1of 11

MANTEMIENTO DEL CLIENTE

Imports System.Data.SqlClient
• Permite trabajar con los controles de control
de sql server de la tabla cliente.
Private oDataAdapter As SqlDataAdapter
Private oDataTable As DataTable
Private oConnection As SqlConnection
Private oDataRow As DataRow
Private oCommandBuilder As SqlCommandBuilder
Private PosFilaAct As Integer
Private vNuevo As Boolean

• Esto permite declarar las variables a utilizar


para el control de datos de tablas y registros
de SQL Server.
Private Sub Activa_Desactiva(ByVal sw As Boolean)
gbdatos.Enabled = Not sw
Me.btnGuardar.Enabled = Not sw
Me.btnNuevo.Enabled = sw
Me.btnEditar.Enabled = sw
Me.btnEliminar.Enabled = sw
Me.btnImprimir.Enabled = sw
Me.dtgclientes.Enabled = sw
End Sub

• Esto permite activar o desactivar los botones


del mantenimiento.
Private Sub CargarDatos()
Try
Me.oDataRow = Me.oDataTable.Rows(Me.PosFilaAct)
Me.txtid.Text = oDataRow("ID_CLIENTE")
Me.txtnom.Text = oDataRow("NOM_CLIENTE")
Me.txtape.Text = oDataRow("APE_CIENTE")
Me.txtdni.Text = oDataRow("DNI_CLIENTE")
Me.txtruc.Text = oDataRow("RUC_CLIENTE")
Me.txttele.Text = oDataRow("TELEFONO_CLIENTE")
Me.txtdomicilio.Text = oDataRow("DOMICILIO_CLIENTE")
Catch ex As Exception
MessageBox.Show("Error" & Chr(13) & "No existen registros", "Cargar datos",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub

• Permite jalar todos los campos de la tabla


Cliente.
Private Sub btnNuevo_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnNuevo.Click
For Each objControl As Control In Me.gbdatos.Controls
If TypeOf objControl Is TextBox Then
objControl.Text = ""
End If
Next
vNuevo = True
Activa_Desactiva(False)
Me.txtnom.Focus()
End Sub

• Nos permite ingresar datos de nuevos clientes.


Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnGuardar.Click
Try
For Each objControl As Control In Me.gbdatos.Controls
If (TypeOf objControl Is TextBox) And objControl.Text = "" _
And Not objControl.Name = "txtid" Then
MessageBox.Show("Error faltan datos en" & Chr(13) & _
objControl.Name, "Guardar", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Next
If MessageBox.Show("¿Desea guardar registro actual?", "Guardar", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
If vNuevo Then
oDataRow = Me.oDataTable.NewRow
End If

• oDataRow("NOM_CLIENTE") = Me.txtnom.Text
oDataRow("APE_CIENTE") = Me.txtape.Text
oDataRow("DNI_CLIENTE") = Me.txtdni.Text
oDataRow("RUC_CLIENTE") = Me.txtruc.Text
oDataRow("TELEFONO_CLIENTE") = Me.txttele.Text
oDataRow("DOMICILIO_CLIENTE") = Me.txtdomicilio.Text
If vNuevo Then
Me.oDataTable.Rows.Add(Me.oDataRow)
Me.PosFilaAct = (Me.oDataTable.Rows.Count - 1)
End If
oCommandBuilder.DataAdapter = Me.oDataAdapter
Me.oDataAdapter.Update(Me.oDataTable)
Me.oDataTable.Clear()
Me.oDataAdapter.Fill(Me.oDataTable)
MessageBox.Show("Datos Guardados", "Guardar", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Else
MessageBox.Show("No se guardó el registro", "Eliminar", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
Activa_Desactiva(True)
Me.CargarDatos()
Catch ex As Exception
MessageBox.Show("Error al intentar guardar registro" & Chr(13) & ex.Message, "Guardar",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub

• Esto es el botón Guardar: Permite guardar


todos los datos de la tabla cliente.
Private Sub btnEditar_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnEditar.Click
MessageBox.Show("Proceda con la edición del registro
actual", "Edición", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Me.Activa_Desactiva(False)
vNuevo = False
Me.txtnom.Focus()
End Sub
• Este botón me permite editar o hacer cambios
en la tabla cliente.
Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
Try
If Me.oDataTable.Rows.Count > 0 Then
If MessageBox.Show("¿Desea eliminar registro actual?", "Eliminar", MessageBoxButtons.YesNo, MessageBoxIcon.Question) =
Windows.Forms.DialogResult.Yes Then
Dim strDeleteSQL As String
Dim oSQLCommand As New SqlCommand
strDeleteSQL = "Delete From TB_CLIENTES where ID_CLIENTE =" & CInt(Me.txtid.Text)
Me.oConnection.Open()
oSQLCommand.Connection = Me.oConnection
oSQLCommand.CommandText = strDeleteSQL
oSQLCommand.ExecuteNonQuery()
Me.oConnection.Close()
Me.PosFilaAct = IIf(Me.PosFilaAct > 0, Me.PosFilaAct - 1, 0)
Me.oDataAdapter = New SqlDataAdapter("Select * From TB_CLIENTES", Me.oConnection)
Me.oDataTable.Clear()
Me.oDataAdapter.Fill(Me.oDataTable)
Me.CargarDatos()
End If
Else
MessageBox.Show("No existen registros para eliminar", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MessageBox.Show("Error para eliminar", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub

• Este es el botón eliminar: Permite eliminar los


campos de la tabla cliente.
Private Sub btnImprimir_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnImprimir.Click
Dim myreport As New rtpclientes
myreport.SetDataSource(Me.oDataTable)
Dim myfrom As New frm_Reporte_clientes
myfrom.crvclientes.ReportSource = myreport
myfrom.WindowState = FormWindowState.Maximized
myfrom.ShowDialog()
End Sub

• Permite imprimir.

You might also like