0% found this document useful (0 votes)
41 views

Conexión: Imports Imports

This document contains code for connecting to a database and performing various operations on a database table like inserting, updating, deleting, searching, and filtering records. It defines connection strings and modules for the database connection. It also includes subroutines for saving, modifying, deleting records, generating codes, handling clicks on the data grid view, calculating totals, and moving panels.

Uploaded by

JOSE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Conexión: Imports Imports

This document contains code for connecting to a database and performing various operations on a database table like inserting, updating, deleting, searching, and filtering records. It defines connection strings and modules for the database connection. It also includes subroutines for saving, modifying, deleting records, generating codes, handling clicks on the data grid view, calculating totals, and moving panels.

Uploaded by

JOSE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CONEXIÓN

Imports System.Data.SqlClient
Imports System.Data.OleDb

Module Conexion
Public CN As New OleDb.OleDbConnection("Provider=sqloledb;Data Source=.\SQLEXPRESS;initial
catalog=municipalidad;user id=sa ;password=123456")
Public cb As New SqlClient.SqlConnection("Data source=.\SQLexpress;initial catalog=municipalidad;user
ID=sa;Password=123456")
End Module

Load
DataGridView1.ColumnHeadersHeight = DataGridView1.ColumnHeadersHeight * 2

BOTON GUARDAR
Dim modi As Boolean
If modi = True Then
guarda()
modi = False
BTNGUARDAR.Enabled = False
Exit Sub
Else
BTNGUARDAR.Enabled = False
BTNNUEVO.Enabled = True
modificardata()
End If

INGRESAR REGISTRO
Sub guarda()
Dim QUERY As String
QUERY = "INSERT INTO Registrador_a VALUES ('" & TextBox2.Text & "','" &
TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text &
"','" & DateTimePicker3.Text & "','" & ComboBox1.Text & "','" & ComboBox2.Text & "','" &
TextBox10.Text & "','" & txtmes.Text & "','" & DateTimePicker1.Text & "','" &
DateTimePicker2.Text & "','" & TextBox15.Text & "','" & ComboBox4.Text & "','" &
ComboBox3.Text & "','" & TextBox16.Text & "','" & TextBox17.Text & "','" & TextBox18.Text &
"','" & TextBox19.Text & "')"
Dim CM As New OleDb.OleDbCommand(QUERY, CN)
CN.Open()
CM.ExecuteNonQuery()
MsgBox("Se a Ingresado los datos Correspondientes" & vbCrLf & "Nota:los Nuevos
trabajadores", MsgBoxStyle.Information)
CN.Close()
GenerarCodigo()
DataGridView1.Refresh()
LIMPIAR()
End Sub

MODIFICAR
Sub modificardata()
Dim c As String
c = "Update Registrador_a set AP_PATERNO='" & TextBox3.Text & "',AP_MATERNO='" &
TextBox4.Text & "',NOMBRE='" & TextBox5.Text & "',DNI='" & TextBox6.Text & "',FECHA_NAC='" &
DateTimePicker3.Text & "',SEXO='" & ComboBox1.Text & "',ESTADO_CIV='" & ComboBox2.Text &
"',N_HIJOS='" & TextBox10.Text & "',mes='" & txtmes.Text & "',FECHA_ING='" &
DateTimePicker1.Text & "',FECHA_Cese='" & DateTimePicker2.Text & "',DIRECCION='" &
TextBox15.Text & "',AREA='" & ComboBox4.Text & "',DESCRIPCION='" & ComboBox3.Text &
"',OCUPACION='" & TextBox16.Text & "',PROFECION='" & TextBox17.Text & "',TELEFONO='" &
TextBox18.Text & "',CELULAR='" & TextBox19.Text & "' WHERE ID_Registro='" & TextBox2.Text &
"'"
CN.Open()
Dim CMD As New OleDb.OleDbCommand(c, CN)
MsgBox("Se A Modificado Los Datos" & vbCrLf & "Nota: Del trabajador",
MsgBoxStyle.Information)
CMD.ExecuteNonQuery()
CN.Close()
End Sub
Eliminar
Dim message As String
message = MessageBox.Show("Desea eliminar el registro", "ADVERTENCIA",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If message = vbYes Then
Dim c As String
c = "DELETE FROM Registrador_a WHERE ID_Registro='" & TextBox2.Text & "'"
CN.Open()
Dim CMD As New OleDb.OleDbCommand(c, CN)
CMD.ExecuteNonQuery()
MsgBox("Se a Eliminado de la Base de datos" & vbCrLf & "Nota:A Un trabajador",
MsgBoxStyle.Information)
CN.Close()
DataGridView1.Refresh()
LIMPIAR()
End If

Buscar
Dim dt As New DataTable
Dim C As String
C = "select * from Registrador_a where ID_Registro='" & TextBox2.Text & "'"
Dim da As New OleDb.OleDbDataAdapter(C, CN)
da.Fill(dt)
If dt.Rows.Count > 0 Then
Dim dr As DataRow = dt.Rows(0)
TextBox3.Text = dr(1)
TextBox4.Text = dr(2)

BtEliminar.Enabled = True
btEditar.Enabled = True
BTNNUEVO.Enabled = False
Modificar.Enabled = True

Exit Sub
Else

BtEliminar.Enabled = False
btEditar.Enabled = False
BTNNUEVO.Enabled = True
Modificar.Enabled = False

End If

Buscar filtrar
Dim DTS As New DataSet
Sub BUSCAREMPLEADO()
Try
Dim CL As New OleDb.OleDbDataAdapter("SELECT ID_Registro,(ap_paterno + ' ' +
ap_materno + ' ' + nombre)AS [ APELLIDOS Y NOMBRES],dni,sexo,OCUPACION FROM
Registrador_a WHERE (ap_paterno + ' ' + ap_materno + ' ' + nombre) LIKE'" & TextBox1.Text &
"%'", CN)
CL.Fill(DTS, 0)
DataGridView1.DataSource = DTS.Tables(0)
DTS.Tables.Clear()
With DataGridView1
.Columns(0).HeaderText = "CODIGO"
.Columns(0).Width = "70"
.Columns(1).Width = "300"
.Columns(2).HeaderText = " DNI"
.Columns(2).Width = "70"
.Columns(3).HeaderText = " SEXO"
.Columns(3).Width = "70"
.Columns(4).HeaderText = " OCUPACION"
.Columns(4).Width = "289"
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Generar código
Dim codS As New DataTable
Sub GenerarCodigo()
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM Registrador_a", CN)
da.Fill(codS)
Dim extra, c As String
If codS.Rows.Count = 0 Then
c = "N° 000001"
TextBox2.Text = c
Else
c = codS.Rows(codS.Rows.Count - 1)("ID_Registro").ToString
extra = Str(Val(c.Substring(5, 5)) + 1000001)
TextBox2.Text = "N° " + extra.Substring(2, 6)
End If
End Sub

DataGridView

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As


System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
TextBox2.Text = DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value
TextBox1.Text = DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value
BTNGUARDAR.Enabled = False
modi = False
End Sub

Sumar datagrid

Public Function calcular(ByVal nombre_Columna As String, ByVal Dgv As DataGridView) As


Double
Dim total As Double = 0
Try
For i As Integer = 0 To Dgv.RowCount - 1
total = total + CDbl(Dgv.Item(nombre_Columna.ToLower, i).Value)
Next
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
Return total
End Function

Sub SUMARCOLUMNA()

If DataGridView7.RowCount >= 1 Then

Dim numero As Integer = DataGridView7.Rows.Count


Dim impor As Double = cal.calcular("Importe", DataGridView7).ToString()

lblImporte.Text = Format(impor, "###.00")

DataGridView7.Enabled = True

Else

lblImporte.Text = "0.00"

DataGridView7.Enabled = False

End If

End Sub

Dim xr As Integer : Dim yr As Integer


Private Sub Mueve(ByVal opcion As Integer, ByVal objeto As Control, ByVal e As
System.Windows.Forms.MouseEventArgs)
If opcion = 0 Then
xr = e.X
yr = e.Y
Else
If e.Button = Windows.Forms.MouseButtons.Left Then
objeto.Location = New Point(objeto.Left + e.X - xr, objeto.Top + e.Y - yr)
End If
End If
End Sub

Private Sub Panel2_MouseDown(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseDown
Mueve(0, Panel2, e)
End Sub
Private Sub Panel2_MouseMove(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseMove
Mueve(1, Panel2, e)
End Sub

dataGridView1.Columns("nombrecolumna").DefaultCellStyle.Format = "HH:mm"

You might also like