Results 1 to 7 of 7
  1. #1
    swagger18 is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2011
    Posts
    19

    If statements

    Having a slight issue with IF statements.

    Private Sub TBS_TanninPercent_Enter()
    If TBS_Base1 = "TBS BASE" Then
    TBS_TanninPercent = TBS_Base1Ratio * 2.5 * TBS_EstJuiceContent / 2.5


    ElseIf TBS_Base1 = "Dabinett Base" Then
    TBS_TanninPercent = TBS_Base1Ratio * 2.5 * TBS_EstJuiceContent / 2.5
    ElseIf TBS_Base1 = "DAB/RS" Then
    TBS_TanninPercent = TBS_Base1Ratio * 2.5 * TBS_EstJuiceContent / 2.5
    Else
    TBS_TanninPercent = 0.2 * 100 / 2.5
    End If
    End Sub

    Thats my code but when i enter into the TanninPercent field and Base1 is BS Base it only uses the 0.2 * 100 / 2.5 formulae and ignores the if statement and same for the others.

    Any help would be greatly appreciated.

  2. #2
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    I think using 'Switch' and 'Case' is better practice than loads of Else IF statements. Might be wrong...

    But anyway, obvious question firstly: Are you sure that you are putting the criteria for the else if statements to work? As in the field text/name must be exact (Unless you use '*' or 'like')

  3. #3
    swagger18 is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2011
    Posts
    19
    Would it be possible for you to show me an example of switch or case statements?

    In regards to your question everything is correct in terms of the criteria i have copied the names of the field and pasted them into the code itself.

    My only other possible thought is as the back end is SQL and the value of the field is decimal(4, 2) maybe it could be that? But usually it errors saying about the field not being big enough to accept what is trying to be entered.

  4. #4
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    Code:
    Private Sub TextBox_AfterUpdate() 
    If Not IsNull(Me![TextBox]) Then 
    'No Variables are declared 
      Select Case Me![TextBox] 
        Case Is < 0 
          E = m * c ^ 2 
        Case 0 
          Pi = 4 * Atn(1#) 
        Case 1 To 4 
          j = -b + Sqr(453) 
        Case 5, 6 
          a = b + c 
        Case 7 
          R = Pi * R ^ 2 
        Case 8 
          P = 2 * (l + w) 
        Case 9 
          gr = Rnd() 
        Case 10 
          a = ((b1 + b2) / 2) * (a ^ 2) 
        Case "..."      'etc., etc. 
        Case Else 
          'General Formula for all 'other' possibilities 
      End Select 
    End If 
    End Sub
    I know that if you attempt to convert a date in SQL server, and the field is too small for the date (i.e yyyymmdd to yyyy/mm/dd) it will still put as many characters into the field as possible.

    Obvious things to check for would be field formats etc, but i'm not good in VBA so cannot really help you with the code much.

  5. #5
    swagger18 is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2011
    Posts
    19
    ummm this is annoying

    I have tried the case code as below:

    Private Sub TBS_TanninPercent_Enter()
    If Not IsNull(Me![TBS_Base1]) Then
    Select Case Me![TBS_Base1]
    Case Is = "BS Base"
    TBS_TanninPercent = TBS_Base1Ratio * 2.5 * TBS_EstJuiceContent / 2.5
    Case Else
    TBS_TanninPercent = 0.2 * 100 / 2.5
    End Select
    End If

    Yet it still only takes the bottom formulae and not the top one when its criteria applies.

  6. #6
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    Hopefully someone else might be able to tell you the answer, but what does the debugger do when you get to the first Case/If Else ?
    Does it just ignore it altogether? That should give you an idea of what the problem might be.

  7. #7
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by swagger18 View Post
    ummm this is annoying

    I have tried the case code as below:

    Private Sub TBS_TanninPercent_Enter()
    If Not IsNull(Me![TBS_Base1]) Then
    Select Case Me![TBS_Base1]
    Case Is = "BS Base"
    TBS_TanninPercent = TBS_Base1Ratio * 2.5 * TBS_EstJuiceContent / 2.5
    Case Else
    TBS_TanninPercent = 0.2 * 100 / 2.5
    End Select
    End If

    Yet it still only takes the bottom formulae and not the top one when its criteria applies.
    • replace "me!" with "me." (there are documented problems with "me!" throughout the various versions of acc)
    • use "case =" instead of "case is ="
    • if "TBS_tanninPercent" is a form control and NOT a variable, add "me." before it
    • make sure you differentiate NULL from "" in your code (isnull() = false if the value is "")

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Multiple IIF statements
    By KevinMCB in forum Queries
    Replies: 4
    Last Post: 12-03-2010, 01:35 PM
  2. IIF statements?
    By staceyo in forum Queries
    Replies: 15
    Last Post: 09-28-2010, 08:45 AM
  3. SQL statements from VBA
    By John Southern in forum Programming
    Replies: 12
    Last Post: 05-16-2010, 01:07 PM
  4. IIF Statements
    By JDA2005 in forum Queries
    Replies: 8
    Last Post: 07-07-2009, 04:24 PM
  5. If statements
    By Ezeecopy in forum Access
    Replies: 0
    Last Post: 03-24-2009, 04:54 AM

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