Results 1 to 7 of 7
  1. #1
    ngooo is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    3

    DoCmd.RunSql with a variable

    Hi Folks,

    We have a database where documents are due based on Order dates. We prefer all starting points to be on a Friday.


    First part, I check for the day it was entered, and store the new order date as FloatOrderDate. That part works no problem as i have check the output via message box.

    I am having an issue with running the docmd.runsql, as I am trying to update a table based on that stored date- FloatOrderDate. How do i put that FloatOrderDate into the RunSQL?


    Code:
    Dim formatorderdate As StringDim FloatOrderDate As String
    
    
    formatorderdate = Format(Forms!newvdlform!OrderDate, "dddd")
        If formatorderdate = "monday" Then
        FloatOrderDate = Forms!newvdlform!OrderDate + 4
        ElseIf formatorderdate = "tuesday" Then
        FloatOrderDate = Forms!newvdlform!OrderDate + 3
        ElseIf formatorderdate = "wednesday" Then
       FloatOrderDate = Forms!newvdlform!OrderDate + 2
        ElseIf formatorderdate = "thursday" Then
        FloatOrderDate = Forms!newvdlform!OrderDate + 1
        ElseIf formatorderdate = "friday" Then
        FloatOrderDate = Forms!newvdlform!OrderDate
    End If
    DoCmd.RunSQL "UPDATE WeeksTable SET WeeksTable.ConvertedDate = FloatOrderDate+7*[WEEKSTABLE].[weeksvalue] WHERE WeeksTable.datetype = 'aro' "
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You have to concatenate it into the string, using appropriate delimiters. For a date:

    SET WeeksTable.ConvertedDate = #" & FloatOrderDate & "# +7*[WEEKSTABLE].[weeksvalue] WHERE
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    Why not use

    Dim MySQL as string
    MySQL = “update... and rest of sql startement”

    Debug.print MySQL

    And look for your sqlstatement in the immediate window.

    I’d say your date variable needs delimiters. #......#

  4. #4
    ngooo is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    3
    Quote Originally Posted by pbaldy View Post
    You have to concatenate it into the string, using appropriate delimiters. For a date:

    SET WeeksTable.ConvertedDate = #" & FloatOrderDate & "# +7*[WEEKSTABLE].[weeksvalue] WHERE

    works perfectly. The quick help is greatly appreciated.

    I am always running into trouble with delimiters. Is there a good site to read up on delimiters.
    Thanks a bunch.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Jason has a faq here:

    http://www.baldyweb.com/BuildSQL.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    ngooo is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    3
    Quote Originally Posted by pbaldy View Post


    Really useful. I was going to bother you folks with another one, as I was trying to apply the date delimiter above on another one but after reading the faq, discovered dates/numbers/strings are handled differently.

    Thanks a bunch.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Glad it helped and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2016, 05:09 AM
  2. DoCmd.RunSQL help!
    By stephenchan433 in forum Access
    Replies: 7
    Last Post: 11-14-2015, 06:35 PM
  3. Using string variable in DoCmd.RunSQL
    By dcdimon in forum Programming
    Replies: 4
    Last Post: 05-08-2014, 09:40 AM
  4. DoCmd.RunSQL
    By Access_Blaster in forum Programming
    Replies: 6
    Last Post: 07-23-2013, 09:27 PM
  5. my first DoCmd.RunSQL
    By broecher in forum Programming
    Replies: 4
    Last Post: 11-05-2010, 09:35 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