Solved

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

Posted on 2004-09-20
8
354 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 125 total points
ID: 12144135
Thanks.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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