What I am testing is how to insert data in access from vb.net.
For what I have, I made a test form with 2 fields plus a save button.
This is the code you make.
Imports System.Data.OleDb
Public Class Form1
Dim conexion As OleDbConnection
Dim comandos As OleDbCommand
Dim StrConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Y:\db\TestDB.accdb"
Private Sub btnEnviar_Click(sender As Object, e As EventArgs) Handles btnEnviar.Click
Try
comandos = New OleDbCommand("INSERT INTO Tabla1 (campo1, campo2) VALUES (txtCampo1, txtCampo2)", conexion)
comandos.Parameters.AddWithValue("@campo1", txtCampo1.Text)
comandos.Parameters.AddWithValue("@campo2", txtCampo2.Text)
comandos.ExecuteNonQuery()
MsgBox("Guardado correctamente", vbInformation, "Correcto")
txtCampo1.Clear()
txtCampo2.Clear()
txtCampo1.Focus()
Catch ex As Exception
MsgBox("Error al guardar", vbExclamation, "Error")
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
conexion.ConnectionString = StrConnection
conexion.Open()
MsgBox("Conexión exitosa!", vbInformation, "Correcto")
Catch ex As Exception
MsgBox("Se ha producido un error al conectarse a DB" + ex.ToString, vbExclamation, "Error")
End Try
End Sub
Private Sub btnSalir_Click(sender As Object, e As EventArgs) Handles btnSalir.Click
'conexion.Close()
MsgBox("Desconectado", vbInformation, "Correcto")
Me.Close()
End Sub
End Class
What I get when trying to connect is a DBSystem.NullReferenceException. At first I thought that I did not have access to the Database, but I did connection tests and if I have access.