Imports System.
Data
Imports System.Data.OleDb
Public Class FrmMantClientes
Sub buscar()
Dim total As Integer
Dim codigo As Integer = 0
codigo = InputBox("ingrese Codigo a buscar")
If Len(codigo) = 1 Then
codigo = "0" + codigo
End If
Try
Dim tblClientes As DataTable
Dim dr As DataRow
da = New OleDbDataAdapter("select * from clientes where dnicliente='" & codigo
& "'", con)
ds = New DataSet
da.Fill(ds, "BK")
tblClientes = ds.Tables("BK")
total = tblClientes.Rows.Count
dr = tblClientes.Rows(0)
dniclientes.Text = dr("dnicliente")
apellidonombre.Text = dr("apellidonombre")
direccion.Text = dr("direccion")
telefono.Text = dr("telefono")
If IsDBNull(dr("dnicliente")) Then
dniclientes.Text = ""
Else
dniclientes.Text = dr("dnicliente")
If IsDBNull(dr("telefono")) Then
telefono.Text = ""
Else
telefono.Text = dr("telefono")
End If
End If
Catch exc As Exception
MessageBox.Show(exc.Message)
End Try
End Sub
Private Sub FrmMantClientes_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
'agregar 00 a telefono
telefono.Text = "00"
DateTimePicker1.Visible = False
Timer1.Start()
'inabilitando
dniclientes.Enabled = False
apellidonombre.Enabled = False
direccion.Enabled = False
telefono.Enabled = False
email.Enabled = False
' llamar a la base de datoas
ds.Clear()
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=" &
Application.StartupPath & "\ventas.accdb"
con.Open()
sql = "select * from clientes"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "BK")
DataGridView1.DataSource = ds.Tables("BK").DefaultView
Catch ex As Exception
MessageBox.Show("ERROR AL LEER .. " & vbCrLf & ex.Message)
End Try
con.Close()
' inavilitar botones
btngrabar.Enabled = False
' texto con pura mayusculas
dniclientes.CharacterCasing = CharacterCasing.Upper
apellidonombre.CharacterCasing = CharacterCasing.Upper
direccion.CharacterCasing = CharacterCasing.Upper
telefono.CharacterCasing = CharacterCasing.Upper
email.CharacterCasing = CharacterCasing.Upper
'foto
picfoto.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
Private Sub btnborrar_Click(sender As Object, e As EventArgs) Handles btnborrar.Click
'condicional
If dniclientes.Text = "" Then
MsgBox("por favor, introduzca el numero de cliente", MsgBoxStyle.Information)
apellidonombre.BackColor = Color.Yellow
Exit Sub
End If
Dim messageboxResults As Integer
messageboxResults = MsgBox("esta seguro de borrar este cliente?",
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If messageboxResults = Windows.Forms.DialogResult.No Then
MsgBox("cancelado")
Exit Sub
End If
' conectar con la base de datos
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=" &
Application.StartupPath & "\ventas.accdb"
sql = "select * from clientes where dnicliente='" & dniclientes.Text & "'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "BK")
If da.Fill(ds, "BK") = Nothing Then
MsgBox("el cliente no existe", MsgBoxStyle.Critical)
Exit Sub
End If
Catch ex As Exception
MessageBox.Show("error al leer la tabla ... " & ex.Message)
End Try
'crear la base de datos
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=" &
Application.StartupPath & "\ventas.accdb"
con.Open()
sql = "delete * from clientes where dnicliente ='" & dniclientes.Text & "'"
Dim cmd = New OleDb.OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
con.Close()
MsgBox(" BORRADO ", MsgBoxStyle.Information)
dniclientes.Text = ""
apellidonombre.Text = ""
direccion.Text = ""
telefono.Text = ""
email.Text = ""
DataGridView1.DataSource = ds.Tables("BK").DefaultView
Catch ex As Exception
MessageBox.Show("error al borrar desde la tabla ... " & ex.Message)
End Try
btnvertodo.PerformClick()
End Sub
Private Sub btngrabar_Click(sender As Object, e As EventArgs) Handles btngrabar.Click
Try
If dniclientes.Text = Nothing Or apellidonombre.Text = Nothing Or direccion.Text =
Nothing Or telefono.Text = Nothing Or email.Text = Nothing Then
MsgBox("llevar todos los detalles del cliente", MsgBoxStyle.Information)
Exit Sub
End If
connect()
sql = "select * from clientes where dnicliente='" & dniclientes.Text & "'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "BK")
con.Close()
If da.Fill(ds, "BK") = Nothing Then
Try
connect()
sql = "INSERT INTO
clientes(dnicliente,apellidonombre,direccion,telefono,email,foto) VALUES
(@dnicliente,@apellidonombre,@direccion,@telefono,@email,@foto)"
Dim cmd = New OleDb.OleDbCommand(sql, con)
cmd.Parameters.AddWithValue("@dnicliente", dniclientes.Text)
cmd.Parameters.AddWithValue("@apellidonombre", apellidonombre.Text)
cmd.Parameters.AddWithValue("@direccion", direccion.Text)
cmd.Parameters.AddWithValue("@telefono", telefono.Text)
cmd.Parameters.AddWithValue("@email", email.Text)
' colocando la foto en la base de datos
If apellidonombre.Text <> "" And Not picfoto Is Nothing Then
Dim ms As New IO.MemoryStream()
picfoto.Image.Save(ms, picfoto.Image.RawFormat)
Dim pic() As Byte = ms.GetBuffer
cmd.Parameters.Add(New OleDbParameter("@foto", pic))
End If
cmd.ExecuteNonQuery()
con.Close()
MsgBox("nuevo cliente registrado")
dniclientes.Text = ""
apellidonombre.Text = ""
direccion.Text = ""
telefono.Text = ""
picfoto.Text = ""
email.Text = ""
Catch ex As Exception
MsgBox("por favor cierre la base de datos")
End Try
btnvertodo.PerformClick()
Else
MsgBox("ESTE NUMERO DE RESERVA YA EXISTE " & vbCrLf & "" & vbCrLf &
"INTENTE OTRO NUMERO DE CLIENTE")
End If
Catch ex As Exception
End Try
'inabilitando
dniclientes.Enabled = False
apellidonombre.Enabled = False
direccion.Enabled = False
telefono.Enabled = False
email.Enabled = False
btngrabar.Enabled = False
End Sub
Private Sub btnvertodo_Click(sender As Object, e As EventArgs) Handles
btnvertodo.Click
ds.Clear()
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=" &
Application.StartupPath & "\ventas.accdb"
con.Open()
sql = "select * from clientes"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "BK")
con.Close()
DataGridView1.DataSource = ds.Tables("BK").DefaultView
Catch ex As Exception
MessageBox.Show("ERROR AL LEER .. " & vbCrLf & ex.Message)
End Try
con.Close()
End Sub
Private Sub btnborrargrid_Click(sender As Object, e As EventArgs)
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As
DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Try
' agrega al textbox
Dim currentCell As DataGridViewRow
currentCell = DataGridView1.CurrentRow
dniclientes.Text = currentCell.Cells("dnicliente").Value
apellidonombre.Text = currentCell.Cells("apellidonombre").Value
direccion.Text = currentCell.Cells("direccion").Value
telefono.Text = currentCell.Cells("telefono").Value
email.Text = currentCell.Cells("email").Value
' agregar la foto a la caja de imagen
picfoto.Image = Nothing
Dim pic() As Byte = DataGridView1.SelectedCells(0).Value
Dim ms As New IO.MemoryStream(pic)
picfoto.Image = Image.FromStream(ms)
' colocar tamao rgande la imagen
picfoto.SizeMode = PictureBoxSizeMode.StretchImage
DataGridView1.Refresh()
Catch ex As Exception
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'habilitando
dniclientes.Enabled = True
apellidonombre.Enabled = True
direccion.Enabled = True
telefono.Enabled = True
email.Enabled = True
btngrabar.Enabled = True
End Sub
Private Sub btnsalir_Click(sender As Object, e As EventArgs) Handles btnsalir.Click
Me.Close()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
' agregando fecha y hora
Dim fecha As String
fecha = DateTimePicker1.Text
Label13.Text = fecha
Label12.Text = TimeOfDay
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
buscar()
End Sub
Private Sub dniclientes_KeyPress(sender As Object, e As KeyPressEventArgs) Handles
dniclientes.KeyPress
' acepta numericos y ascci
If Char.IsDigit(e.KeyChar) Then
e.Handled = False
ElseIf Char.IsControl(e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
Private Sub apellidonombre_KeyPress(sender As Object, e As KeyPressEventArgs)
Handles apellidonombre.KeyPress
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles picfoto.Click
' buscar imagenes
Try
OpenFileDialog1.Title = "Abrir Imagen"
OpenFileDialog1.FileName = "Imagenes"
OpenFileDialog1.Filter = "All files"
OpenFileDialog1.ShowDialog()
picfoto.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName)
Catch ex As Exception
End Try
End Sub
Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles
btnbuscarimagen.Click
Dim ofd As New OpenFileDialog
ofd.Filter = "imagen |*.*"
ofd.RestoreDirectory = True
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
picfoto.Image = Image.FromFile(ofd.FileName)
End If
End Sub
Private Sub Label14_Click(sender As Object, e As EventArgs) Handles Label14.Click
End Sub
Private Sub telefono_KeyPress(sender As Object, e As KeyPressEventArgs) Handles
telefono.KeyPress
' acepta numericos y ascci
If Char.IsDigit(e.KeyChar) Then
e.Handled = False
ElseIf Char.IsControl(e.KeyChar) Then
e.Handled = False
Else
e.Handled = True
End If
End Sub
End Class