Results 1 to 6 of 6

Thread: If Then Else Statement

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    9

    Question If Then Else Statement

    Although this is VBA (Access) some VB guru should be able to spot what I've done wrong...

    I have a form with 7 fields representing a record. The form has 7 unbound textboxes and the 7 fields are hidden. The form opens and the current values in the hidden fields are copied into the appropriate unbound box. The user can then edit any of the fields. They can then click the update button which will copy the values in the unbound boxes to the appropriate fields close the form and open the main menu form. This works fine.

    The poblem I have is when the user clicks a second button labled cancel. This button will check to see if there have been any changes. If changes have been made a modal message is displayed asking if the user is sure they want to disregard any changes they have made.

    The code for the button click goes like this....

    Private Sub Button2_Click()
    ' The cancel button
    'Check if data has been changed and take appropriate action
    If Field1 = ubField1 And Field2 = ubField2 And Field3 = ubField3 And Field4 = ubField4 And Field5 = ubField5 And Field6 = ubField6 And Field7 = ubField7 Then
    Call FncCloseForm 'Close the form and open Main Menu Form
    Else
    Call FncAreYouSure 'Open a modal message asking are you sure
    End If
    End Sub

    On clicking the button on a record that has not been changed I still get the Are you sure message box. Why? What have I done wrong? Is there a betterway of doing this?

    Hellllllp please

  2. #2
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Its opening the messagebox bvecasue your first argument with all the AND's is not true... it's false.

    Check to make sure all the fields = what you think they are suppost to.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    9

    Checked!

    As I mentioned above the message box is displayed on a record that has had NO changes made....

    The form loads... Fields transfered to unbound textboxes....

    Cancel button clicked (no changes made to any textboxes) and the message box appears.

    I placed a break on the IF line and checked each Field and ubField and they are identical therefore the THEN condition should run but it jumps to the ELSE statement....

    Any Ideas Anyone

  4. #4
    Addicted Member Pickler's Avatar
    Join Date
    May 2001
    Location
    nffanb
    Posts
    248
    You may want to add brackets around your fields.
    If (Field1 = ubField1) And (Field2 = ubField2) And etc....

    Another way to approach the problem is to use a Global/Public variable.
    ie:
    Public HasChanged as Boolean

    In your Change events for the fields you set HasChanged=True
    then in the CancelButton click event
    If haschanged=true then
    ' yadda yadda yadda
    else
    ' whatever
    end if

  5. #5
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Well it's not a problem with VB. It's not a problem with your code. It's a problem with the Fields not Equaling what you think they do.

    Here is an example of what you are doing except without the DB Fields. It works as it should.

    VB Code:
    1. Private Sub Command1_Click()
    2. If a = a And b = b And c = c And d = d And e = e And f = f Then
    3. MsgBox "No Problem"
    4. Else
    5. MsgBox "Somethings Wrong"
    6. End If
    7. End Sub

    But when you do it using tyou DB Fields it doesnt work. THerefor the Error lies in your DBFiled comparison.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    9
    Pickler said
    You may want to add brackets around your fields.
    If (Field1 = ubField1) And (Field2 = ubField2) And etc....

    This did not work

    Another way to approach the problem is to use a Global/Public variable.
    ie:
    Public HasChanged as Boolean

    In your Change events for the fields you set HasChanged=True
    then in the CancelButton click event
    If haschanged=true then
    ' yadda yadda yadda
    else
    ' whatever
    end if

    Great worked but had to change IF line to read

    If HasChanged Then
    Call FncAreYouSure ' the condition is true ther have been changes
    Else
    Call FncCloseForm
    End If


    THANKSSS

    Can Anybody help with my other query also posted today...
    See 'Goto Recored from Datasheet - Problem'
    Last edited by Mych; Mar 20th, 2002 at 09:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width