Error: Referencia a objeto no establecida como instancia de un objeto
Imports System.Data.OleDb 'Para establecer la conexión con Access
Public Class frmCp
Inherits System.Windows.Forms.Form
Private cn As OleDbConnection 'Conexión
Private da As OleDbDataAdapter 'Maneja los registros de Cp
Private ds As DataSet 'Contine los datos de la tabla Cp
Private sw As Integer
Private fila As Integer
Dim objCadenas As New ClaseCadenas
Private Sub frmCp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Me.CPTableAdapter.Fill(Me.CalderasDataSet.CP)
conectaBD()
activaBotones(True)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
limpiarTextos()
activaBotones(False)
Me.txtId.Text = 0
Me.txtCp.Clear()
Me.txtPobla.Clear()
Me.txtCp.Focus()
sw = 1
End Sub
Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
activaBotones(False)
sw = 2
End Sub
Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
If sw = 1 Then
If MessageBox.Show("¿Desea guardar el Cp?", "Añadir Cp", MessageBoxButtons.YesNo) = MsgBoxResult.Yes Then
insertaCp()
End If
End If
If sw = 2 Then
If MessageBox.Show("¿Desea Modificar el Cp?", "Modificar Cp", MessageBoxButtons.YesNo) = MsgBoxResult.Yes Then
editarCp()
End If
End If
actualizarCp()
activaBotones(True)
End Sub
Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
If MessageBox.Show("¿Desea eliminar el Cp?", "Eliminar Cp", MessageBoxButtons.YesNo) = MsgBoxResult.Yes Then
eliminarCp()
actualizarCp()
End If
End Sub
Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
limpiarTextos()
activaBotones(True)
End Sub
Private Sub btnCerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCerrar.Click
If MessageBox.Show("¿Desea cerrar el programa?", "Terminar proceso", MessageBoxButtons.YesNo) = MsgBoxResult.Yes Then
cn.Close()
Me.Close()
End If
End Sub
Private Sub btnDcha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDcha.Click
Try
Dim num As Short = Me.CPDataGridView.ColumnCount
ReDim Preserve aCp(num)
aCp(num - 2).rId = Me.txtId.Text
aCp(num - 1).rCp = Me.txtCp.Text
aCp(num).rPobla = Me.txtPobla.Text
Me.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub txtFiltroPobla_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFiltroPobla.TextChanged
Dim consulta As String
Dim lista As Byte
Try
If Len(Me.txtFiltroPobla.Text) > 0 Then
consulta = "SELECT * FROM Cp WHERE Poblacion like '" & Trim(txtFiltroPobla.Text) & "%'"
da = New OleDb.OleDbDataAdapter(consulta, cn)
ds = New DataSet
da.Fill(ds, "Cp")
lista = ds.Tables("Cp").Rows.Count
End If
If lista <> 0 Then
Me.CPDataGridView.DataSource = ds
Me.CPDataGridView.DataMember = "Cp"
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub txtFiltroCp_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFiltroCp.TextChanged
Dim consulta As String
Dim lista As Byte
Try
If Len(Me.txtFiltroCp.Text) > 0 Then
consulta = "SELECT * FROM Cp WHERE Cp like '" & Trim(txtFiltroCp.Text) & "%'"
da = New OleDb.OleDbDataAdapter(consulta, cn)
ds = New DataSet
da.Fill(ds, "Cp")
lista = ds.Tables("Cp").Rows.Count
End If
If lista <> 0 Then
Me.CPDataGridView.DataSource = ds
Me.CPDataGridView.DataMember = "Cp"
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub txtPobla_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtPobla.Validating
If Me.txtPobla.Text.Length = 0 Then
Me.ErrorProvider1.SetError(Me.txtPobla, "Debe tener un valor")
Else
Me.ErrorProvider1.SetError(Me.txtPobla, "")
End If
End Sub
Private Sub txtCp_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCp.Validating
If Me.txtCp.Text.Length = 0 Then
Me.ErrorProvider1.SetError(Me.txtCp, "Debe tener un valor")
ElseIf Me.txtCp.Text.Length > 0 And Me.txtCp.Text.Length < 6 Then
Me.ErrorProvider1.SetError(Me.txtCp, "")
Else
Me.ErrorProvider1.SetError(Me.txtCp, "Código Postal erroneo")
End If
End Sub
Private Sub txtpobla_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPobla.LostFocus
Dim t As Integer
Dim cadena As String
Dim mayus, minus As Char
Try
mayus = objCadenas.Mayuscula(Me.txtPobla.Text, 1)
cadena = mayus
For num As Integer = 2 To Len(Me.txtPobla.Text)
minus = objCadenas.Minuscula(objCadenas.Media(Me.txtPobla.Text, num, 1))
If minus Like " " Then
t = 1
cadena = cadena & minus & " "
Else
If t = 0 Then
cadena = cadena & minus
Else
mayus = objCadenas.Mayuscula(minus, 1)
cadena = cadena & mayus
t = 0
End If
End If
Next
Me.txtPobla.Text = cadena
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub conectaBD()
Try
cn = New OleDbConnection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\gpCantabrico\gpCantabrico\gpCantabrico\calderas.accdb"
cn.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub activaBotones(ByVal x As Boolean)
Me.btnNuevo.Enabled = x
Me.btnGuardar.Enabled = Not x
Me.btnEditar.Enabled = x
Me.btnCancelar.Enabled = Not x
Me.btnEliminar.Enabled = x
Me.btnCerrar.Enabled = x
Me.gbDatos.Enabled = Not x
End Sub
Sub insertaCp()
Try
Dim dr As DataRow
dr = ds.Tables("Cp").NewRow
dr(0) = Me.txtId.Text
dr(1) = Me.txtCp.Text
dr(2) = Me.txtPobla.Text
ds.Tables("Cp").Rows.Add(dr)
da.Update(ds, "cp")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub editarCp()
Try
Dim dr As DataRow
dr = ds.Tables("Cp").Rows(fila)
dr(1) = Me.txtCp.Text
dr(2) = Me.txtPobla.Text
da.Update(ds, "Cp")
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Errors.Count)
End Try
End Sub
Sub actualizarCp()
Try
Dim da As New OleDbDataAdapter("Select * From Cp", cn)
Dim ds As New DataSet
da.Fill(ds, "Cp") 'Cargar el DataSet
CPDataGridView.DataSource = ds.Tables("Cp")
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Errors.Count)
End Try
End Sub
Sub eliminarCp()
Dim dr As DataRow
dr = ds.Tables("Cp").Rows(fila)
dr.Delete()
Dim tb As DataTable
tb = ds.Tables("Cp").GetChanges(DataRowState.Deleted)
da.Update(tb)
ds.Tables("Cp").AcceptChanges()
BindingNavigatorMoveFirstItem.PerformClick()
End Sub
Private Sub CpDataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CPDataGridView.SelectionChanged
fila = CInt(Me.CPDataGridView.CurrentRow.Index.ToString())
End Sub
Sub limpiarTextos()
Try
Dim miControl As Control
For Each miControl In Me.Controls
If TypeOf miControl Is TextBox Then
miControl.Text = ""
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Disculpa, te mando toda la clase, haber si así me puedes ayudar. Yo me he metido en este mundo hace 6 meses, ya que tenía ganar de conocer la programación a fondo y ahora que estoy jubilado quiero intentarlo.
Si es posible me puedes indicar donde conseguir un manual de Visual NET, referido solo a las Visual.NET.
Gracias y un saludo