Solved

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

Posted on 2004-09-20
8
311 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now