Está en la página 1de 13

CONEXIN DE BASE DE DATOS UTILIZANDO VISUAL BASIC 6.

0 Y MICROSOFT ACCESS PARTE 1: CONEXIN A LA BASE DE DATOS, INGRESO Y CREACIN DE USUARIOS Utilizacin del control ADO en Visual Basic 6.0 Para activar este control se debe se agregar el siguiente componente: Clic derecho en la barra de controles (General) Luego escogemos la opcin Microsoft ADO Data Control 6.0 (OLEDB) y pulsamos en el botn aceptar. Utilizacin del control DTPicker en Visual basic 6.0 Clic derecho en la barra de controles (General) Luego escogemos la opcin Microsoft Windows Common Controls-2.6.0 (SP4) y pulsamos en el botn aceptar. Utilizacin del control DataGrid en Visual basic 6.0 Clic derecho en la barra de controles (General) Luego escogemos la opcin Microsoft DataGrid Controls 6.0 (OLEDB) y pulsamos en el botn aceptar. Creacin de un proyecto para la conexin de base de datos utilizando el control ADO. PRIMERO ABRIR VISUAL BASIC 6.0 Crear 7 formularios STANDAR, un formulario MDI y 2 mdulos STANDAR. En los 7 formularios cambiamos en la ventana de propiedades las siguientes opciones: Form1: Nombre= frmcaratula BorderStyle=None StarUpPosition=CenterScreen Width=7065 Height=4335 Form2 : Nombre= frmingreso BorderStyle=None StarUpPosition=CenterScreen Width= 5625 Height= 3075 Form3 : Nombre= frmcrear BorderStyle=None Width= 14460

Height= 7890 MDIChild=TRUE

Form4 : Nombre= frmmodificar BorderStyle=None Width= 14460 Height= 7890 MDIChild=TRUE Form5 : Nombre= frmeliminar BorderStyle=None Width= 14460 Height= 7890 MDIChild=TRUE Form6 : Nombre= frmbuscar BorderStyle=None Width= 10530 Height= 6405 MDIChild=TRUE Form7 : Nombre= frmacerca BorderStyle=None Width= 7680 Height= 4635 FormMDI : Nombre= mdiprincipal Caption=Sistema de Usuarios WindowState= Maximized Module1 : Nombre= moduledeclare Module2 : Nombre= modulesentences

Luego en el formulario frmcaratula ponemos los siguientes controles: Insertar 4 labels, un Timer como se muestra en la figura.

En todos los labels cambiamos en la ventana de propiedades las siguientes opciones: Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14

En el Timer cambiamos en la ventana de propiedades las siguientes opciones: Nombre =tmrinicio Interval=3000 Luego abrimos la ventana de programacin del tmrinicio y escribimos el siguiente cdigo: Private Sub tmrinicio_Timer() trminicio.Enabled = False frmingreso.Show Unload Me End Sub

En los mdulos creados vamos a escribir el siguiente cdigo: Antes de hacer esto necesitamos realizar lo siguiente: En la barra de mens escogemos proyecto, luego referencias damos un clic se abra una ventana en la cual seleccionamos Microsoft ActiveX Data Objects 2.1 Library, lo cual nos permitir declarar variables de tipo ADODB para manipular la base de datos una vez seleccionada damos en aceptar y listo. Ahora abrimos el moduledeclare y escribimos lo siguiente: (la variable base servir para la conexin a la base de datos y la variable Rsusuario servir para el acceso a la tabla de la base de datos) Global base As New ADODB.Connection Global Rsusuario As New ADODB.Recordset Ahora abrimos el modulesentences y escribimos lo siguiente: Esto nos permitir realizar la conexin a la base de datos creada en Acces. Sub main() With base .CursorLocation = adUseClient .Open "aqu va la cadena de conexin que se genera con el control Adodc visto en la clase anterior" End With End Sub Esto nos permitira verificar si la tabla de la base de datos esta abierta, si es as la cerraremos y la abriremos nuevamente. Sub usuario() With Rsusuario If .State = 1 Then .Close .Open "select * from Usuario", base, adOpenStatic, adLockOptimistic End With End Sub Luego en el formulario frmingreso ponemos los siguientes controles: Insertar 2 labels, y 2 Textbox como se muestra en la figura. 2 Commandbutton,

En los textbox cambiamos en la ventana de propiedades las siguientes opciones: Text1 Text2

Nombre=txtid Height= 375 Width= 2535 Nombre=txtclave Height= 375 Width=2535 PasswordChar=*

En los CommandButton cambiamos en la ventana de propiedades las siguientes opciones: commandButton1 nombre= cmdingresar caption=Ingresar width=1335 Height=495 commandButton2 nombre= cmdcancelar caption= Cancelar width= 1335 Height= 495 En los Label cambiamos en la ventana de propiedades las siguientes opciones: Label1 Label2

caption= Id del Usuario Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14 caption= Contrasea Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14

Abrimos la ventana de programacin para proceder a ingresar el siguiente cdigo en los commandbutton:

Private Sub cmdingresar_Click() If txtid.Text = "" Then MsgBox "Por favor ingrese el Nombre de Usuario", vbInformation, "Aviso" txtid.SetFocus Exit Sub End If If txtClave.Text = "" Then MsgBox "Por favor ingrese su Contrasea", vbInformation, "Aviso" txtClave.SetFocus Exit Sub End If With Rsusuario .Requery 'actualiza la tabla de la base de datos .Find "id='" & Trim(txtid.Text) & "'" If .EOF Then MsgBox "Usuario Incorrecto", vbInformation, "Aviso" txtid.Text = "" txtid.SetFocus Exit Sub Else If !clave = Trim(txtClave.Text) Then MsgBox "Registro Satisfactorio", vbInformation, "Aviso" mdiprincipal.Show Unload Me Else MsgBox "Contrasea Incorrecta", vbInformation, "Aviso" txtClave.Text = "" txtClave.SetFocus Exit Sub End If End If End With End Sub

Private Sub cmdCancelar_Click() If MsgBox("Esta seguro que desea cancelar", vbInformation + vbYesNo, "Salir") = vbYes Then MsgBox "Gracias por Utilizar la Aplicacin", vbInformation, "Aviso" End End If End Sub Private Sub Form_Load() main usuario End Sub

Luego en el formulario mdiprincipal creamos los siguientes mens como se muestra en la figura:

Para realizar estas operaciones nos dirigimos a la barra de mens y escogemos herramientas y luego escogemos editor de mens.

Los nombres de los mens son: mnuproyecto mnuusuarios mnucrear mnumodificar mnueliminar mnubuscar mnusalir mnuacerca

En el caption de cada uno de los mens vamos a poner lo siguiente: Proyecto Usuarios Crear Modificar Eliminar Buscar Salir Acerca de..

Luego abrimos la ventana de programacin para ingresar el siguiente cdigo: Private Sub MDIForm_QueryUnload(Cancel As Integer, UnloadMode As Integer) If MsgBox("Esta seguro que desea cerrar la aplicacin", vbInformation + vbYesNo, "Cerrar") = vbYes Then MsgBox "Gracias por utilizar la Aplicacion", vbInformation, "Gracias" base.Close frmingreso.Show Unload Me Else Cancel = 1 End If End Sub Private Sub mnuacerca_Click() mnuproyecto.Enabled = False frmacerca.Show End Sub Private Sub mnubuscar_Click() mnucrear.Enabled = False mnuacerca.Enabled = False mnumodificar.Enabled = False mnueliminar.Enabled = False mnusalir.Enabled = False frmbuscar.Show End Sub

Private Sub mnucrear_Click() mnuacerca.Enabled = False mnumodificar.Enabled = False mnueliminar.Enabled = False mnusalir.Enabled = False mnubuscar.Enabled = False frmcrearusuario.Show End Sub Private Sub mnueliminar_Click() mnuacerca.Enabled = False mnumodificar.Enabled = False mnucrear.Enabled = False mnusalir.Enabled = False mnubuscar.Enabled = False frmeliminarusuario.Show End Sub Private Sub mnumodificar_Click() mnuacerca.Enabled = False mnueliminar.Enabled = False mnucrear.Enabled = False mnusalir.Enabled = False mnubuscar.Enabled = False frmmodificar.Show End Sub Private Sub mnusalir_Click() Unload Me End Sub Sub activar() mnuproyecto.Enabled = True mnuusuarios.Enabled = True mnuacerca.Enabled = True mnueliminar.Enabled = True mnucrear.Enabled = True mnusalir.Enabled = True mnubuscar.Enabled = True mnumodificar.Enabled = True End Sub Luego en el formulario frmcrear ponemos los siguientes controles: Insertar 8 labels, 6 Textbox y un DTPicker como se muestra en la figura. 2 Commandbutton,

En los Label cambiamos en la ventana de propiedades las siguientes opciones: Label1 Label2 Label3 Label4

caption= Id del Usuario Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14 caption= Contrasea Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14 caption= Nombre Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14 caption= Apellido Autosize=true BackStyle=Transparent Border style=None

Label5 Label6 Label7 Label8

Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14 caption= Direccin Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14 caption= Telfono Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14 caption= Fecha Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:14 caption= Creacin de Usuarios Autosize=true BackStyle=Transparent Border style=None Font= fuente: MS Sans Serif; Estilo de fuente:Negrita; Tamao:24

En los textbox cambiamos en la ventana de propiedades las siguientes opciones: Text1 Text2

Nombre=txtid Height= 375 Width= 2535 Nombre=txtclave Height= 375 Width=2535 PasswordChar=*

Text3 Nombre=txtnombre Height= 327.236 Width= 2433.646

Text4 Text5 Text6

Nombre=txtapellido Height= 327.236 Width= 2433.646 Nombre=direccion Height= 327.236 Width= 2433.646 Nombre=txttelefono Height= 327.236 Width= 2433.646

En el DTPicker1 cambiamos en la ventana de propiedades las siguientes opciones: Nombre:DTPFecha Height= 327.236 Width= 1267.29 En los CommandButton cambiamos en la ventana de propiedades las siguientes opciones: commandButton1 nombre= cmdcrear caption=Crear width= 998.131 Height= 431.951 commandButton2 nombre= cmdcancelar caption= Cancelar width= 998.131 Height= 431.951 En el DataGrid1 cambiamos en la ventana de propiedades las siguientes opciones: Nombre: gridusuarios Caption: Lista de Usuarios Height= 1793.252 Width= 10508.42

Abrimos la ventana de programacin para proceder a ingresar el siguiente cdigo en los commandbutton:

Private Sub cmdCrear_Click() With Rsusuario

.Requery .AddNew !id = txtId.Text !clave = txtClave.Text !nombre = txtNombre !apellido = txtApellido !direccion = txtDireccion !telefono = txtTelefono !fecha = DTPFecha.Value .Update .Requery limpiar End With End Sub Private Sub Form_Load() usuario Set Gridusuarios.DataSource = tabla frmcrear.Top = Screen.Height / 2 - frmcrear.Height / 2 frmcrear.Left = Screen.Width / 2 - frmcrear.Width / 2 tabla.Requery DTPFecha.Value = Date End Sub Sub limpiar() txtId.Text = "" txtClave.Text = "" txtNombre.Text = "" txtApellido.Text = "" txtDireccion.Text = "" txtTelefono.Text = "" DTPFecha.Value = Date txtId.SetFocus End Sub Private Sub cmdCerrar_Click() If MsgBox("Esta seguro que desea salir", vbInformation + vbYesNo, "Salir") = vbYes Then Unload Me mdiprincipal.activar End If End Sub

También podría gustarte