Vous êtes sur la page 1sur 22

ROTEIRO DE BANCO DE DADOS

Nesse projeto o banco de dados, se chama Bdredes.mdb e a tabela se chama redes. - Criar um formulrio semelhante figura abaixo:

- Coloque nos objetos os seguintes names: Objeto Textbox1 Textbox2 Boto Novo Boto Salvar Boto Alterar Boto Excluir Boto Consultar Boto Cancelar Boto Filtro Boto Relatrio Boto Sair Name Txtcod Txtnome Btnnovo Btnsalvar Btnalterar Btnexcluir Btnconsulta Btncancel Btnfiltro Btnrelatorio Btnsair

- Agora ser necessrio adicionar um Mdulo. Siga as instrues da figura abaixo:

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

- Escolha a opo: Module

- V no menu Project na opo: Add References

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

- Aba: .Net , selecione a opo: Ado, conforme a figura abaixo:

No Module digite as linhas de cdigo:

Imports System.Data.OleDb Module Module1 Public conecta As New ADODB.Connection Public tabela As New ADODB.Recordset Public status As Boolean Public da As OleDbDataAdapter Public ds As DataSet

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Public Sub abrebanco() conecta = New ADODB.Connection conecta.Open("Provider = MICROSOFT.JET.OLEDB.4.0;data Source=bdredes.MDB") 'bdredes.mdb dever ser substituido pelo nome do banco de dados que foi desenvolvido. LEMBRE-SE, coloque-o no subdiretrio: Bin> DEBUG End Sub Public Sub limpa() With Form1 .txtcod.Text = "" .txtnome.Text = "" End With End Sub Public Sub carregagrid() 'exibe informao da tabela no grid tabela = New ADODB.Recordset tabela.Open("select * from Clientes order by Codigo", conecta) da = New OleDbDataAdapter ds = New DataSet da.Fill(ds, tabela, "Clientes") Form3.grid.DataSource = ds.Tables("Clientes") Form3.grid.Refresh() End Sub End Module
D um duplo clique no formulrio, abrir a programao no evento load(). Digite a seguinte codificao:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load btnalterar.Enabled = False btnexcluir.Enabled = False btncancel.Enabled = False btnrelatorio.Enabled = True btnsalvar.Enabled = False status = True abrebanco() End Sub

Boto Salvar:

Private Sub btnsalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsalvar.Click If txtcod.Text = "" Then MsgBox("Campo cdigo em branco", MsgBoxStyle.Critical) Exit Sub End If If txtnome.Text = "" Then MsgBox("Campo nome em branco", MsgBoxStyle.Critical) Exit Sub End If Dim strsql As String If status = True Then strsql = "insert into redes (codigo,nome)" & "values (" & txtcod.Text & ",'" & txtnome.Text & "')" conecta.Execute(strsql) MsgBox("Seus dados foram includos com sucesso", MsgBoxStyle.Information, "Dados gravados") ElseIf status = False Then

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

strsql = "update bdredes set nome='" & txtnome.Text & "' where codigo=" & txtcod.Text & "" conecta.Execute(strsql) MsgBox("Registros atualizados com sucesso", MsgBoxStyle.Information, "Atualiza") End If End Sub

Boto Novo:

Private Sub btnnovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnovo.Click btnsalvar.Enabled = True btncancel.Enabled = True End Sub

Boto Alterar:

Private Sub btnalterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnalterar.Click status = False txtcod.Enabled = False txtnome.Focus() End Sub

Boto Cancelar:

Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click btnalterar.Enabled = False btnexcluir.Enabled = False btncancel.Enabled = False btnrelatorio.Enabled = False btnsalvar.Enabled = False End Sub

Boto Excluir:

Private Sub btnexcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexcluir.Click Dim resposta As String resposta = MsgBox("Confirma excluso deste cliente '" & txtnome.Text & "' ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Excluir") If resposta = vbNo Then Exit Sub Else tabela = New ADODB.Recordset tabela.Open("DELETE from redes where codigo = " & txtcod.Text & "", conecta) MsgBox("'" & txtnome.Text & "' Cliente Excluido ", MsgBoxStyle.Information, "Excluido") End If End Sub

Boto Consulta: No boto de consulta dever ser aberto outro formulrio, que nesse projeto teve o nome de Form2.

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Codificao:

Private Sub btnconsulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconsulta.Click btnalterar.Enabled = True btnexcluir.Enabled = True Form2.Show() End Sub

No Form2, no boto Consultar dever ser digitado:

Private Sub btnconsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconsultar.Click tabela = New ADODB.Recordset tabela.Open("SELECT * FROM redes WHERE codigo=" & txtcod.Text & "", conecta,ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic) If tabela.EOF = False Then With Form1 .txtcod.Text = tabela.Fields("Codigo").Value() .txtnome.Text = tabela.Fields("Nome").Value() End With Me.Hide() txtcod.Text = "" Else MsgBox("Registro no encontrado") End If End Sub

Boto Filtro: Nesse boto dever ser aberto outro formulrio, que nesse projeto foi chamado de Form3. Codificao do boto Filtro:

Private Sub btnfiltro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfiltro.Click Form3.Show() End Sub

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

O nome desse objeto DataGridView, altere o name para: Grid

D um duplo clique no Form, abrir o evento Load(). Insira a codificao:

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load carregagrid() End Sub

D um duplo clique no txtnome, abrir o evento: TextChanged. Insira a codificao:

Private Sub txtnome_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnome.TextChanged ds = New DataSet tabela = New ADODB.Recordset tabela.Open("SELECT * FROM redes WHERE Nome LIKE '" & txtnome.Text & "%' ", conecta, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic) da.Fill(ds, tabela, "ITEM") grid.DataSource = ds.Tables(0) End Sub

Crie a subrotina abaixo:

Sub LinhaSelecionadaDoGrid() Form1.Show() With Form1 .txtcod.Text = grid.Rows(grid.SelectedCells(0).RowIndex).Cells(0).Value.ToString() .txtnome.Text = grid.Rows(grid.SelectedCells(0).RowIndex).Cells(1).Value.ToString() End With Me.Hide() End Sub

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

D um duplo clique no Grid e digite o cdigo:

Private Sub grid_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grid.CellContentClick LinhaSelecionadaDoGrid() End Sub

Boto Relatrio: Nesse boto dever ser aberto outro Form. Que nesse projeto foi chamado de Form4.

Private Sub btnrelatorio_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrelatorio.Click Form4.Show() End Sub

Para gerar o relatrio, ser necessrio criar um DataSet. Para isso, v no menu Data e escolha a opo: Add New Data Source

Selecione a opo DataBase e o boto Next.

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Na sequencia a opo DataSet e o boto Next.

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Na prxima tela clique no boto New Connection...

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Selecione o diretrio correspondente ao Banco de Dados:

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Clicar em: Test Connection

Dever aparecer:

Clicar em: Ok

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

- D um duplo clique no objeto ReportView

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

- No form1, no boto relatrio, escreva a llinha de cdigo:

Form4.Show()

Boto Sair

Private Sub btnsair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsair.Click

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

If MsgBox("Deseja realmente sair?", MsgBoxStyle.YesNo, "Banco de Dados") = vbYes Then End Else btncancel.PerformClick() End If End Sub

Prof Klbia Thom klebia@uninove.br Tecnologia em Informtica

Vous aimerez peut-être aussi