Claves para realizar el proyecto final. Declaración de variables en Módulo:
Public Base As DAO.Database
Public RstClientes As DAO.RecordSet
Public RstProductos As DAO.Recordset
Public RstProveedores As DAO.Recordset
'(As� igual para cada tabla de la base de datos)
Inicializar variables (evento Load del formulario inicial):
Private Sub Form_Load()
Set Base = OpenDatabase(App.Path & "\Sistema final.mdb")
Set RstClientes = Base.OpenRecordset("Clientes")
Set RstProductos = Base.OpenRecordset("Productos")
Set RstProveedores = Base.OpenRecordset("Proveedores")
End Sub
Llenar un ListBox con el campo Apellido y Nombre de la tabla Clientes:
Public Sub CargarClientes()
List1.Clear
If RstClientes.BOF And RstClientes.EOF Then
MsgBox "No hay clientes en la base de datos.", vbCritical
Exit Sub
End If
RstClientes.MoveFirst
Do While Not RstClientes.EOF
List1.AddItem RstClientes!Apellido & ", " & RstClientes!Nombre
RstClientes.MoveNext
Loop
List1.Listindex = 0
End Sub
Seleccionar un cliente en el ListBox:
Private Sub List1_Click()
If List1.ListIndex = -1 Then Exit Sub
RstClientes.MoveFirst
Do While RstClientes!Apellido & ", " & RstClientes!Nombre <> List1.text
RstClientes.MoveNext
Loop
Text1.Text = RstClientes!CodCli
Text2.Text = RstClientes!Nombre
Text3.Text = RstClientes!Apellido
Text4.Text = RstClientes!Direccion
Text5.Text = RstClientes!Localidad
Text6.Text = RstClientes!Deuda
End Sub
Permitir s�lo n�meros en un TextBox:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub
Agregar un nuevo registro en una tabla:
Private Sub CmdGuardar_Click()
On Error Goto Error:
If Msgbox("¿Desea guardar el cliente?", VbYesNo + VbQuestion) = VbYes Then
RstClientes.AddNew
RstClientes!CodCli = Text1.Text
RstClientes!Nombre = Text2.Text
RstClientes!Apellido = Text3.Text 'Puede reemplazar el método AddNew
RstClientes!Direccion = Text4.Text 'por una instrucci�n SQL INSERT INTO...
RstClientes!Localidad = Text5.Text
RstClientes!Deuda = Text6.Text
RstClientes.Update
MsgBox "El Cliente ha sido registrado", VbInformation
End If
Exit Sub
Error:
If Err.Number = 3022 Then
MsgBox "Ya existe un cliente con ese código.", VbCritical
Else
Msgbox Err.Number & " " & Err.Description
End If
End Sub
Modificar un registro de la tabla:
Private Sub CmdGuardar_Click()
On Error Goto Error:
If Msgbox("¿Desea modificar el cliente?", VbYesNo + VbQuestion) = VbYes Then
RstClientes.Edit
RstClientes!CodCli = Text1.Text
RstClientes!Nombre = Text2.Text
RstClientes!Apellido = Text3.Text 'Puede reemplazar el método Edit
RstClientes!Direccion = Text4.Text 'por una instrucci�n SQL UPDATE...
RstClientes!Localidad = Text5.Text
RstClientes!Deuda = Text6.Text
RstClientes.Update
MsgBox "El Cliente ha sido modificado", VbInformation
End If
Exit Sub
 Error:
If Err.Number = 3022 Then
MsgBox "Ya existe un cliente con ese código.", VbCritical
Else
Msgbox Err.Number & " " & Err.Description
End If
End Sub
Eliminar un registro:
Private Sub MnuEliminar_Click()
If Msgbox("¿Desea eliminar el registro?", VbYesNo + VbQuestion) = VbYes Then
Base.Execute "Delete * From Clientes Where CodCli = '" & Text1.Text & "';"
Msgbox "El cliente ha sido eliminado.", VbInformation
CargarClientes
End If
End Sub