Results 1 to 3 of 3
  1. #1
    rustynails is offline Novice
    Windows 8 Access 2007
    Join Date
    Jul 2013
    Posts
    25

    Passing variable in DLookup (VBA)

    I am asking the user to input date. I am using the month of that date to look up month name (5 is may, 6 is june, etc). I have a ref table called months with month_id and month_name (1-january, 2-february, etc). I am writing this in VBA:


    Code:
    Dim FileDate as Date
    
    Dim DateString as String
    
    Dim monthname as String
    
    DateString = InputBox("Enter the file date in MM/DD/YYYY format", "Title")
    
    FileDate = DateValue(DateString)
    
    monthname = DLookup("[month_name]", "[months]", "Format(Month(FileDate),'0') = [month_id]")
    But it is giving me error. If I use "Format(Month(Date()),'0') = [month_id]" it is working correctly, but apparently it does not like it when I pass a variable. What am I doing incorrectly?



    Edit: I understand that there is MonthName function, but I am doing this because I have a need for month names in Spanish, so that's why I created a list of month names in a table in Spanish

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    It doesn't work because the variable is within quotes. This means the literal string "FileDate" is passed, not the contents of FileDate. Need to concatenate variables.

    The Format function should not be necessary.

    monthname = DLookup("[month_name]", "[months]", Month(FileDate) & " = [month_id]")

    Also, if user mistypes entry your code will still attempt the lookup. Should have some validation or other error handling code. Like:

    If IsDate(DateString) 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.

  3. #3
    rustynails is offline Novice
    Windows 8 Access 2007
    Join Date
    Jul 2013
    Posts
    25
    Quote Originally Posted by June7 View Post
    It doesn't work because the variable is within quotes. This means the literal string "FileDate" is passed, not the contents of FileDate. Need to concatenate variables.

    The Format function should not be necessary.

    monthname = DLookup("[month_name]", "[months]", Month(FileDate) & " = [month_id]")

    Also, if user mistypes entry your code will still attempt the lookup. Should have some validation or other error handling code. Like:

    If IsDate(DateString) Then
    ...
    Oooo got it. Thank you june7 you are awesome as always.

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

Similar Threads

  1. Passing a variable back from a sub
    By RonL in forum Programming
    Replies: 7
    Last Post: 01-25-2013, 12:10 AM
  2. Variable Not passing to query
    By chris.williams in forum Queries
    Replies: 2
    Last Post: 10-14-2011, 08:30 AM
  3. passing a variable to an append query
    By Baroj Von Reich in forum Programming
    Replies: 4
    Last Post: 09-02-2011, 08:08 AM
  4. Passing SQL result into variable
    By jonny in forum Access
    Replies: 3
    Last Post: 10-18-2009, 07:46 AM
  5. Passing a variable to a form
    By cjamps in forum Forms
    Replies: 0
    Last Post: 03-02-2009, 05:32 AM

Tags for this Thread

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