Results 1 to 14 of 14
  1. #1
    Lhoj is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2021
    Posts
    93

    DLookup as textbox source

    I have this particular field from a table I want to display in a textbox, so I'm trying to bring it up with a Dlookup as the control source. I know it should be doable and I managed to do it through VBA code but wanted to set it up in the control source and I can't find the proper syntax. The table is Laptops, field name is IMEI, criteria is SerialNum field must equal a value from a combobox from the same form, named Laptop.



    so I tried:

    Code:
    =DLookup("[IMEI]","Laptops","[SerialNum] = '" & [Laptop] & "'")
    Bad syntax, so I tried to play a bit with the different reference methods and the quotations and brackets, and tried:

    Code:
    =DLookup("[IMEI]","[Laptops]","[SerialNum] = '" & [Laptop] & "'")
    =DLookup("[IMEI]","Laptops","[SerialNum] = """ & [Laptop] & """'")
    =DLookup("[Laptops]![IMEI]","[Laptops]","[Laptops]![SerialNum] = '" & [Laptop] & "'")
    and every possible combination without luck. What's the correct syntax for a Dlookup when used as a control source?

    Thanks in advance!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    Your first effort looks fine to me.

    However, there is a better way.

    Include the IMEI field in the combobox RowSource SQL. Refer to that column by index in textbox expression. Combobox column index begins with 0. So if IMEI is in column 3 its index is 2.

    =[Laptop].Column(2)
    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.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Is laptop numeric?
    If so, no single quotes required

    You can always test the syntax in the immediate window as well.?

    June7's method is much better anyway, but for the next time you do need a DLookup() .....
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    Lhoj is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2021
    Posts
    93
    Nop, Laptop combobox contains the serial number of the laptop, which is text. Don't know why the first one (nor any other combination I've tried) works. Thanks for the tip about the combo box column reference June, that's actually what I was doing for another field, only in a total different way because I didn't know you could reference a column like that in the control source. Turns out if you copy the combobox, hide all but the column you want and turn it to a textbox it works too, but this is way cleaner. I had to add another table to the combobox's source query but it works perfectly.

    It still bothers me not being able to get the DLookup syntax right, but I like this solution better, thank you both very much!

  5. #5
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    When you do it in the textbox's rowsource, you must use semicolons instead of commas between parameters.

    Queries also - the SQL uses commas, the graphic grid uses semicolons. Access will switch them back and forth as you switch between views, but you must use the correct ones when you are editing either view manually.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by pdanes View Post
    When you do it in the textbox's rowsource, you must use semicolons instead of commas between parameters.
    Wrong, this works fine for me?

    Code:
    =DLookUp("ID","Table1","ID=" & [ID])
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I suspect it might be regionally "changed" depending where you are.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by Minty View Post
    I suspect it might be regionally "changed" depending where you are.
    My thoughts as well Minty, as I have seen plenty of examples from members on various sites.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    Quote Originally Posted by Welshgasman View Post
    Wrong, this works fine for me?

    Code:
    =DLookUp("ID","Table1","ID=" & [ID])
    Wrong.

    This works:
    Code:
    =Switch(Nz([txtPocetRozdil])='';'';Val(Nz([txtSumKusy]))=Val(Nz([txtKusyvKatalogu]));"Vše určené";Val(Nz([txtSumKusy]))<Val(Nz([txtKusyvKatalogu]));"Zbývá";Val(Nz([txtSumKusy]))>Val(Nz([txtKusyvKatalogu]));"Chyba")
    This throws an error, and will not even get saved in the source property box:
    Code:
    =Switch(Nz([txtPocetRozdil])='','',Val(Nz([txtSumKusy]))=Val(Nz([txtKusyvKatalogu])),"Vše určené",Val(Nz([txtSumKusy]))<Val(Nz([txtKusyvKatalogu])),"Zbývá",Val(Nz([txtSumKusy]))>Val(Nz([txtKusyvKatalogu])),"Chyba")

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    As already stated, the separator needed depends on your regional settings. In many countries such as the UK its a comma.
    If I entered the semicolon version that works for you, I would get an error.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    In the U.S., also use comma.
    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.

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    So where does the O/P reside?

    @lhoj?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    Lhoj is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2021
    Posts
    93
    Sorry, guys, really wished I had more time to invest in this project, I'm actually enjoying it, but hadn't touched it since I last posted.

    It's semicolons here in Spanish version of Access. I use so much VBA and so little access functions I had totally forgotten about that. So thanks you for the tip, pdanes. Once that was fixed it basically works with almost any syntax (with or without brackets for the fields, for example). With DBúsq (spanish version of DLookup) this worked fine:
    Code:
    DBúsq("IMEI";"Laptops";"SerialNum = '" & [Laptop] & "'")
    Anyway, I'm gonna stick to combobox column reference approach, but I'm glad we got this sorted out. Thank you all!

  14. #14
    pdanes is offline Competent Performer
    Windows 10 Access 2007
    Join Date
    Sep 2019
    Posts
    208
    Glad it helped.


    One additional note - you need brackets in such expressions when your field or table name contains 'bad' characters. Unfortunately, 'bad' means various things at various times. I can't give you a comprehensive list, but spaces are always bad, the standard 26 Latin characters are always good. I think underscores are also always good (except maybe as the first character, sometimes - I'm not 100% sure about that). For you, you may run into issues if you have Spanish characters, like ñ in your names. I work in Prague, and I've had problems with the special characters the Czech language uses. Sometimes it works, sometimes not, sometimes it works only with brackets. I avoid the issue by not using such characters anywhere in my own designs, but I do run across it occasionally when dealing with other people's work, so keep an eye out for that.

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

Similar Threads

  1. Replies: 9
    Last Post: 12-15-2016, 07:31 PM
  2. Textbox Control Source
    By asmores in forum Access
    Replies: 5
    Last Post: 02-19-2015, 09:59 AM
  3. DLookup in Control Source
    By bgephart in forum Forms
    Replies: 2
    Last Post: 08-28-2012, 02:06 PM
  4. Replies: 17
    Last Post: 02-08-2012, 10:06 AM
  5. Dlookup as Control Source
    By alsoto in forum Forms
    Replies: 1
    Last Post: 08-28-2011, 07:05 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