If function problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jaroslavu
    New Member
    • Dec 2011
    • 6

    If function problem

    Hello everyone, I got some problems with one of my codes. I am making a database where a person can tick boxes and chose 60credits from existing 120. But somehow my if function does not calculate the total..and at the end I get Message "Please chose 30credits per semester"

    I would appreciate some help! Thank you :)

    Code:
    
    Option Compare Database
    
    Private Sub ModuleOptions_Click()
    
    Dim International_Business_1 As Integer
    Dim Business_Programming_1 As Integer
    Dim Decision_Making As Integer
    Dim Change_Management As Integer
    Dim Business_Planning As Integer
    Dim Small_Business_Issues As Integer
    Dim Decision_Analysis As Integer
    Dim International_Business_2 As Integer
    Dim Business_Programming_2 As Integer
    Dim Business_Finance As Integer
    Dim Corporate_Strategy As Integer
    Dim Career_Management As Integer
    Dim Business_Ethics As Integer
    Dim Corporate_Finance As Integer
    Dim Semester1 As Integer
    Dim Semester2 As Integer
    
    Semester1 = 0
    Semester2 = 0
    
    If International_Business_1 = 0 And International_Business_2 = 20 Then
        MsgBox "You cannot select International Business 2 in semester 2 unless International Business 1 in semester 1 is selected.", vbCritical, "Incorrect choice"
        Exit Sub
    End If
    
    If Business_Programming_1 = 0 And Business_Programming_2 = 20 Then
        MsgBox "You cannot select Business Programming 2 in semester 2 unless Business Programming 1 in semester 1 is selected.", vbCritical, "Incorrect choice"
        Exit Sub
    End If
        
    If Decision_Making = 20 And Decision_Analysis = 10 Then
        MsgBox "You cannot select both Decision Making and Decision Analysis because of the common material shared", vbCritical, "Incorrect choice"
        Exit Sub
    End If
        
    If Business_Finance = 20 And Corporate_Finance = 10 Then
        MsgBox "You cannot select both Business Finance and Corporate Finance because of the common material shared", vbCritical, "Incorrect choice"
        Exit Sub
    End If
    
    If Business_Planning = 10 And Corporate_Strategy = 10 Then
        MsgBox "You cannot select both Business Planning and Corporate Strategy because of the common material shared", vbCritical, "Incorrect choice"
        Exit Sub
    End If
    
    If Management_Dissertation = 20 Then
    
        Semester1 = Semester1 + 10
        Semester2 = Semester2 + 10
    End If
    
    InternationalBusiness1.Value = International_Business_1
    BusinessProgramming1.Value = Business_Programming_1
    DecisionMaking.Value = Decision_Making
    ChangeManagement.Value = Change_Management
    BusinessPlanning.Value = Business_Planning
    SmallBusinessIssues.Value = Small_Business_Issues
    DecisionAnalysis.Value = Decision_Analysis
    InternationalBusiness2.Value = International_Business_2
    BusinessProgramming2.Value = Business_Programming_2
    BusinessFinance.Value = Business_Finance
    CorporateStrategy.Value = Corporate_Strategy
    CareerManagement.Value = Career_Management
    BusinessEthics.Value = Business_Ethics
    CorporateFinance.Value = Corporate_Finance
    
    Semester1 = (Semester1 + International_Business_1 + Business_Programming_1 + Decision_Making + Change_Management + Business_Planning + Small_Business_Issues + Decision_Analysis)
    Semester2 = (Semester2 + International_Business_2 + Business_Programming_2 + Business_Finance + Corporate_Strategy + Career_Management + Business_Ethics + Corporate_Strategy)
    
    If Semester1 <> 30 Or Semester2 <> 30 Then
        MsgBox "Each semester should consist of 30 credits.", vbOKOnly + vbCritical, "Incorrect choice"
        Exit Sub
    Else
        MsgBox "Thank you for your time. Confirmation letter has been sent to your email adress.", vbOKOnly + vbExclamation, "Choice confirmation"
    End If
    
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.OpenReport "Module Options", acViewPreview, , "[StudentID] = '" & [StudentID] & "'"
    
    End Sub
  • Mihail
    Contributor
    • Apr 2011
    • 759

    #2
    Jar !
    From my practice I can say that more conditions after IF = more troubles.
    Of course, the full skilled coders can handle that very well. But only they.
    My advice is to use parenthesis in order to say to VBA exactly what you wish. As example:

    Instead:
    Code:
    If International_Business_1 = 0 And International_Business_2 = 20 Then


    use this:
    Code:
    If (International_Business_1 = 0) And (International_Business_2 = 20) Then


    Good luck !

    Comment

    • Jaroslavu
      New Member
      • Dec 2011
      • 6

      #3
      Dear Mihail,

      Thank for your time, but your solution does not solve my problem. I tried to use brackets in my if statements and it still does not calculate the variables..

      Jaroslav ;(

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32634

        #4
        Well, let's start with the basics and send you to base-camp (When Posting (VBA or SQL) Code) first. When we have a proper question we can think about looking at it for you.

        Comment

        • Jaroslavu
          New Member
          • Dec 2011
          • 6

          #5
          Dear NeoPa,

          Sorry for misunderstandin g, I am beginner at VBA for Access. The code that I have written is the one that goes on button. There is a Form where a user can tick the boxes and select modules he do want to study. After a selection of boxes user presses button and the IF function that I have written above should state whether 60credits in total (30 1semester and 30 2semester) were selected. I tried different approached to code this, but somehow I think that, there might be a mistake in line 73 and 74..but I am not sure about that.

          Will appreciate any sort of help.

          Thank You

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32634

            #6
            I did just try to help - but you've ignored it.

            Go back to the linked page and at least follow the instructions found there. Being a beginner in Access is no reason for failing to follow simple instructions on what you must do when asking a question.

            Comment

            • Jaroslavu
              New Member
              • Dec 2011
              • 6

              #7
              NeoPa,

              Sorry probably you did get me wrong. The problem is that I did debug and it does not show any errors at all. Everything seems to work fine, but at the end - when i chose the options..no matter what options I chose 10 or 120credits it shows the same message that is on the line 77

              Code:
              "Each semester should consist of 30 credits."

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                You've created a bunch of integer variables that you never initialize. Therefore, their value is going to be 0. You then try to compare those variables to constants. Which isn't going to get you anywhere because they're never going to match since they're always 0. And then you attempt to set some, what I assume to be, controls with the values of those variables, which is always 0. And finally, you add those variables together. Which is always 0. So it doesn't matter what you check, it's always 0.

                Comment

                • Jaroslavu
                  New Member
                  • Dec 2011
                  • 6

                  #9
                  Hmm, maybe you have suggestions how I could solve this problem?

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32634

                    #10
                    Originally posted by Jaroslavu
                    Jaroslavu:
                    The problem is that I did debug and it does not show any errors at all.
                    I assume you're referring to a compile. I'm glad you at least managed to follow that instruction, though it would have been almost pointless without the Option Explicit unfortunately.
                    Originally posted by Jaroslavu
                    Jaroslavu:
                    Sorry probably you did get me wrong.
                    Your code doesn't have Option Explicit set. Compiling without that is more than 50% useless. This is the third post on the same issue. In what way did I remotely get you wrong?

                    I suspect Rabbit has outlined your main problems for you anyway, but I refuse to work on code that doesn't even have the basics done properly first. It's not like it's complicated or anything.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32634

                      #11
                      Originally posted by Jaroslavu
                      Jaroslavu:
                      Hmm, maybe you have suggestions how I could solve this problem?
                      Determine in your head first what you want the code to do. When this is clear and makes sense, create code to implement this logic. When such situations are clearly understood these types of errors are pretty rare.

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        My suggestion, if it wasn't clear from my overuse of the phrase, is to initialize your variables so they're not 0. But even if you do that, the logic doesn't make much sense. I can only tell you what is wrong with your current code, I can't tell you what your logic is.

                        Comment

                        • Jaroslavu
                          New Member
                          • Dec 2011
                          • 6

                          #13
                          Sorry for annoying you..and Thanks for help. But to be honest I don't have any patience any more to work on this code..its jus driving me crazy

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32634

                            #14
                            It sounds like software development isn't really your thing. SD takes a particular way of thinking which doesn't seem to be your way. That's only a bad thing if you think it is. Many people do perfectly well in other areas.

                            Comment

                            • Rabbit
                              Recognized Expert MVP
                              • Jan 2007
                              • 12517

                              #15
                              Let's use a simpler example and see if you notice where you went wrong.
                              Code:
                              Dim x As Integer
                              
                              If x = 10 Then
                                 MsgBox "Success"
                              End If
                              What's x? When will you see the message box? Why is it impossible for the message box to show? If we ignore all business logic, this example shows why your code doesn't work.

                              Comment

                              Working...