Jump to content

Recommended Posts

Posted

Boa tarde,

Ao fazer um update de um registo em Access utilizando o VB.NET e o ADO.NET , obtenho este erro e não compreendo porquê. Está aqui o código do botão Update e o erro:

Na linha: ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text

Obtenho o erro: "A referência de objecto não foi definida como uma instância de um objecto."

 

    Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click

        Dim cb As New OleDb.OleDbCommandBuilder(da)

        ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text
        ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text

        da.Update(ds, "AddressBook")

        MessageBox.Show("Data updated")


    End Sub

Podem-me ajudar?

Obrigado

Posted

Isso deve vir com indicação da linha onde ocorre o erro.
Só com essa informação, diria que o "da" refere não foi instanciado, ou seja, é uma variável que na verdade está a null nesse momento.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Posted

Mas no Form Load tenho:

 

 Private Sub Form_Ensaios_Primeira_Verificação_Contadores_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


        'SET THE DATABASE AND WHERE THE DATABASE IS
        TheDatabase = "/Ensaio_Contadores_2018.accdb"

        'MyDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        MyDocumentsFolder = "C:\Ensaio Contadores 2018"

        FullDatabasePath = MyDocumentsFolder & TheDatabase


        '***********


        'SET UP THE PROVIDER

        dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Dados.accdb;Jet OLEDB:System database=system.mdw;"


        'SET THE DATA SOURCE 
        dbSource = "Data Source = " & FullDatabasePath

        'SET THE CONNECTION STRING
        con.ConnectionString = dbProvider & dbSource


        '***********


        'OPEN THE DATABASE
        con.Open()


        'STORE THE SQL STRING
        sql = "SELECT * FROM Tabela1"


        'PASS THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
        da = New OleDb.OleDbDataAdapter(sql, con)


        'FILL THE DATASET WITH RECORDS FROM DATABASE TABLE
        'Tabela = "Tipo_de_Caudal"
        da.Fill(ds, "AdressBook")

        'CLOSE THE DATABASE
        con.Close()



        MaxRows = ds.Tables("AdressBook").Rows.Count

        inc = 0


        txtFirstName.Text = ds.Tables("AdressBook").Rows(0).Item("First Name")

        txtSurname.Text = ds.Tables("AdressBook").Rows(0).Item("Surname")


    End Sub
Posted (edited)

E em relação ao DataSet - ds:

Dim ds As New DataSet        'HOLDS a DataSet OBJECT

 

Public Class Form_Ensaios_Primeira_Verificação_Contadores


    Dim inc As Integer
    Dim MaxRows As Integer

    Dim con As New OleDb.OleDbConnection    'THE CONNECTION OBJECT

    Dim dbProvider As String                'HOLDS THE PROVIDER
    Dim dbSource As String                  'HOLDS THE DATA SOURCE
    Dim MyDocumentsFolder As String         'HOLDS THE DOCUMENTS FOLDER
    Dim TheDatabase As String               'HOLDS THE DATABASE NAME
    Dim FullDatabasePath As String          'HOLDS THE DATABASE PATH


    Dim ds As New DataSet                   'HOLDS a DataSet OBJECT
    Dim da As OleDb.OleDbDataAdapter        'HOLDS a DataAdapter OBJECT
    Dim sql As String                       'HOLDS a SQL STRING
Edited by diogoarsousa
Posted

Coloca um breakpoint e faz debug passo-a-passo para veres qual a variável que devia estar instanciada e não está.

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Posted

Tem de ser o ds - «O Dataset»

Ele dá erro na linha: 

        ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text

Erro: A referência de objecto não foi definida como uma instância de um objecto.

Como é que eu posso resolver isto?
 

   Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click

        Dim cb As New OleDb.OleDbCommandBuilder(da)

        ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text
        ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text

        da.Update(ds, "AddressBook")

        MessageBox.Show("Data updated")

    End Sub
Posted (edited)

Bom dia,

A questão tem a ver com o Provider.

O mesmo programa, em tudo igual, funciona perfeitamente com o

PROVIDER=Microsoft.Jet.OLEDB.4.0     -  com um ficheiro de uma versão do Access mais antiga - extensão MDB

Mas dá o erro acima -  com um ficheiro de uma versão do Access 2016 - extensão accdb

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Dados.accdb;Jet OLEDB:System database=system.mdw

----------

Já instalei Microsoft Access Driver (*.mdb, *.accdb) - This is the newer "ACE" ODBC driver

Tirado do site da Microsoft:

' ''There are actually two (2) different Access ODBC drivers from Microsoft:

        ' ''Microsoft Access Driver (*.mdb) - This is the older 32-bit "Jet" ODBC driver.
        ' ''    It is included as a standard part of a Windows install.
        ' ''    It only works with .mdb (not .accdb) files. It is also officially deprecated.

        ' ''Microsoft Access Driver (*.mdb, *.accdb) - This is the newer "ACE" ODBC driver.
        ' ''    It is not included with Windows, but it is normally included as part of a Microsoft Office install.
        ' ''    It is also available as a free stand-alone "redistributable" installer for machines without Microsoft Office.
        ' ''    There are separate 64-bit and 32-bit versions of the "ACE" Access Database Engine (and drivers),
        ' ''    and normally one has either the 64-bit version or the 32-bit version installed.
        ' ''    (It is possible to force both versions to exist on the same machine but it is not recommended as it can "break" Office installations.
        ' ''    Therefore, if you already have Microsoft Office it is highly recommended that you use a Python environment that matches the "bitness" of the Office install.)

Edited by diogoarsousa
Posted

M6,

Peço-lhe desculpa.

Descobri o erro no programa. Uma coisa completamente estúpida:

 

Escrevi  "AdressBook"  no preenchimento do DataSet:

 

       'FILL THE DATASET WITH RECORDS FROM DATABASE TABLE

        da.Fill(ds, "AdressBook")

 

E escrevia  "AddressBook" com dois "dd" no código - o que, naturalmente, dava erro.

ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text

 

Obrigado pelo seu tempo.

Diogo Sousa

  • Vote 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.