Set a Variable in VBA to be Used in SQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cynicon
    New Member
    • Feb 2010
    • 20

    Set a Variable in VBA to be Used in SQL

    Hi, everyone

    So this is really simple,

    I have an Access Table called "BDAXA", where there are many records, and a column called "Date".

    I use an imput box called (DAT) where I ask the user an especific month to generate a query in a format "mmyyyy", so lets say the user writes 022010, then DAT = 022010,

    I need to know how can I use that variable (DAT) in the sql languaje
    Code:
    strsql = SELECT BDAXA.Fecha
    FROM BDAXA
    WHERE (((BDAXA.Date)=[B]DAT[/B]));
    Last edited by NeoPa; Feb 12 '10, 07:33 PM. Reason: Please use the [CODE] tags provided
  • mshmyob
    Recognized Expert Contributor
    • Jan 2008
    • 903

    #2
    Try

    [code=vb]
    strsql = "SELECT BDAXA.Fecha FROM BDAXA WHERE (((BDAXA.Date) = #" & DAT & "# ));"[/code]
    cheers

    Comment

    • cynicon
      New Member
      • Feb 2010
      • 20

      #3
      Thanks so much mshmyob, you are really kind thanks for your answer, thanks thanks thanks, see ya

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32634

        #4
        That will often work but not always.

        Check out Literal DateTimes and Their Delimiters (#) for a fully portable solution.

        Comment

        Working...