Anda di halaman 1dari 5

2011

Cdigos fuentes
aplicativo Agenda con
VB.NET
Cdigos fuente del aplicativo desarrollado en Visual Basic .Net, para el formulario
Agenda.vb y el mdulo funciones.vb

Ver cdigo fuente de Agenda.vb


Ver cdigo fuente de funciones.vb

Andres
[Escriba el nombre de la compaa]
01/01/2011

AGENDA.VB
Imports MySql
Imports MySql.Data.MySqlClient
Public Class Agenda
Public pbus As String
Public idactual As Integer
Private Sub consultaxcodigo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
consultaxcodigo.Click
param.Text = "Cdigo estudiante"
pbus = "COD_EST"
parbusqueda.Clear()
pconsulta.Visible = True
borratb()
Me.Height = 108
bagregar.Enabled = False
modificar.Enabled = True
eliminar.Enabled = True
End Sub
Private Sub consultaxapellidos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles consultaxapellidos.Click
param.Text = "Apellidos"
pbus = "APELLIDOS"
parbusqueda.Clear()
pconsulta.Visible = True
borratb()
Me.Height = 108
bagregar.Enabled = False
modificar.Enabled = True
eliminar.Enabled = True
End Sub
Private Function borratb()
tbcodigo.Clear()
tbapellidos.Clear()
tbnombres.Clear()
tbdocumento.Clear()
tbtelefono.Clear()
fecha.Value = Date.Today
Return 0
End Function
Private Sub bconsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
bconsultar.Click
Dim datos As MySqlDataReader
If parbusqueda.TextLength <> 0 Then
Dim parbuscar As String = "'" & parbusqueda.Text & "'"
If conexion(True) Then
datos = consultar("SELECT * FROM datos WHERE " & pbus & "=" & parbuscar & ";")
If datos.HasRows <> 0 Then
datos.Read()
idactual = datos("ID_EST")
tbcodigo.Text = datos("COD_EST")
tbapellidos.Text = datos("APELLIDOS")
tbnombres.Text = datos("NOMBRES")
tbdocumento.Text = datos("NO_DI")
fecha.Value = datos("FECHA_NACE")

tbtelefono.Text = datos("TELEFONO")
datos.Close()
Me.Height = 334
Else
MsgBox("No se han encontrado registros para su bsqueda", MsgBoxStyle.Information,
"Registro no encontrado")
End If
con.Close()
End If
End If
End Sub
Private Sub Agenda_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Height = 108
fecha.CustomFormat = "d 'de' MMMM 'de' yyy" ' definimos el formato con que nos va mostrar la fecha
en el datatimepicker
End Sub
Private Sub modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
modificar.Click
If conexion(True) Then
Try
actualizar("UPDATE datos SET COD_EST='" & tbcodigo.Text & "', APELLIDOS='" &
tbapellidos.Text & _
"', NOMBRES='" & tbnombres.Text & "', NO_DI='" & tbdocumento.Text &
"', FECHA_NACE='" & _
fecha.Value.ToString("yyyy-MM-dd") & "', TELEFONO='" &
tbtelefono.Text & "' WHERE ID_EST=" & idactual & ";")
MsgBox("El registro se ha modificado exitosamente", MsgBoxStyle.Information, "Modificacin
exitosa")
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End If
con.Close()
End Sub
Private Sub eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
eliminar.Click
If conexion(True) Then
Try
consultar("DELETE from datos WHERE ID_EST=" & idactual & ";")
MsgBox("El registro se ha borrado exitosamente", MsgBoxStyle.Information, "Borrado
exitoso")
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End If
con.Close()
End Sub
Private Sub agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
agregar.Click
Me.Height = 334
pconsulta.Visible = False
borratb()
bagregar.Enabled = True
modificar.Enabled = False
eliminar.Enabled = False
End Sub

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


Cerrar.Click
Me.Close()
End Sub
Private Sub bagregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
bagregar.Click
If conexion(True) Then
Try
actualizar("INSERT INTO datos (COD_EST,APELLIDOS, NOMBRES,NO_DI,FECHA_NACE,TELEFONO) VALUES
('" & _
tbcodigo.Text & "','" & tbapellidos.Text & "','" & tbnombres.Text & "','" &
tbdocumento.Text & _
"','" & fecha.Value.ToString("yyyy-MM-dd") & "','" & tbtelefono.Text & "');")
MsgBox("El registro se ha creado exitosamente", MsgBoxStyle.Information, "Creacin
exitosa")
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End If
con.Close()
End Sub
End Class

FUNCIONES.VB
Imports MySql
Imports MySql.Data.MySqlClient
Imports System.Data
Module funciones
Public con As MySqlConnection
Public Function conexion(ByVal msg As Boolean)
con = New MySqlConnection()
con.ConnectionString = "server=localhost; user id=usuariosgd; password=sgd11; database=sgd11;"
Try
con.Open()
conexion = True
Catch mierror As MySqlException
conexion = False
If msg Then
MsgBox("No ha sido posible conectarse al sistema" & Chr(13) & _
mierror.Message.ToString & Chr(13) & _
"Intentelo ms tarde o verifique la conexin al Servidor", _
MsgBoxStyle.Critical, "Error al conectarse al Sistema")
End If
End Try
End Function
Public Function consultar(ByVal busqueda As String) As MySqlDataReader
Dim myadapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.Connection = con
command.CommandText = busqueda
myadapter.SelectCommand = command
consultar = command.ExecuteReader
End Function
Public Function actualizar(ByVal registro As String) As Integer
Dim myadapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.Connection = con
command.CommandText = registro
myadapter.SelectCommand = command
actualizar = command.ExecuteNonQuery
End Function
End Module

Anda mungkin juga menyukai