Results 1 to 11 of 11
  1. #1
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33

    Lightbulb Help Using VBA with a table lookup



    Code:
    PrivateSub cboAsiaNoticeServiceContractCustomer_LostFocus()
    If [cboAsiaNoticeShipLine] = "FE APL" And[cboAsiaNoticeServiceContractCustomer] = "AMERINA, INC." Then
        [AsiaNoticeContractNumber] = "N/C"
    ElseIf [cboAsiaNoticeShipLine] = "FE Maersk" And[cboAsiaNoticeServiceContractCustomer] = "AMERINA, INC." Then
        [AsiaNoticeContractNumber] = "627157"
    ElseIf [cboAsiaNoticeShipLine] = "FE MOLU" And[cboAsiaNoticeServiceContractCustomer] = "AMERINA, INC." Then
       [AsiaNoticeContractNumber] = "US0000280"
    EndIf
      End Sub
    I'm looking for a better way to rewrite this code, because contract numbers change frequently it's become a pain to have edit the code.
    So my thought was to create a Lookup table containing , AsiaNoticeShipLine, AsiaNoticeServiceContractCustomer and AsiaNoticeContractNumber.
    Now that I have table with data, I looked at using Dlookup, but couldn't see how to use it with a Lookup table.

    I thought there might be a way to modify the current VBA code todo the lookup, but don't have the enough experience with VBA to know.

    Thank you in advance for any and all suggestion

    OldCityCat

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Can you describe this iin more detail?
    because contract numbers change frequently


    What does a ContractNumber represent? Why do they change frequently?


    Can you post a jpg of your tables and relationships?

  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,652
    You could use the form control method, with both fields in the criteria:

    http://access.mvps.org/access/general/gen0018.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33
    Canyou describe this in more detail?


    Whydo they change frequently?
    We will be receiving new contact numbers from our customer for the next 4 Months
    SoI will have to update the code with the new number in order to complete abooking request.


    Whatdoes a ContractNumber represent?
    ContractNumber is a field on a form - see attached file


    Canyou post a jpg of your tables and relationships?

    Thereare no relationships defined in this db
    Attached Thumbnails Attached Thumbnails BookingForm.jpg  

  5. #5
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33
    Paul,
    Thanks for the suggestion and link.
    Before I run off and try using Dlookup, I have another concern,
    You wouldn't know this because I posted only 3 of 200+ "If, ElseIf statements" from my code.
    With that in mind Is it still smart to use Dlookup as a replacement?

    Thanks
    OldCityCat

  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,652
    From the sound of it, sure, as long as they are all still looking at those 3 fields. You'd be replacing the 200+ If statements with a single DLookup. There could be 1,000 records in your lookup table, and it's still a single DLookup to find the 3rd field based on the 2 combos.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    OldCityCat is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    33
    Paul,
    Thanks that's music to my ears.
    With not having much experience with using Dlookup.
    If may ask how does this look ?
    Code:
      DLookup("AsiaNoticeContractNumber", "AsiaContractOwner","Criteria = '" & forms!frmAsiaNotice!cboAsiaNoticeShipLine  & forms!frmAsiaNotice!cboAsiaNoticeServiceContractCustomer& "'")

  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,652
    Mediocre.

    Try


    DLookup("AsiaNoticeContractNumber", "AsiaContractOwner"," AsiaNoticeShipLine= '" & forms!frmAsiaNotice!cboAsiaNoticeShipLine & "' And AsiaNoticeServiceContractCustomer = '" & forms!frmAsiaNotice!cboAsiaNoticeServiceContractCustomer & "'")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    The expression as constructed assumes the fields are text. If the value of combobox is actually a number ID then the code will probably fail. Don't use apostrophe delimiter for number fields. Also, use # delimiter with date fields.

    However, since your original code has been working, the comboboxes must be returning text values so just be aware of the use of delimiters in SQL statements, which the WHERE argument of domain aggregate functions is patterned off.
    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.

  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,652
    Quote Originally Posted by June7 View Post
    The expression as constructed assumes the fields are text. If the value of combobox is actually a number ID then the code will probably fail. Don't use apostrophe delimiter for number fields. Also, use # delimiter with date fields.
    If you read the first post, you'll see the values are text. All that was covered by the link as well.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Yeh, I went back and edited my post after a closer look at the first.
    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.

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

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2013, 03:13 PM
  2. Replies: 3
    Last Post: 06-28-2013, 01:59 PM
  3. Replies: 8
    Last Post: 03-22-2012, 08:48 AM
  4. Replies: 4
    Last Post: 02-12-2012, 02:58 PM
  5. Using lookup data for table 1 mutible times in table 2
    By mbjazz in forum Database Design
    Replies: 5
    Last Post: 04-26-2011, 01:18 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