Академический Документы
Профессиональный Документы
Культура Документы
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
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
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
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.
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
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
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load carregagrid() End Sub
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
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
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
Dever aparecer:
Clicar em: Ok
Form4.Show()
Boto Sair
If MsgBox("Deseja realmente sair?", MsgBoxStyle.YesNo, "Banco de Dados") = vbYes Then End Else btncancel.PerformClick() End If End Sub