0

I have a solution with multiple-project, and i have a master form called master_ChartofAccount, and on coa_Create after saving the data, i want to call a Sub named showCoa to refresh a grid on coa_Create, here's my code :

Private Sub cmdAdd_Click(sender As Object, e As EventArgs) Handles cmdAdd.Click
    Dim frm As New coa_Create
    frm.ShowDialog(Me)
End Sub

and here's cmdSave code on coa_Create :

Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
    If Not allowSave() Then Exit Sub
    Dim str As String
    txtKode.Tag = IIf(IsNothing(txtKode.Tag), "", txtKode.Tag)

    If txtKode.Tag.ToString = "" Then
        str = "insert into t_chart_of_account (coa_code, coa_name, p_code, is_parent, db_cr, bs_is) " _
            & "values ('" & objComp.clearSingleQuote(txtKode.Text) & "', '" & objComp.clearSingleQuote(txtNama.Text) & "', " _
            & "'" & objComp.clearSingleQuote(txtParentCode.Text) & "', " & IIf(chkParent.Checked = True, 1, 0) & ", " _
            & "'" & IIf(optDb.Checked = True, "DR", "CR") & "', '" & IIf(optBS.Checked = True, "BS", "IS") & "')"
    Else
        str = "update t_chart_of_account set coa_code = '" & txtKode.Text & "', " _
            & "coa_name = '" & objComp.clearSingleQuote(txtNama.Text) & "', p_code = '" & txtParentCode.Text & "', " _
            & "is_parent = " & IIf(chkParent.Checked = True, 1, 0) & ", db_cr = '" & IIf(optDb.Checked = True, "DR", "CR") & "', " _
            & "bs_is = '" & IIf(optBS.Checked = True, "BS", "IS") & "' WHERE id = " & txtKode.Tag.ToString
    End If
    objComp.setExecute(str, False)

    master_ChartofAccount.showCoa() 'error on this line : Reference to a non-shared member requires an object reference

    objComp.msgShow(Me, "Data berhasil di simpan.", "Simpan Berhasil", MessageBoxButtons.OK, MessageBoxIcon.Information)
    cmdClose_Click(sender, e)
 End Sub

why there's an error msg :

Reference to a non-shared member requires an object reference

on master_ChartofAccount.showCoa()

how to fix this ? is there other way to call a Sub from another Form?

i tried :

Dim frm as new master_ChartofAccount
frm.showCoa()

but the grid on master_ChartofAccount won't refresh. Thanks

MatSnow
  • 7,357
  • 3
  • 19
  • 31
XMozart
  • 879
  • 2
  • 10
  • 22
  • 3
    Possible duplicate of [Reference to a non-shared member requires an object reference occurs when calling public sub](https://stackoverflow.com/questions/13462479/reference-to-a-non-shared-member-requires-an-object-reference-occurs-when-callin) – MatSnow Nov 24 '17 at 07:33
  • pass your master_chartofAccount form to your coa_Create form – F0r3v3r-A-N00b Nov 24 '17 at 07:51
  • @F0r3v3r-A-N00b In fact, this is already done with `frm.ShowDialog(Me)`. You can access it with `Me.Owner` on `coa_Create` – MatSnow Nov 24 '17 at 07:55
  • Then you should cast the parent of coa_Create to the class type of master_chartofAccount – F0r3v3r-A-N00b Nov 24 '17 at 07:57

1 Answers1

0

Inside coa_Create, declare a property of master_ChartofAccount form type.

Public Property Master_ChartofAccount As <class name for your master_chartofaccount form>

Then in cmdAdd_Click

Private Sub cmdAdd_Click(sender As Object, e As EventArgs) Handles cmdAdd.Click
    Dim frm As New coa_Create
    frm.Master_ChartofAccount = Me
    frm.ShowDialog(Me)
End Sub

And in your cmdSave_Click

Me.MasterChartofAccount.showCoa()

OR

inside cmdSave_Click, just cast the parent to the class type of mast_chartofaccount

CType(Me.Parent, <class name of master chart of account>).showCoa()
F0r3v3r-A-N00b
  • 2,903
  • 4
  • 26
  • 36