Form Refresh Issue

ReportsNerd

Active member
Joined
Nov 24, 2012
Messages
31
Programming Experience
3-5
Hello, I am having trouble with a refresh on my main form. When I put a stop in my main form, it appears that my search variables are being sent correctly by the Search form's Button1_Click, but the main form is not being refreshed. Help appreciated. Here is my code so far:
VB.NET:
Public Class Menu
    Public Shared mySearchString As String
    Public Shared SearchField As String
    'Create variables for Master/Detail 
    Private masterBindingSource As New BindingSource()
    Private detailsBindingSource As New BindingSource()
Public Sub GetData()
        Try
            ' Specify a connection string:
            Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & Application.StartupPath & "\TestDB1.accdb'"
            Dim connection As New OleDb.OleDbConnection(connectionString)
            ' Create a DataSet. 
            Dim data As New DataSet()
            data.Locale = System.Globalization.CultureInfo.InvariantCulture
            ' Add data from the Debtor table to the DataSet. 
            Dim masterDataAdapter As _
            New OleDb.OleDbDataAdapter("select FILE_NO, NAME1, NAME2 from Debtor Where " & SearchField & " Like '" & mySearchString & "%'", connection)
           
 masterDataAdapter.Fill(data, "Debtor")
            ' Add data from the Transactions table to the DataSet. 
            Dim detailsDataAdapter As _
                New OleDb.OleDbDataAdapter("select FILE_NO, DATE, CODE, AMT, BAL, NOTES from TRANS ORDER By DATE DESC, CODE DESC", connection)
            detailsDataAdapter.Fill(data, "Trans")
            ' Establish a relationship between the two tables. 
            Dim relation As New DataRelation("DebtorTrans", _
                data.Tables("Debtor").Columns("FILE_NO"), _
                data.Tables("Trans").Columns("FILE_NO"), False)
            data.Relations.Add(relation)
            ' Bind the master data connector to the Debtor table.
            masterBindingSource.DataSource = data
            masterBindingSource.DataMember = "Debtor"
            ' Bind the details data connector to the master data connector using the DataRelation name to filter the information in the  
            ' details table based on the current row in the master table. 
            detailsBindingSource.DataSource = masterBindingSource
            detailsBindingSource.DataMember = "DebtorTrans"
            'Clear Main Tab Controls
            txtFileNo.DataBindings.Clear()
            txtName1.DataBindings.Clear()
            txtName2.DataBindings.Clear()
            
          
            'Populate Main Tab controls
            txtFileNo.DataBindings.Add("Text", masterBindingSource, "FILE_NO")
            txtName1.DataBindings.Add("Text", masterBindingSource, "NAME1")
            txtName2.DataBindings.Add("Text", masterBindingSource, "NAME2")
            
        Catch ex As OleDb.OleDbException
            MessageBox.Show("I must have messed up " & _
                "bad!.")
        End Try
    End Sub
Private Sub Menu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        
        Me.DEBTORTableAdapter.Fill(Me.DTdataset.DEBTOR)
        ' Bind the controls to the BindingSource components and load the data from the database.
       
        detailsDataGridView.DataSource = detailsBindingSource
        
        GetData()
            detailsDataGridView.AutoSizeColumnsMode = _
            DataGridViewAutoSizeColumnsMode.AllCells
    End Sub
Private Sub BtnSeek_Click(sender As System.Object, e As System.EventArgs) Handles BtnSeek.Click
       
        Dim ShowSearch As New Search()
        ShowSearch.Show()
    End Sub
'This is my search form. I want to collect the values entered on this form and pass it back to the query on my main form:
Public Class Search
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Menu As New Menu()
        Dim ChangeField As String = "Name1"
        If optName1.Checked Then
            ChangeField = "Name1"
        ElseIf optName2.Checked Then
            ChangeField = "Name2"
        ElseIf optSSN.Checked Then
            ChangeField = "SSN"
        End If
        Menu.SearchField = ChangeField
        Menu.mySearchString = txtSearch.Text
        Menu.GetData()
    End Sub
End Class
 
Hi,

You are going to have all sorts of problems here. When declaring object names you must ensure that you do not use existing class names within the .NET Framework and the VB language. As it is, the word Menu refers to the Menu class in .NET and therefore you are always going to get conflict issues when trying to refer to your parent form if it is called Menu.

So, and firstly, declare your objects with a prefix of the object type to ensure that you have a unique name within your project. So, your Menu form could become something like:-

VB.NET:
frmMenu

Once done, your next issue is that you are creating a new instance of your Menu Form by declaring this in your Search Form:-

VB.NET:
Dim Menu As New Menu

If you comment this out and change the name of the form as mentioned, then you will find that you will be able to access the current instance of your form by calling the name of the form i.e:-

VB.NET:
With frmMenu
  .SearchField = ChangeField
  .mySearchString = txtSearch.Text
  .GetData()
End With

Hope that helps.

Cheers,

Ian
 
Hi Ian, I made those changes, but I still cant get the frmMenu to run GetData() and refresh the controls. Dont know if it is a problem, but I'm getting a informational message under the
.SearchField and .mySearchString variables. The underline indicates: Access of shared member, constant member, enum member or nested type thru an insance; qualifying expression will not be evaluated.

I put a stop halfway down the GetData() back on my frmMenu and its definately passing in the variables from the search form. So that is good. Just no refresh.

My current code on the search form:
VB.NET:
Public Class Search
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim ChangeField As String = "Name1"
        If optName1.Checked Then
            ChangeField = "Name1"
        ElseIf optName2.Checked Then
            ChangeField = "Name2"
        ElseIf optSSN.Checked Then
            ChangeField = "SSN"
        End If
        With frmMenu
            .SearchField = ChangeField
            .mySearchString = txtSearch.Text
            .GetData()
        End With
    End Sub
End Class
 
Hi,

I have not, as yet, written an example of this on my own system to test your issue but I would suggest that you need to get rid of the Shared keyword on your Privately declared variables.

Give it a go and if this does not work then I will look into this in more depth and find out what is going wrong.

Cheers,

Ian
 
Thanks, Ian. I removed the Shared keyword from the variables, but I still cant get the controls to refresh on the main form. I tried putting in a stop again and it is passing the values to the variables OK (from the search form back to the main form). I just cant get it to refresh. Help appreciated.
 
Hi,

I have had another look at this now by converting your own code to use the Customers and Orders table from the Northwind Database. These are the only changes I have made to test your current logic. As it is, this works fine if you have made all the changes based on my previous posts so far.

If you have made all the changes correctly then this can only be down to an SQL string that returns no values? To prove or disprove this theory add the following two lines to the end of your GetData subroutine:-

VB.NET:
Try
  'your code etc...
  MsgBox(data.Tables(0).Rows.Count)
  MsgBox(data.Tables(1).Rows.Count)
Catch etc...
  'your code etc...
End Try

If your two tables result in a value of 0 then this proves that there are no records being returned from your tables via your search string. If the number of rows in your tables are greater than 0 then this should be working fine and we will need to look deeper into what is going wrong.

Hope that helps.

Cheers,

Ian
 
Hi Ian, I added the message box code lines and I am getting values back for both data tables. I know we have made some changes, so I have included all my current code below if that helps. I appreciate all the help. So, when I click the OK button on my search form, it doesn't hide or anything. It just passes the values into GetData(). But I'm not seeing my main form react to GetData() sub routine. Here is all of my current code:


VB.NET:
Public Class frmMenu
    Public mySearchString As String
    Public SearchField As String
    'Create variables for Master/Detail 
    Private masterBindingSource As New BindingSource()
    Private detailsBindingSource As New BindingSource()
Public Sub GetData()
        Try
            ' Specify a connection string:
            Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & Application.StartupPath & "\TestDB1.accdb'"
            Dim connection As New OleDb.OleDbConnection(connectionString)
            ' Create a DataSet. 
            Dim data As New DataSet()
            data.Locale = System.Globalization.CultureInfo.InvariantCulture
            ' Add data from the Debtor table to the DataSet. 
            Dim masterDataAdapter As _
            New OleDb.OleDbDataAdapter("select FILE_NO, NAME1, NAME2, ADDR1, ADDR2, ADDR3, WORK, NOTES, COURT, CASE_NUM, LOAN_NUM, DEALERNO, ATTY_PER, INT_RATE, PHONEW, PHONEH, PHONEO, STATUS, CLIENT, ACCR_FEE, CODE, DATE, AMT, LST_PMTA, TOTAL_PMT, ACCR_COST, ACCR_INT, TOTAL_INT, LST_PMTD, LST_TRAN  from Debtor Where " & SearchField & " Like '" & mySearchString & "%'", connection)
            masterDataAdapter.Fill(data, "Debtor")

            ' Add data from the Transactions table to the DataSet. 
            Dim detailsDataAdapter As _
                New OleDb.OleDbDataAdapter("select FILE_NO, DATE, CODE, AMT, BAL, NOTES from TRANS ORDER By DATE DESC, CODE DESC", connection)
            detailsDataAdapter.Fill(data, "Trans")
            ' Establish a relationship between the two tables. 
            Dim relation As New DataRelation("DebtorTrans", _
                data.Tables("Debtor").Columns("FILE_NO"), _
                data.Tables("Trans").Columns("FILE_NO"), False)
            data.Relations.Add(relation)
            ' Bind the master data connector to the Debtor table.
            masterBindingSource.DataSource = data
            masterBindingSource.DataMember = "Debtor"
            ' Bind the details data connector to the master data connector using the DataRelation name to filter the information in the  
            ' details table based on the current row in the master table. 
            detailsBindingSource.DataSource = masterBindingSource
            detailsBindingSource.DataMember = "DebtorTrans"
            MsgBox(data.Tables(0).Rows.Count)
            MsgBox(data.Tables(1).Rows.Count)

            'Clear Main Tab Controls
            txtFileNo.DataBindings.Clear()
            txtName1.DataBindings.Clear()
            txtName2.DataBindings.Clear()
          
            'Clear Financial Tab Controls
            fIRPercent.DataBindings.Clear()
         
            'Populate Main Tab controls
            txtFileNo.DataBindings.Add("Text", masterBindingSource, "FILE_NO")
            txtName1.DataBindings.Add("Text", masterBindingSource, "NAME1")
            txtName2.DataBindings.Add("Text", masterBindingSource, "NAME2")
           
            'Populate Financials Tab Controls
            fIRPercent.DataBindings.Add("Text", masterBindingSource, "INT_RATE")
          
        Catch ex As OleDb.OleDbException
            MessageBox.Show("Yikes " & _
                "Learn to code n00b! " & _
                "or get help.")
        End Try
    End Sub
Private Sub Menu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        
        Me.DEBTORTableAdapter.Fill(Me.DTdataset.DEBTOR)
        ' Bind the controls to the BindingSource components and load the data from the database.
               detailsDataGridView.DataSource = detailsBindingSource
        SearchField = "Name1"
        'GetData()
           detailsDataGridView.AutoSizeColumnsMode = _
            DataGridViewAutoSizeColumnsMode.AllCells
    End Sub
Private Sub BtnSeek_Click(sender As System.Object, e As System.EventArgs) Handles BtnSeek.Click
       
        Dim ShowSearch As New Search()
        ShowSearch.Show()
    End Sub
Public Class Search
'This is my serach form!
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim ChangeField As String = "Name1"
        If optName1.Checked Then
            ChangeField = "Name1"
        ElseIf optName2.Checked Then
            ChangeField = "Name2"
        ElseIf optSSN.Checked Then
            ChangeField = "SSN"
        End If
        With frmMenu
            .SearchField = ChangeField
            .mySearchString = txtSearch.Text
            .GetData()
        End With
    End Sub
End Class

Thanks!
 
Hi,

On the basis that you have confirmed that the two data tables are populated after the Search string has been queried against the database then I am struggling as to what to suggest next.

The one thing that I did do to confirm that the data and relationship were returned and constructed correctly was to add two DataGridViews and set their DataSources as follows:-

VB.NET:
DataGridView1.DataSource = masterBindingSource
DataGridView2.DataSource = detailsBindingSource

Give this a try to see if there get populated correctly. If they do, as I expect they should, then there is a "Red Herring" somewhere throwing you onto the wrong track.

Hope that helps.

Cheers,

Ian
 
Back
Top