Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228

    code logic error

    hello,

    i have a function a boolean function called checkmeasurements, which checks to see if the numbers entered in three fields make sense, if one or more do not make sense, it should setfocus on the textbox in which the incorrect number is at.

    i also have a function called saveandexit, which does just that.



    i wrote the following code in the save button:

    Code:
    Private Sub btnSaveandExit_Click()
    
    Dim blncheck As Boolean
     
    blncheck = False
    blncheck = checkmeasurements
    
    
    
    
    If checkmeasurements Then
    Call saveandexitfcn
    End If
    
    
    End Sub
    when i click save btn, i want it to check the entries in those 3 text boxes, if the numbers are okay, then save, else, i want it to focus back on the box.

    right now, what it does is it checks them, tells you if they are invalid entries, then saves the record anyway. might be a simple fix, but i've tried a few things and cant get it to work properly,

    the two functions are below:

    Code:
    Private Function saveandexitfcn()
    
        Dim rst As DAO.Recordset
        Dim db As DAO.Database
        Set db = CurrentDb()
        Set rst = db.OpenRecordset("AfterEtchDieSize")
    
    
        rst.AddNew
        rst!Date_Time = Now()
        rst!Etch_Lot = Me.cboEtchLot
        rst!Measurement1 = Me.txtMeasurement1
        rst!Measurement2 = Me.txtMeasurement2
        rst!Measurement3 = Me.txtMeasurement3
        rst!Diode_Type = Me.TxtCutType
        rst!Operator = Me.txtOperator
        rst!Notes = Me.txtNotes
        rst!Part_Number = Me.txtPartNumber
        rst!Acid_Number = Me.txtacidnumber
        rst.Update
        rst.Close
        MsgBox ("Record saved!")
    End Function
    Code:
    Private Function checkmeasurements()
    
    
    
    
    
    If Me.TxtCutType = "Hex" And Me.txtcutsize = "0.125" Then  'condition
        If Val(Me.txtMeasurement1) > Val(0.10825) Then          'value comparison
            MsgBox ("Measurement entered is too large")
            Me.txtMeasurement1.SetFocus
            checkmeasurements = False
        ElseIf Val(Me.txtMeasurement2) > Val(0.10825) Then
             MsgBox ("Measurement entered is too large")
            Me.txtMeasurement2.SetFocus
            checkmeasurements = False
        ElseIf Val(Me.txtMeasurement3) > Val(0.10825) Then
            MsgBox ("Measurement entered is too large")
            Me.txtMeasurement3.SetFocus
            checkmeasurements = False
        End If
    ElseIf Me.TxtCutType = "Hex" And Me.txtcutsize = "0.093" Then
        If Val(Me.txtMeasurement1) > Val(0.08054) Then
            MsgBox ("Measurement entered is too large")
            Me.txtMeasurement1.SetFocus
            checkmeasurements = False
        ElseIf Val(Me.txtMeasurement2) > Val(0.08054) Then
            MsgBox ("Measurement entered is too large")
            Me.txtMeasurement2.SetFocus
            checkmeasurements = False
        ElseIf Val(Me.txtMeasurement3) > Val(0.08054) Then
            MsgBox ("Measurement entered is too large")
            Me.txtMeasurement3.SetFocus
            checkmeasurements = False
        End If
    ElseIf Me.TxtCutType = "Hex" And Me.txtcutsize = "0.063" Then
        If Val(Me.txtMeasurement1) > Val(0.05456) Then
            MsgBox ("Measurement 1 entered is too large")
            Me.txtMeasurement1.SetFocus
            checkmeasurements = False
        ElseIf Val(Me.txtMeasurement2) > Val(0.05456) Then
            MsgBox ("Measurement 2 entered is too large")
            Me.txtMeasurement2.SetFocus
            checkmeasurements = False
        ElseIf Val(Me.txtMeasurement3) > Val(0.05456) Then
            MsgBox ("Measurement 3 entered is too large")
            Me.txtMeasurement3.SetFocus
            checkmeasurements = False
        End If
    ElseIf Me.TxtCutType = "Hex" And Me.txtcutsize = "0.25" Then
        If Val(Me.txtMeasurement1) > Val(0.2165) Then
            MsgBox ("Measurement entered is too large")
            Me.txtMeasurement1.SetFocus
            checkmeasurements = False
        ElseIf Val(Me.txtMeasurement2) > Val(0.2165) Then
            MsgBox ("Measurement entered is too large")
            Me.txtMeasurement2.SetFocus
            checkmeasurements = False
        ElseIf Val(Me.txtMeasurement3) > Val(0.2165) Then
            MsgBox ("Measurement entered is too large")
            Me.txtMeasurement3.SetFocus
            checkmeasurements = False
        End If
    Else
        checkmeasurements = True
    End If

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    The form isn't bound to the table, is it? If not, have you set a breakpoint and stepped through the code so you can keep an eye on the variables and watch the flow of the code?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    does the save button code look fine?
    it seems like it should work okay, the table isn't bound, which im not a fan of, but it was made before i took over this db.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    It looked okay offhand, but the way to debug it is to set a breakpoint and follow the code. We're trying to learn how to fish here.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    so i stepped through the code, (which im not very good at) and in the appropriate If statement, it will show checkmeasurements as true (all of them in fact) after the setfocus line of code.
    i believe it should say false, that way btn SaveandExit wont call the function saveandexitfcn

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Is this line not being executed?

    checkmeasurements = False

    Can you post the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    hmm, i dont think it is being executed.
    the db is attached here:

    http://www.box.com/files#/files/0/f/...3/post_etch_db

    Post etch db

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Can you post it here? I have no desire to sign up for another site.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    PostEtchTest.zip

    had to battle with my computer, wasn't letting me upload to this site for some reason.
    it is attached now.
    Thanks a ton Pbaldy

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    So what would I enter to make the code fail? Perhaps your problem is in the check. I put in values that made it fail the first test. It correctly set the variable to False, but then continues down to the next test. The Else clause in that test set it back to True. Perhaps you want to add

    Exit Function

    after a failed test.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    any number greater than 1 should make the test fail.
    i think you're right, adding exit function after every checkmeasurements=false would do the trick.
    any clue as to why the else clause is setting it back to true?
    thought the else would be skipped if the If condition holds.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    The code I'm talking about isn't in your code above (which is why I didn't catch the logic flaw earlier). It's the section titled

    ' *** This part of code checks square measurements

    Since the test I made wasn't square, the Else clause of that series of tests changed the result, in my view inappropriately.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    ommmggaawwwww

    so true.

    if a Hex cut is in question and fails, it will set the focus, but reglardless. the code will continue to the other if statements. so if the next part of the code is to check squares, since me.txtcuttype is "hex" it skips the IF part of Square and goes to else, making Checkmeasurements=true.


    what would be the best way to fix this?
    should i make it all one big long If elseif statement?
    or exit function after every test?

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    My preference would be to avoid all that, because you've basically hard-coded business rules. Is it conceivable to have the values being tested in a table? Either added to the table the cut type and size are coming from, or a lookup table with fields for those 2 plus the value to test against? That would let you have one simple test of each of the 3 textboxes, either getting the test value (like 0.10825 in the first test) from the combo or the lookup table.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    mejia.j88 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Nov 2011
    Location
    california
    Posts
    228
    What do you mean by: Is it conceivable to have the values being tested in a table? The values I'm entering or the values I am testing against?
    i can definitely put hte values im testing agaisnt in a table; it will have to be queried because the cut type and size come from a table containing individual part numbers (a ton of part numbers).
    I'm not 100% sure i understand the method you are suggesting :-/


Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. need better VBA logic than I currently have
    By jscriptor09 in forum Programming
    Replies: 4
    Last Post: 11-27-2011, 09:16 AM
  2. Can't get the logic right.
    By Nouri31 in forum Queries
    Replies: 3
    Last Post: 03-31-2011, 08:25 AM
  3. Relationship Logic
    By Huddle in forum Access
    Replies: 2
    Last Post: 01-25-2011, 04:27 PM
  4. Nested If Statements Using Or (Logic Error)
    By IFA Stamford in forum Access
    Replies: 7
    Last Post: 12-30-2010, 08:53 AM
  5. Need help with code logic/consolidation
    By bg18461 in forum Programming
    Replies: 1
    Last Post: 03-31-2010, 04:19 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
  •  
Other Forums: Microsoft Office Forums