Hi guys,

Im having trouble with this code, when i had it working with static variables set, it was fine, now i am using a passed variable, its not working... Might not be anything to do with it, anyhow...

Public Class frmContacts
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim projectid As Integer
    Private Sub frmContacts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call reload()

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

    End Sub
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

    End Sub
    Public Sub clear()
        ds.Tables("contacts").Clear()

    End Sub
    Public Sub reload()
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\EZaudit.mdb"
        con.Open()


        sql = "SELECT id As pk, contacts.p_id As id, contacts.con_name as Name, contacts.con_position AS JobTitle, contacts.con_email AS Email, contacts.con_phone AS PhoneNo FROM contacts WHERE contacts.p_id=" & projectid & ""
        da = New OleDb.OleDbDataAdapter(sql, con)

        da.Fill(ds, "contacts")

        con.Close()

        dgCon.DataSource = ds.Tables("contacts")
        dgCon.Visible = True
        dgCon.Columns("id").Visible = False
        dgCon.Columns("pk").Visible = False
        dgCon.Columns("Name").Width = 100
        dgCon.Columns("JobTitle").Width = 100
        dgCon.Columns("Email").Width = 145
        dgCon.Columns("PhoneNo").Width = 120
    End Sub
    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        Dim entry As New frmConEntry()
        entry.ProjectId3 = projectid
        entry.Show()
    End Sub
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        da.Update(ds, "contacts")
        MsgBox("Records Updated")
    End Sub
    Public Property ProjectId5() As String
        Get
            Return projectid
        End Get
        Set(ByVal Value As String)
            projectid = Value
        End Set
    End Property

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.clear()
        Me.reload()

    End Sub
End Class

The 2 public subs being clear, and reload.

If i call them within the same form - with button2.click, then it works as it should... but the following code in another form...

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow



        dsNewRow = ds.Tables("contacts").NewRow()

        dsNewRow.Item("id") = projectid
        dsNewRow.Item("Name") = txtName.Text
        dsNewRow.Item("JobTitle") = txtJTitle.Text
        dsNewRow.Item("Email") = txtEmail.Text
        dsNewRow.Item("PhoneNo") = txtPhoneNo.Text


        ds.Tables("contacts").Rows.Add(dsNewRow)

        da.Update(ds, "contacts")

        MsgBox("New Record added to the Database")

        txtName.Text = ""
        txtJTitle.Text = ""
        txtEmail.Text = ""
        txtPhoneNo.Text = ""

        frmContacts.clear()
        frmContacts.reload()
    End Sub

frmContacts.clear(), just does not clear, it gives a "NullReferenceException - Object reference not set to an instance of an object" and the reload doesn't seem to reload the set of data which was loaded the first time round...

Very Confusing... Any ideas? Everything else is working fine, but i dont want to have to click a button to refresh the table every time i add a contact in a seperate form...

Any ideas?

Hi guys,

Im having trouble with this code, when i had it working with static variables set, it was fine, now i am using a passed variable, its not working... Might not be anything to do with it, anyhow...

Public Class frmContacts
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim projectid As Integer
    Private Sub frmContacts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call reload()

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

    End Sub
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

    End Sub
    Public Sub clear()
        ds.Tables("contacts").Clear()

    End Sub
    Public Sub reload()
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\EZaudit.mdb"
        con.Open()


        sql = "SELECT id As pk, contacts.p_id As id, contacts.con_name as Name, contacts.con_position AS JobTitle, contacts.con_email AS Email, contacts.con_phone AS PhoneNo FROM contacts WHERE contacts.p_id=" & projectid & ""
        da = New OleDb.OleDbDataAdapter(sql, con)

        da.Fill(ds, "contacts")

        con.Close()

        dgCon.DataSource = ds.Tables("contacts")
        dgCon.Visible = True
        dgCon.Columns("id").Visible = False
        dgCon.Columns("pk").Visible = False
        dgCon.Columns("Name").Width = 100
        dgCon.Columns("JobTitle").Width = 100
        dgCon.Columns("Email").Width = 145
        dgCon.Columns("PhoneNo").Width = 120
    End Sub
    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        Dim entry As New frmConEntry()
        entry.ProjectId3 = projectid
        entry.Show()
    End Sub
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        da.Update(ds, "contacts")
        MsgBox("Records Updated")
    End Sub
    Public Property ProjectId5() As String
        Get
            Return projectid
        End Get
        Set(ByVal Value As String)
            projectid = Value
        End Set
    End Property

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.clear()
        Me.reload()

    End Sub
End Class

The 2 public subs being clear, and reload.

If i call them within the same form - with button2.click, then it works as it should... but the following code in another form...

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow



        dsNewRow = ds.Tables("contacts").NewRow()

        dsNewRow.Item("id") = projectid
        dsNewRow.Item("Name") = txtName.Text
        dsNewRow.Item("JobTitle") = txtJTitle.Text
        dsNewRow.Item("Email") = txtEmail.Text
        dsNewRow.Item("PhoneNo") = txtPhoneNo.Text


        ds.Tables("contacts").Rows.Add(dsNewRow)

        da.Update(ds, "contacts")

        MsgBox("New Record added to the Database")

        txtName.Text = ""
        txtJTitle.Text = ""
        txtEmail.Text = ""
        txtPhoneNo.Text = ""

        frmContacts.clear()
        frmContacts.reload()
    End Sub

frmContacts.clear(), just does not clear, it gives a "NullReferenceException - Object reference not set to an instance of an object" and the reload doesn't seem to reload the set of data which was loaded the first time round...

Very Confusing... Any ideas? Everything else is working fine, but i dont want to have to click a button to refresh the table every time i add a contact in a seperate form...

Any ideas?

May not be the best way, but since it's within a class, the way i would do it is.

instead of

Public Sub reload()

i would put

Public Shared Sub reload()

Then you could call it by using

me.reload()

or

frmContacts.reload

from anywhere else in your project.

Do the same thing with your other procedure and i think you'll be alright.

Let me know.

Thanks

Unfortunately, as soon as i make the sub "shared" it stops recognising all the variables and components on the form it relates to?

Any way around this? or any other ideas?

Public Subs/Functions are best off put into a module file so that all forms within a project can access these methods.

I will look into this as a solution, thanks TomW. Got any good resources i can look at re: module files?

Nothing really needed, just add a Module file and start creating your public or private subs & functions. If created as public they can be seen and accessed from anywhere else in your project. The only thing really different in a module file is the declaration of variables. If you want to access global variables in a module file from outside it, you must use the Public keyword instead of Dim.

This article has been dead for over six months. Start a new discussion instead.