Código:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter
Friend WithEvents OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand
Friend WithEvents OleDbConnection1 As System.Data.OleDb.OleDbConnection
Friend WithEvents lblnome As System.Windows.Forms.Label
Friend WithEvents lblmorada As System.Windows.Forms.Label
Friend WithEvents txtNome As System.Windows.Forms.TextBox
Friend WithEvents txtMorada As System.Windows.Forms.TextBox
Friend WithEvents cmdInserir As System.Windows.Forms.Button
Friend WithEvents cmdActualizar As System.Windows.Forms.Button
Friend WithEvents lblRegistos As System.Windows.Forms.Label
Public WithEvents DsCliente1 As Contactos.dsCliente
Friend WithEvents cmdNavegaPrimeiroRegisto As System.Windows.Forms.Button
Friend WithEvents cmdNavegaRegistoAnterior As System.Windows.Forms.Button
Friend WithEvents cmdNavegaRegistoSeguinte As System.Windows.Forms.Button
Friend WithEvents cmdNavegaUltimoRegisto As System.Windows.Forms.Button
Friend WithEvents cmdSair As System.Windows.Forms.Button
Friend WithEvents cmdEliminar As System.Windows.Forms.Button
Friend WithEvents cmdLimparCampos As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.OleDbDataAdapter1 = New System.Data.OleDb.OleDbDataAdapter
Me.OleDbInsertCommand1 = New System.Data.OleDb.OleDbCommand
Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection
Me.OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand
Me.DsCliente1 = New Contactos.dsCliente
Me.lblnome = New System.Windows.Forms.Label
Me.lblmorada = New System.Windows.Forms.Label
Me.txtNome = New System.Windows.Forms.TextBox
Me.txtMorada = New System.Windows.Forms.TextBox
Me.cmdInserir = New System.Windows.Forms.Button
Me.cmdActualizar = New System.Windows.Forms.Button
Me.lblRegistos = New System.Windows.Forms.Label
Me.cmdNavegaPrimeiroRegisto = New System.Windows.Forms.Button
Me.cmdNavegaRegistoAnterior = New System.Windows.Forms.Button
Me.cmdNavegaRegistoSeguinte = New System.Windows.Forms.Button
Me.cmdNavegaUltimoRegisto = New System.Windows.Forms.Button
Me.cmdSair = New System.Windows.Forms.Button
Me.cmdEliminar = New System.Windows.Forms.Button
Me.cmdLimparCampos = New System.Windows.Forms.Button
CType(Me.DsCliente1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'OleDbDataAdapter1
'
Me.OleDbDataAdapter1.InsertCommand = Me.OleDbInsertCommand1
Me.OleDbDataAdapter1.SelectCommand = Me.OleDbSelectCommand1
Me.OleDbDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Cliente", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("Morada", "Morada"), New System.Data.Common.DataColumnMapping("Nome", "Nome")})})
'
'OleDbInsertCommand1
'
Me.OleDbInsertCommand1.CommandText = "INSERT INTO Cliente(Morada, Nome) VALUES (?, ?)"
Me.OleDbInsertCommand1.Connection = Me.OleDbConnection1
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Morada", System.Data.OleDb.OleDbType.VarWChar, 50, "Morada"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Nome", System.Data.OleDb.OleDbType.VarWChar, 50, "Nome"))
'
'OleDbConnection1
'
Me.OleDbConnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" & _
"ocking Mode=0;Data Source=""C:\Documents and Settings\Vitor Lopes\Os meus documen" & _
"tos\bd1.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLED" & _
"B:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Prop" & _
"erties=;Mode=Share Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create S" & _
"ystem Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Comp" & _
"act Without Replica Repair=False;User ID=Admin;Jet OLEDB:Global Bulk Transaction" & _
"s=1"
'
'OleDbSelectCommand1
'
Me.OleDbSelectCommand1.CommandText = "SELECT Morada, Nome FROM Cliente"
Me.OleDbSelectCommand1.Connection = Me.OleDbConnection1
'
'DsCliente1
'
Me.DsCliente1.DataSetName = "dsCliente"
Me.DsCliente1.Locale = New System.Globalization.CultureInfo("pt-PT")
'
'lblnome
'
Me.lblnome.Location = New System.Drawing.Point(16, 40)
Me.lblnome.Name = "lblnome"
Me.lblnome.Size = New System.Drawing.Size(40, 16)
Me.lblnome.TabIndex = 0
Me.lblnome.Text = "Nome"
Me.lblnome.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'lblmorada
'
Me.lblmorada.Location = New System.Drawing.Point(16, 72)
Me.lblmorada.Name = "lblmorada"
Me.lblmorada.Size = New System.Drawing.Size(48, 23)
Me.lblmorada.TabIndex = 1
Me.lblmorada.Text = "Morada"
Me.lblmorada.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'txtNome
'
Me.txtNome.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.DsCliente1, "Cliente.Nome"))
Me.txtNome.Location = New System.Drawing.Point(72, 40)
Me.txtNome.Name = "txtNome"
Me.txtNome.Size = New System.Drawing.Size(152, 20)
Me.txtNome.TabIndex = 2
Me.txtNome.Text = ""
'
'txtMorada
'
Me.txtMorada.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.DsCliente1, "Cliente.Morada"))
Me.txtMorada.Location = New System.Drawing.Point(72, 72)
Me.txtMorada.Name = "txtMorada"
Me.txtMorada.Size = New System.Drawing.Size(248, 20)
Me.txtMorada.TabIndex = 3
Me.txtMorada.Text = ""
'
'cmdInserir
'
Me.cmdInserir.Location = New System.Drawing.Point(376, 24)
Me.cmdInserir.Name = "cmdInserir"
Me.cmdInserir.TabIndex = 4
Me.cmdInserir.Text = "Inserir"
'
'cmdActualizar
'
Me.cmdActualizar.Location = New System.Drawing.Point(376, 56)
Me.cmdActualizar.Name = "cmdActualizar"
Me.cmdActualizar.TabIndex = 5
Me.cmdActualizar.Text = "Actualizar"
'
'lblRegistos
'
Me.lblRegistos.Location = New System.Drawing.Point(120, 152)
Me.lblRegistos.Name = "lblRegistos"
Me.lblRegistos.Size = New System.Drawing.Size(112, 23)
Me.lblRegistos.TabIndex = 6
Me.lblRegistos.Text = "Label1"
Me.lblRegistos.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'cmdNavegaPrimeiroRegisto
'
Me.cmdNavegaPrimeiroRegisto.Location = New System.Drawing.Point(24, 152)
Me.cmdNavegaPrimeiroRegisto.Name = "cmdNavegaPrimeiroRegisto"
Me.cmdNavegaPrimeiroRegisto.Size = New System.Drawing.Size(40, 23)
Me.cmdNavegaPrimeiroRegisto.TabIndex = 7
Me.cmdNavegaPrimeiroRegisto.Text = "<<"
'
'cmdNavegaRegistoAnterior
'
Me.cmdNavegaRegistoAnterior.Location = New System.Drawing.Point(72, 152)
Me.cmdNavegaRegistoAnterior.Name = "cmdNavegaRegistoAnterior"
Me.cmdNavegaRegistoAnterior.Size = New System.Drawing.Size(40, 23)
Me.cmdNavegaRegistoAnterior.TabIndex = 8
Me.cmdNavegaRegistoAnterior.Text = "<"
'
'cmdNavegaRegistoSeguinte
'
Me.cmdNavegaRegistoSeguinte.Location = New System.Drawing.Point(240, 152)
Me.cmdNavegaRegistoSeguinte.Name = "cmdNavegaRegistoSeguinte"
Me.cmdNavegaRegistoSeguinte.Size = New System.Drawing.Size(40, 23)
Me.cmdNavegaRegistoSeguinte.TabIndex = 9
Me.cmdNavegaRegistoSeguinte.Text = ">"
'
'cmdNavegaUltimoRegisto
'
Me.cmdNavegaUltimoRegisto.Location = New System.Drawing.Point(288, 152)
Me.cmdNavegaUltimoRegisto.Name = "cmdNavegaUltimoRegisto"
Me.cmdNavegaUltimoRegisto.Size = New System.Drawing.Size(40, 23)
Me.cmdNavegaUltimoRegisto.TabIndex = 10
Me.cmdNavegaUltimoRegisto.Text = ">>"
'
'cmdSair
'
Me.cmdSair.Location = New System.Drawing.Point(376, 152)
Me.cmdSair.Name = "cmdSair"
Me.cmdSair.TabIndex = 11
Me.cmdSair.Text = "Sair"
'
'cmdEliminar
'
Me.cmdEliminar.Location = New System.Drawing.Point(376, 88)
Me.cmdEliminar.Name = "cmdEliminar"
Me.cmdEliminar.TabIndex = 12
Me.cmdEliminar.Text = "Eliminar"
'
'cmdLimparCampos
'
Me.cmdLimparCampos.Location = New System.Drawing.Point(376, 120)
Me.cmdLimparCampos.Name = "cmdLimparCampos"
Me.cmdLimparCampos.TabIndex = 13
Me.cmdLimparCampos.Text = "Limpar"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(464, 206)
Me.Controls.Add(Me.cmdLimparCampos)
Me.Controls.Add(Me.cmdEliminar)
Me.Controls.Add(Me.cmdSair)
Me.Controls.Add(Me.cmdNavegaUltimoRegisto)
Me.Controls.Add(Me.cmdNavegaRegistoSeguinte)
Me.Controls.Add(Me.cmdNavegaRegistoAnterior)
Me.Controls.Add(Me.cmdNavegaPrimeiroRegisto)
Me.Controls.Add(Me.lblRegistos)
Me.Controls.Add(Me.cmdActualizar)
Me.Controls.Add(Me.cmdInserir)
Me.Controls.Add(Me.txtMorada)
Me.Controls.Add(Me.txtNome)
Me.Controls.Add(Me.lblmorada)
Me.Controls.Add(Me.lblnome)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DsCliente1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DsCliente1.Clear()
OleDbDataAdapter1.Fill(DsCliente1)
Mostra_Dados()
End Sub
Private Sub Mostra_Dados()
Dim contador As Integer
Dim posicao As Integer
contador = Me.BindingContext(DsCliente1, "Cliente").Count
If contador = 0 Then
MsgBox("Não existem dados", MsgBoxStyle.Exclamation)
Else
posicao = Me.BindingContext(DsCliente1, "Cliente").Position + 1
PosicaoCorrente()
End If
End Sub
Private Sub PosicaoCorrente() 'Conta o númro de registos existentes
lblRegistos.Text = ((("Registo " & Me.BindingContext(DsCliente1, "Cliente").Position + 1).ToString + " de ") + Me.BindingContext(DsCliente1, "Cliente").Count.ToString) & " registo(s)"
End Sub
Private Sub cmdInserir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInserir.Click
Dim dr As DataRow
Dim num As Integer
OleDbDataAdapter1.Fill(DsCliente1)
dr = DsCliente1.Cliente.NewRow()
dr.BeginEdit()
dr("Nome") = txtNome.Text
dr("Morada") = txtMorada.Text
dr.EndEdit()
DsCliente1.Cliente.Rows.Add(dr) ' Adiciona registo ao Dataset
num = OleDbDataAdapter1.Update(DsCliente1, "Cliente") ' Actualiza tabela Cliente
If num = 1 Then
MsgBox("Dados inseridos com sucesso!", MsgBoxStyle.Information)
Else
MsgBox("Erro na inserção dos dados!", MsgBoxStyle.Information)
End If
End Sub
Private Sub cmdActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdActualizar.Click
Try
OleDbDataAdapter1.Update(DsCliente1)
MsgBox("As alterações foram guardadas com exito!", MsgBoxStyle.Information)
Mostra_Dados()
Catch Erro As Exception
MsgBox("Ouve um erro na actualização dos dados!", MsgBoxStyle.Critical)
End Try
End Sub
Private Sub cmdNavegaPrimeiroRegisto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNavegaPrimeiroRegisto.Click
Try
Me.BindingContext(DsCliente1, "Cliente").Position = 0
PosicaoCorrente()
Catch Erro As Exception
MsgBox(Erro.Message)
Me.BindingContext(DsCliente1, "Cliente").CancelCurrentEdit()
End Try
End Sub
Private Sub cmdNavegaRegistoAnterior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNavegaRegistoAnterior.Click
Try
If Me.BindingContext(DsCliente1, "Cliente").Position > 0 Then
Me.BindingContext(DsCliente1, "Cliente").Position = (Me.BindingContext(DsCliente1, "Cliente").Position - 1)
PosicaoCorrente()
End If
Catch dataException As Exception
MsgBox(dataException.Message)
Me.BindingContext(DsCliente1, "Cliente").CancelCurrentEdit()
End Try
End Sub
Private Sub cmdNavegaRegistoSeguinte_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNavegaRegistoSeguinte.Click
Try
If Me.BindingContext(DsCliente1, "Cliente").Position < Me.BindingContext(DsCliente1, "Cliente").Count Then
Me.BindingContext(DsCliente1, "Cliente").Position = (Me.BindingContext(DsCliente1, "Cliente").Position + 1)
PosicaoCorrente()
End If
Catch erro As Exception
MsgBox(erro.Message)
Me.BindingContext(DsCliente1, "Cliente").CancelCurrentEdit()
End Try
End Sub
Private Sub cmdNavegaUltimoRegisto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNavegaUltimoRegisto.Click
Try
Me.BindingContext(DsCliente1, "Cliente").Position = Me.BindingContext(DsCliente1, "Cliente").Count - 1
PosicaoCorrente()
Catch Erro As Exception
MsgBox(Erro.Message)
Me.BindingContext(DsCliente1, "Cliente").CancelCurrentEdit()
End Try
End Sub
Private Sub cmdSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSair.Click
Close()
End Sub
Private Sub cmdEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEliminar.Click
'If MessageBox.Show("EXCLUIR item: << " & Me.txtNome.Text & "," & Me.txtMorada.Text & " >>", "Confirma ?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
'Me.BindingContext(DsCliente1, "Cliente").RemoveAt(Me.BindingContext(DsCliente1, "Cliente").Position)
'Mostra_Dados()
'End If
'Ciclo if anterior funciona, mas só remove no Dataset, e não na BD Access.
'O seguinte ciclo if faz o mesmo que o ciclo if anterior, actualizando logo automaticamente o nº de registos existentes na tabela.
'If MessageBox.Show("Quer remover o registo corrente?", "Remover_Registo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
'If (Me.BindingContext(DsCliente1, "Cliente").Count > 0) Then
'Me.BindingContext(DsCliente1, "Cliente").RemoveAt(Me.BindingContext(DsCliente1, "Cliente").Position)
'Mostra_Dados()
'Else
'MessageBox.Show("Não há registos a remover!")
'End If
'End If
End Sub
Private Sub cmdLimparCampos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLimparCampos.Click
txtNome.Text = ""
txtMorada.Text = ""
End Sub
End Class
cmdInserir funciona, mas depois do registo inserido não dá a posição correta do mesmo.