Solved

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

Posted on 2004-09-20
8
374 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get sourcecode path 14 61
Sending HTML Formatted Email from ASP.NET 2 55
sql server connection string in config file 4 41
Updating Printer Details in VB.Net 16 42
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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