Solved

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

Posted on 2004-09-20
8
341 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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