?
Solved

How to compare current and original values of multiple controls and when different my boolean HasChanges = True

Posted on 2004-09-20
8
Medium Priority
?
399 Views
Last Modified: 2010-04-23
Hi Experts,

I want to compare original and current values of some controls on a form and if there are diffrences make my boolean variable true, then say if true perform updates. Suppose that in the first comparison that runs a difference is found and the variable is set to true, how can I then make the code step through checking for any other differences, rather than going straight to the if true then statement? Is this something a For ... Next statement or the OR operand in the If ... Then statement could achieve?

Thanks in anticipation,
0
Comment
Question by:louise001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 7

Expert Comment

by:natloz
ID: 12107069
You need to be a little more clear on what you are trying to do. Give a walkthrough example...what controls and how are the comparisons triggered/when are the comparisons triggered?
0
 
LVL 7

Expert Comment

by:natloz
ID: 12107082
Normally I would do something like this if I understand you...

if cboUser.selectedvalue <> intSelectedUser then
  'Do some code to update User related things
else
  if cboTester.selectedvalue <> intSelectedTester then
    'Do some code to update Tester related things
  else
    if cboMachine.selectedvalue <> intSelectedMachine then
     'Do some code to update machine related things
    endif
  endif
endif
0
 
LVL 7

Expert Comment

by:natloz
ID: 12107086
Any of the IF statements are true...will then skip the rest...until the next iteration
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:fulscher
ID: 12107353
louise,

I probably would do something like this:

    Private Function CheckAllControls() As Boolean
        Dim x As System.Windows.Forms.Control
        Dim Modified As Boolean

        For Each x In Me.Controls
            If x.Tag <> GetOriginalValue(x) Then Return True
        Next
        Return False
    End Function

    Private Function SaveControlValues()
        Dim x As System.Windows.Forms.Control

        If CheckAllControls() Then
            For Each x In Me.Controls
                If x.Tag <> GetOriginalValue(x) Then SaveValue(x)
            Next
        End If
    End Function

In the Tag property of each control, I would store a key which allows me to find the original value of that control. If you're working with a database, this key could be, for example, the field name.

Hope this helps
Jan
0
 

Author Comment

by:louise001
ID: 12113750
I think natloz is right I need to be more specific: what I'm trying to do is update a database through a storedproc, for various reasons to do with data types and how the data needs to be displayed I can't just say if mydataset.haschanges = true then (perform update). So this is an example of what I've got at the moment:

Try
      'first name
            If Me.drowSelectedEmployee("first_name", DataRowVersion.Original) Is DBNull.Value Then
                Dim strFN As String = Me.drowSelectedEmployee("first_name", DataRowVersion.Original) & String.Empty
                If strFN <> Me.rtbFirstName.Text Then
                    HasChanges = True
                End If
            End If

            If Not Me.drowSelectedEmployee("first_name", DataRowVersion.Original) Is DBNull.Value Then
                If Me.drowSelectedEmployee("first_name", DataRowVersion.Original) <> Me.rtbFirstName.Text Then
                    HasChanges = True
                End If
            End If

       'Manager
            If Me.cboEmployeeManager.SelectedIndex = -1 Then
                uManager = SqlInt32.Null
            Else : uManager = New SqlInt32(Int32.Parse(Me.cboEmployeeManager.SelectedValue))
                If Me.drowSelectedEmployee("manager_id", DataRowVersion.Original) <> uManager.ToString Then
                    HasChanges = True
                End If
            End If

Catch ex As Exception
            MsgBox(ex.ToString)

GetValue()

End Try

Private Sub GetValue()

        If HasChanges = False Then
            Exit Sub
        ElseIf HasChanges = True Then
            If MsgBox("Confirm changes for " & (drowSelectedEmployee("known_as")) & " " & (drowSelectedEmployee("last_name")), MsgBoxStyle.YesNo, "Frontier Employees") = MsgBoxResult.Yes Then
                UpdateRecord()
            End If
            End If

    End Sub

(FYI code UpdateRecord() looks up the actual parameters)

The update procedure works fine, but it runs on the first instance of HasChanges = True rather that stepping through each comparison. I can see that I could have different update statements as fulscher suggests but I guess that's will have a performance overhead.

So what I'm looking for is how to make the code step through each value comparison and only then make my boolean HasChanges = True.

I'd be grateful for any guidance on this,

Louise



0
 
LVL 7

Expert Comment

by:natloz
ID: 12113821
You need to separate your comparisons with IF THEN ELSE IF statements...here is a simple example of me vaidating ALL Required fields on a Form before Adding/Updating the record in the database...You can use this same approach for comparisons....Because the IF statements are NESTED...whenever one evaluates to TRUE the rest do not get entered until the next iteration....

If cboBranch.SelectedValue = 0 Then
            MsgBox("Please select a Branch from the list.")
            cboBranch.Focus()
            bFail = True
        Else
            If txtSupplierName.Text = "" Then
                MsgBox("Please enter a Supplier Name.")
                txtSupplierName.Focus()
                bFail = True
            Else
                If txtAddress.Text = "" Then
                    MsgBox("Please enter a Supplier Address.")
                    txtAddress.Focus()
                    bFail = True
                Else
                    If txtCity.Text = "" Then
                        MsgBox("Please enter a Supplier City.")
                        txtCity.Focus()
                        bFail = True
                    Else
                        If txtProvince.Text = "" Then
                            MsgBox("Please enter a Supplier Province.")
                            txtProvince.Focus()
                            bFail = True
                        Else
                            If txtPostalCode.Text = "" Then
                                MsgBox("Please enter a Supplier Postal Code.")
                                txtPostalCode.Focus()
                                bFail = True
                            End If
                        End If
                    End If
                End If
            End If
        End If
0
 

Author Comment

by:louise001
ID: 12143974
Thanks natloz, that works & I've accepted your answer.
0
 
LVL 7

Accepted Solution

by:
natloz earned 375 total points
ID: 12144135
Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question