Results 1 to 4 of 4
  1. #1
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61

    vba - search table for value

    Hi

    I want to make an if statement that looks for a matching value in a table´s column.

    Table name: table1
    column: name

    I have a value being held in a variable called "username" (dont know if im explaining this correctly)
    If i do a code
    Code:
     	 	msgbox username
    i do get the expected value i want... now i want to look if that value is stored in a table in a column.



    Code:
    If table1,column=name = username then
    msgbox "value found in table"
    else
    msgbox "value not found in table"
    end if
    from what i have read i should do a DLookup line.... but i dont know how to write that.
    Anyone able to help me?

  2. #2
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,159
    The syntax for a DLookup is DLookup("TheFieldValueYouWantreturned", "TheTableORQueryYouWanttoLookIn", "TheCriteria")
    So for your given example (this is a bit convoluted for demonstration purposes but should give you the idea)

    Code:
    Dim strAnswer as String
    
    strAnswer =  Nz(Dlookup("Name","Table1","[Name] = '" & username & "'") ,"Not Found")
    
    If strAnswer <> "Not Found" Then 
         MsgBox strAnswer & " Was found"
    Else
           MsgBox strAnswer 
    End If
    This will capture the event of it not being successful.
    Note that Name is a reserved word in Access so will normally need enclosing in square brackets.

    Edit : More syntax help here http://access.mvps.org/access/general/gen0018.htm
    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 ↓↓

  3. #3
    Behedwin is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Posts
    61
    Quote Originally Posted by Minty View Post
    The syntax for a DLookup is DLookup("TheFieldValueYouWantreturned", "TheTableORQueryYouWanttoLookIn", "TheCriteria")
    So for your given example (this is a bit convoluted for demonstration purposes but should give you the idea)

    Code:
    Dim strAnswer as String
    
    strAnswer =  Nz(Dlookup("Name","Table1","[Name] = '" & username & "'") ,"Not Found")
    
    If strAnswer <> "Not Found" Then 
         MsgBox strAnswer & " Was found"
    Else
           MsgBox strAnswer 
    End If
    This will capture the event of it not being successful.
    Note that Name is a reserved word in Access so will normally need enclosing in square brackets.

    Edit : More syntax help here http://access.mvps.org/access/general/gen0018.htm

    It kind of works... My "username" variable contain a name and even if that name is not in table1 i still get the message that "was found".
    So it does not seem to match to anything, it just gives true and display the messge.
    cant provoke the "not found" message.

    Edit:
    my misstake, i did an error and fixed it.
    works great now.

  4. #4
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,159
    Excellent - Good luck with the rest of your project!
    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 ↓↓

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

Similar Threads

  1. Search through a table
    By Thompyt in forum Programming
    Replies: 21
    Last Post: 03-20-2017, 06:41 PM
  2. Search changes the main table
    By cuddles in forum Forms
    Replies: 2
    Last Post: 05-12-2014, 01:37 PM
  3. Wilcard search using table
    By raytackettsells in forum Queries
    Replies: 6
    Last Post: 10-15-2013, 10:36 AM
  4. Replies: 25
    Last Post: 03-19-2013, 10:08 PM
  5. Replies: 1
    Last Post: 04-11-2012, 03:30 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