PRIMERA PARTE Para el ejemplo se usar la siguiente base de datos con la relacin siguiente (PRODUCTOS.accdb) de Access 2010:
NOTA: Los campos de tipo fecha/hora tienen el formato de Fecha Corta. Los campos de tipo texto, el tamao del campo se adecua al contenido del campo.
CREACIN DEL PROYECTO EN VISUAL BASIC 2010 Se abre el Visual Studio 2010/Visual Basic 2010 y se crea un nuevo Proyecto de Visual Basic/Windows Aplicacin de Windows Forms.
Una vez creado el proyecto se guarda en un directorio denominado Sistema de Ventas (en el escritorio). Dentro del directorio Sistema de Ventas/Sistema de Ventas/bin/Debug/ se colocar la base de datos PRODUCTOS.accdb.
Dentro del proyecto de Visual Basic 2010, se guarda el Form1 con el nombre de frmMenu desde la opcin Men Archivo/ Guardar Form1 como; en el directorio Sistema de ventas.
Propiedades del formulario frmMenu.vb: OBJETO frmMenu VALOR PROPIEDAD Name frmMenu Text Sistema de Ventas WindowState Maximized
Al formulario frmMenu se agrega un control MenuStrip para los mens de acceso del Sistema.
Men Clientes
Men Ventas
Men Ayuda
Men Salir
Al terminar de crear los mens, se agrega un formulario desde el men Proyecto/Agregar Windows Form, se muestra la pantalla siguiente, se selecciona Windows Forms y se presiona Agregar:
El Form2 se guarda con el nombre de frmRegProductos.vb dentro del directorio Sistema de Ventas, con las siguientes propiedades. OBJETO frmMenu VALOR PROPIEDAD Name frmRegProd Text Registro de Productos StartPosition CenterScreen Al formulario se agregan los siguientes controles:
3 Buttons GroupBox 5 Labels GroupBox DateTimerPick er DataGridView
2 TextBox
2 NumericUpDown
CODIFICACIN
Imports System.Data.OleDb Public Class frmRegProductos Dim strConexion As New OleDbConnection With {.ConnectionString = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" & Application.StartupPath & "/PRODUCTOS.accdb;"} Dim strSQL As New OleDbCommand Dim oData As OleDbDataReader Private Sub txtCodigo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCodigo.KeyPress
e.KeyChar = UCase(e.KeyChar) If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then txtProducto.Focus() ElseIf InStr("0123456789.ABCDEFGHIJKLMNOPQRSTUVWYXZ" & vbBack, e.KeyChar) = 0 Then e.Handled = True End If End Sub Private Sub txtProducto_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtProducto.KeyPress e.KeyChar = UCase(e.KeyChar) If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then txtPrecio.Focus() ElseIf InStr("0123456789.ABCDEFGHIJKLMNOPQRSTUVWYXZ- +#" & vbBack, e.KeyChar) = 0 Then e.Handled = True End If End Sub Private Sub txtPrecio_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrecio.KeyPress If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then txtCaducidad.Focus() ElseIf InStr("0123456789." & vbBack, e.KeyChar) = 0 Then e.Handled = True End If End Sub Private Sub txtCaducidad_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCaducidad.KeyPress If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then txtExistencia.Focus() End If End Sub Private Sub txtExistencia_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtExistencia.KeyPress e.KeyChar = UCase(e.KeyChar) If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then cmdGuardar.Focus() ElseIf InStr("0123456789" & vbBack, e.KeyChar) = 0 Then e.Handled = True End If End Sub Private Sub cmdSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSalir.Click Me.Close() End Sub
Private Sub cmdNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNuevo.Click txtCodigo.Text = "" txtProducto.Text = "" txtPrecio.Text = "" txtCaducidad.Text = "" txtExistencia.Text = "" cmdNuevo.Text = "Cancelar" cmdGuardar.Enabled = True txtCodigo.Focus() End Sub Private Sub cmdGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGuardar.Click If (txtCodigo.Text = "") Then MsgBox("Inserte el Codigo del Producto.......") txtCodigo.Focus() ElseIf (txtProducto.Text = "") Then MsgBox("Inserte el Nombre del Producto.......") txtProducto.Focus() ElseIf (Val(txtPrecio.Text) <= 0) Then MsgBox("Inserte el Precio del Producto.......") txtPrecio.Focus() ElseIf (Val(txtExistencia.Text) <= 0) Then MsgBox("Inserte el Numero de Existencia del Producto.......") txtExistencia.Focus() Else Try strConexion.Open() 'Abre base de datos 'Cadena de texto SQL para insertar el producto strSQL.CommandText = "INSERT INTO PRODUCTOS(CODIGO, PRODUCTO, PRECIO, FECHACADUCIDAD, EXISTENCIA) VALUES ('" & txtCodigo.Text & "','" & txtProducto.Text & "', " & Val(txtPrecio.Text) & ", '" & txtCaducidad.Text & "', " & Val(txtExistencia.Text) & ")" strSQL.Connection = strConexion 'Asigna la conexion al comando de la consulta oData = strSQL.ExecuteReader() 'Ejecuta la consulta SQL strConexion.Close() 'Cierra la conexion a la base de datos cmdNuevo_Click(sender, e) cmdNuevo.Text = "Nuevo" cmdGuardar.Enabled = False rellenar() Catch ex As Exception MsgBox("Ha ocurrido un Error...........") strConexion.Close() End Try End If End Sub Private Sub rellenar() Dim da As New OleDbDataAdapter("SELECT * FROM PRODUCTOS", strConexion) Dim ds As New DataSet
da.Fill(ds) DataGridView1.DataSource = ds.Tables(0) End Sub Private Sub frmRegProductos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load rellenar() End Sub End Class
Agregamos un nuevo formulario y lo guardamos con el nombre de frmEditarProd.vb, con el diseo similar al anterior.
Su codificacin es la siguiente
Imports System.Data.OleDb Public Class frmEditarProd Public strConexion As New OleDbConnection With {.ConnectionString = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" & Application.StartupPath & "/PRODUCTOS.accdb;"} Public strSQL As New OleDbCommand Public oData As OleDbDataReader Public Codigo As String Public dv As New DataView
Private Sub cmdBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuscar.Click If Trim(txtCodigo.Text) = "" Then MsgBox("Introduzca el Codigo del Producto a Buscar .....") txtCodigo.Focus()
Exit Sub End If Codigo = txtCodigo.Text strSQL.CommandText = "SELECT * FROM PRODUCTOS WHERE CODIGO='" & Codigo & "'" strSQL.Connection = strConexion strConexion.Open() 'Abre base de datos oData = strSQL.ExecuteReader() 'Ejecuta la consulta SQL If (oData.HasRows) Then oData.Read() txtCodigo.Text = oData.Item(0) txtProducto.Text = oData.Item(1) txtPrecio.Text = oData.Item(2) txtCaducidad.Text = oData.Item(3) txtExistencia.Text = oData.Item(4) cmdBuscar.Text = "Cancelar" cmdGuardar.Enabled = True Else MsgBox("El Codigo no Existe ..............") txtCodigo.Focus() End If strConexion.Close() End Sub Private Sub cmdGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGuardar.Click If (txtCodigo.Text = "") Then MsgBox("Inserte el Codigo del Producto.......") txtCodigo.Focus() ElseIf (txtProducto.Text = "") Then MsgBox("Inserte el Nombre del Producto.......") txtProducto.Focus() ElseIf (Val(txtPrecio.Text) <= 0) Then MsgBox("Inserte el Precio del Producto.......") txtPrecio.Focus() ElseIf (Val(txtExistencia.TextAlign)) Then MsgBox("Inserte el Numero de Existencia del Producto.......") txtExistencia.Focus() Else Try strConexion.Open() strSQL.CommandText = "UPDATE PRODUCTOS SET CODIGO='" & txtCodigo.Text & "',PRODUCTO='" & txtProducto.Text & "', PRECIO=" & Val(txtPrecio.Text) & ", FECHACADUCIDAD='" & txtCaducidad.Text & "', EXISTENCIA=" & Val(txtExistencia.Text) & " WHERE CODIGO='" & Codigo & "'" strSQL.Connection = strConexion oData = strSQL.ExecuteReader() strConexion.Close() Nuevo() cmdBuscar.Text = "Buscar" cmdGuardar.Enabled = False rellenar()
Catch ex As Exception MsgBox("Ha ocurrido un Error") strConexion.Close() End Try End If End Sub Private Sub rellenar() Dim da As New OleDbDataAdapter("SELECT * FROM PRODUCTOS", strConexion) Dim ds As New DataSet da.Fill(ds) dv.Table = ds.Tables(0) DataGridView1.DataSource = dv End Sub Private Sub Nuevo() txtCodigo.Text = "" txtProducto.Text = "" txtPrecio.Text = "" txtCaducidad.Text = "" txtExistencia.Text = "" cmdBuscar.Text = "Buscar" cmdGuardar.Enabled = True txtCodigo.Focus() End Sub Private Sub cmdSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSalir.Click Me.Close() End Sub Private Sub txtCodigo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCodigo.KeyPress e.KeyChar = UCase(e.KeyChar) If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then txtProducto.Focus() ElseIf InStr("0123456789.ABCDEFGHIJKLMNOPQRSTUVWYXZ" & vbBack, e.KeyChar) = 0 Then e.Handled = True End If End Sub Private Sub txtProducto_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtProducto.KeyPress e.KeyChar = UCase(e.KeyChar) If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then txtPrecio.Focus() ElseIf InStr("0123456789.ABCDEFGHIJKLMNOPQRSTUVWYXZ- +#" & vbBack, e.KeyChar) = 0 Then e.Handled = True End If End Sub
Private Sub txtPrecio_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrecio.KeyPress If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then txtCaducidad.Focus() ElseIf InStr("0123456789." & vbBack, e.KeyChar) = 0 Then e.Handled = True End If End Sub Private Sub txtCaducidad_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCaducidad.KeyPress If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then txtExistencia.Focus() End If End Sub Private Sub txtExistencia_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtExistencia.KeyPress e.KeyChar = UCase(e.KeyChar) If (Asc(e.KeyChar) = 13 Or Asc(e.KeyChar) = 9) Then txtProducto.Focus() ElseIf InStr("0123456789" & vbBack, e.KeyChar) = 0 Then e.Handled = True End If End Sub Private Sub frmEditarProd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load rellenar() cmdGuardar.Enabled = False cmdBuscar.Text = "Buscar" End Sub Private Sub txtCodigo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCodigo.TextChanged dv.RowFilter = String.Format("CODIGO Like '%{0}%'", txtCodigo.Text) End Sub End Class
Agregamos otro nuevo formulario con el nombre de frmDelProd.vb con el diseo similar a los anteriores.
Su codificacin es la siguiente
Imports System.Data.OleDb Public Class frmDelProd Public strConexion As New OleDbConnection With {.ConnectionString = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" & Application.StartupPath & "/PRODUCTOS.accdb;"} Public strSQL As New OleDbCommand Public oData As OleDbDataReader Public Codigo As String Public dv As New DataView Private Sub rellenar() Dim da As New OleDbDataAdapter("SELECT * FROM PRODUCTOS", strConexion) Dim ds As New DataSet da.Fill(ds) dv.Table = ds.Tables(0) DataGridView1.DataSource = dv End Sub Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick Dim Fila As Integer = DataGridView1.CurrentCell.RowIndex Dim dato As String = DataGridView1(0, Fila).Value txtCodigo.Text = dato End Sub Private Sub frmDelProd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load rellenar() End Sub
Private Sub cmdBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBuscar.Click If Trim(txtCodigo.Text) = "" Then MsgBox("Introduzca el Codigo del Producto a Buscar .....") txtCodigo.Focus() Exit Sub End If Codigo = txtCodigo.Text strSQL.CommandText = "SELECT * FROM PRODUCTOS WHERE CODIGO='" & Codigo & "'" strSQL.Connection = strConexion strConexion.Open() 'Abre base de datos oData = strSQL.ExecuteReader() 'Ejecuta la consulta SQL If (oData.HasRows) Then oData.Read() txtCodigo.Text = oData.Item(0) txtProducto.Text = oData.Item(1) txtPrecio.Text = oData.Item(2) txtCaducidad.Text = oData.Item(3) txtExistencia.Text = oData.Item(4) cmdBuscar.Text = "Cancelar" cmdEliminar.Enabled = True Else MsgBox("El Codigo no Existe ..............") txtCodigo.Focus() End If strConexion.Close() End Sub Private Sub Nuevo() txtCodigo.Text = "" txtProducto.Text = "" txtPrecio.Text = "" txtCaducidad.Text = "" txtExistencia.Text = "" cmdBuscar.Text = "Buscar" cmdEliminar.Enabled = True txtCodigo.Focus() End Sub Private Sub cmdEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEliminar.Click If (txtCodigo.Text = "") Then MsgBox("Inserte el Codigo del Producto.......") txtCodigo.Focus() Else Try strConexion.Open() strSQL.CommandText = "DELETE FROM PRODUCTOS WHERE CODIGO='" & txtCodigo.Text & "'" strSQL.Connection = strConexion oData = strSQL.ExecuteReader() strConexion.Close() Nuevo() cmdBuscar.Text = "Buscar" cmdEliminar.Enabled = False rellenar()
Catch ex As Exception MsgBox("Ha ocurrido un Error") strConexion.Close() End Try End If End Sub Private Sub txtCodigo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCodigo.TextChanged dv.RowFilter = String.Format("CODIGO Like '%{0}%'", txtCodigo.Text) End Sub Private Sub cmdSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSalir.Click Me.Close() End Sub End Class
dv.Table = ds.Tables(0) DataGridView1.DataSource = dv End Sub Private Sub txtCodigo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCodigo.TextChanged dv.RowFilter = String.Format("CODIGO Like '%{0}%'", txtCodigo.Text) End Sub End Class