Results 1 to 13 of 13
  1. #1
    bgeorge12 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Location
    Eustis, FL
    Posts
    25

    If statement help please

    I have the following code:



    Private Sub Unit_Price_GotFocus()
    If Forms![Order Details]![Line] = "[Oakhill Cath]" Then
    Me![Unit Price] = DLookup("[Price]", "[Oakhill Cath]", "[Product ID]='" & Forms![Order Details]![sbfOrderDetails].Form![Product ID] & "'")
    Else
    Me![Unit Price] = 500#
    End If
    End Sub

    My structure is a main form named Orders Details with a subform sbfOrderDetails. On the main form is a combo box named Line. The subform contains the fields Product ID (which is a combo box) and Price.

    When I run this it returns 500 even if the Line field says Oakhill Cath. It seems to not be recognizing it. What have I done wrong?

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Where is this code? Is it on the main form? If so, it would be:
    Code:
    Private Sub Unit_Price_GotFocus()
    If Me.[Line] = "[Oakhill Cath]" Then
       Me![Unit Price] = DLookup("[Price]", "[Oakhill Cath]", "[Product ID]='" & Me.sbfOrderDetails.Form![Product ID] & "'")
    Else
       Me![Unit Price] = 500
    End If
    End Sub
    But that really depends on if the name of the subform CONTROL (control on the parent form which HOUSES the subform) is named sbfOrderDetails. You use the subform control name and not the subform name unless they are named the same. So, let's say the control is really named sbfOrderDetails subform, then you would use
    Code:
       Me![Unit Price] = DLookup("[Price]", "[Oakhill Cath]", "[Product ID]='" & Me.[sbfOrderDetails subform].Form![Product ID] & "'")

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Does the value in the combo really contain the brackets? Or a numeric ID value? Set a breakpoint and step through the code so you can see what the combo is actually returning.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    bgeorge12 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Location
    Eustis, FL
    Posts
    25
    The combo box Line (on the main form) is tied to a table that lists the names of different lines of cabinets. The field is text. I am putting this code on the subform as a get focus event for Unit Price.

  5. #5
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    If the code is on the subform, then you would reference Product ID by using

    Me.[Product ID]

  6. #6
    bgeorge12 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Location
    Eustis, FL
    Posts
    25
    Okay I think I have the code as you suggested:
    Private Sub Unit_Price_GotFocus()
    If Forms![Order Details]![Line] = "[Oakhill Cath]" Then
    Me![Unit Price] = DLookup("[Price]", "[Oakhill Cath]", "[Product ID]='" & Me![Product ID] & "'")
    Else
    Me![Unit Price] = 500
    End If
    End Sub

    It still returns 500 even if Line has the text Oakhill Cath in it.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Maybe this line:
    If Forms![Order Details]![Line] = "[Oakhill Cath]" Then

    Should be:
    If Forms![Order Details]![Line] = "Oakhill Cath" Then
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    bgeorge12 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Location
    Eustis, FL
    Posts
    25
    Just tryed that. Still returns 500.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You need to validate that the combo is returning what you think it is:

    Quote Originally Posted by pbaldy View Post
    Set a breakpoint and step through the code so you can see what the combo is actually returning.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    bgeorge12 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Location
    Eustis, FL
    Posts
    25
    Sorry pbaldy I do not understand "set a breakpoint". The combo box just shows what is in the only field of a table.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    See if Jason's FAQ helps:

    http://www.baldyweb.com/Debugging.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    bgeorge12 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Location
    Eustis, FL
    Posts
    25
    Thank all of you! Got it to work!

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What was the problem?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Help on an IIF statement
    By Madmax in forum Access
    Replies: 4
    Last Post: 06-23-2011, 12:02 PM
  2. iff Statement
    By tkandy in forum Access
    Replies: 0
    Last Post: 03-20-2011, 02:31 PM
  3. if statement
    By sirlosi in forum Queries
    Replies: 4
    Last Post: 03-07-2011, 11:26 AM
  4. If Then Statement Help
    By Kapelluschsa in forum Programming
    Replies: 5
    Last Post: 08-11-2010, 09:24 AM
  5. IIf Statement
    By dref in forum Forms
    Replies: 2
    Last Post: 07-16-2010, 02:46 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