Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216

    Don't know how to make an update query


    I am having trouble learning queries. The sub following runs, but it don't update the table. Please help.

    #Sub modQue_addNewLocation() ' update query Dim db As DAO.Database
    Set db = CurrentDb
    Dim dateDate As Date
    Dim strLineNo As String
    Dim strSQL As String

    dateDate = DateValue("9/27/2025")
    strLineNo = "104"
    strSQL = "UPDATE tblCalendar SET fLngProposalNo = 20250870 " _
    & "WHERE fDate = #" & dateDate & "# AND fStrLineNo = ' & strLineNo & ';"

    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
    End Sub#

    The fields in the table are fDate , fStrLineNo . and fLngProposalNo .
    I am trying to put the proposal number (20250870 ) in the table. Thank you.

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Office 365
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,739
    & "WHERE fDate = #" & dateDate & "# AND fStrLineNo = '" & strLineNo & "'"

  3. #3
    Gustav's Avatar
    Gustav is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2025
    Posts
    32
    You must format the date value to a string expression to be safe:
    Code:
    strSQL = "UPDATE tblCalendar SET fLngProposalNo = 20250870 " & _
    "WHERE fDate = #" & Format(dateDate, "yyyy\/mm\/dd") & "# AND fStrLineNo = '" & strLineNo & "';"

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?
    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format
    Numbers do not need anything
    Also for anything other than one item of criteria, I tend to put the the criteria into a string variable and then debug.print it, until correct then also use that in the code.
    Added benefit is, if you still cannot see the error, you can copy and paste back here the output from the debug.print for someone else to spot it.
    Example:
    tt="Eg'g"
    ? dlookup("FoodID","tblFoods","FoodName = """ & tt & """")

    Same applies to building query sql.

    As I kept forgetting how to protect a character I used something I found on the net.
    Code:
    'Public Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Needed for dates in queries as Access expects USA format.
    Public Const strcJetDate = "\#yyyy-mm-dd\#"  'Needed for dates in queries as Access expects USA but will accept ISO format.
    Then use that in the Format() function.
    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

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    Your sub is called addNewLocation but using an update query. Update queries don’t add a new record, they update an existing one.

    perhapd that is what you intend, but the comments are at odds with the name.

    to add a new location, you use an insert query



  6. #6
    Join Date
    Sep 2025
    Location
    Newport, Shropshire, UK
    Posts
    16
    Quote Originally Posted by Welshgasman View Post
    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?
    A literal double quote character is represented by a contiguous pair of double quote characters, which can be anywhere within a literal string expression delimited by double quote characters in the usual way, so they are not necessarily triple double quotes in the sense that there is a triplet of contiguous double quote characters. People often seem to have difficulties with this, though I'm not sure why. All that's necessary is to put two contiguous double quote characters "" wherever you would normally put a single quote character ' in the string. e.g.

    Code:
    MyString = "My name in its unanglicized form is ""Cináed O'Siridean"""
    which would evaluate to:

    My name in its unanglicized form is "Cináed O'Siridean"

  7. #7
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    That solved my problem. Thanks to everyone.

  8. #8
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    I marked this solved. However I am still having trouble with it. I had to add more criteria and I also using variables. These queries give me a hard time. The following is what I have:
    #
    Sub modQue_updateNewLocation() ' update tblCalendar query
    Dim db As DAO.Database
    Set db = CurrentDb
    Dim strSQL As String
    Dim strSvc As String
    Dim dateDate As Date
    Dim strLineNo As String
    Dim lngProposalNo As Long

    dateDate = DateValue("9/30/2025") ' Forms![frmCalendar].Form![hTbToDate]
    strLineNo = "103" ' Right(Forms![frmCalendar].Form![hTbToName], 3)
    strSvc = "stripe" ' Forms![frmCalendar].Form![hTbService]
    lngProposalNo = 20250537 ' Forms![frmCalendar].Form![hTbProposalNo]

    strSQL = "UPDATE tblCalendar SET fLngProposalNo = " & lngProposalNo & " AND " _
    & "fTxtService = '" & strSvc & "'" _
    & "WHERE fDate = #" & dateDate & "# AND fStrLineNo = '" & strLineNo & "'"


    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True

    End Sub
    #
    I also changed the query name. It is an update query. It runs but it don't update the table. Please help.

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Debug.print strSQL until you get it correct
    Only then attempt to use it.

    You have already been advised to use format() on the date.

    I tend to use Select first to make sure I have the correct records, then change to required query type.
    Last edited by Welshgasman; 10-02-2025 at 04:03 PM.
    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

  10. #10
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    I changed the line to:
    & "WHERE fDate = #" & Format(dateDate, "mm/dd/yyyy") & "# AND fStrLineNo = '" & strLineNo & "'"

    The query ran without error but did not update the table. Any ideas.

  11. #11
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    Have you checked that a record exists in the table that has the date and line number required. And that the date in the field does not have a time element, perhaps hidden with formatting

  12. #12
    Join Date
    Sep 2025
    Location
    Newport, Shropshire, UK
    Posts
    16
    Quote Originally Posted by lawdy View Post
    I changed the line to:
    & "WHERE fDate = #" & Format(dateDate, "mm/dd/yyyy") & "# AND fStrLineNo = '" & strLineNo & "'"

    The query ran without error but did not update the table. Any ideas.
    Debug.Print the SQL statement to the immediate window. Then copy and paste it into the query designer, and open the query as a datasheet. Does it return a row? If not, it points to the row you are trying to update not existing in the table.

  13. #13
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    There is for sure a record where the fDate is 9/30/2025 and fStrLineNo is 103. I have check over & over. I printed the sql with the same non results. I don't know what to do.

  14. #14
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,927
    Would help if you provided the full code since, based on the code you provided in post #8, you need a space before WHERE.

    And comment out setwarnings until you know the code is working as required

    Use debug.print strSQL to check you have the correct values

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Well use a select and get to see the record first. As mentioned if there is a time element in there (and you should know that), then it will not match. In that case use DateValue()
    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

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 01-24-2024, 06:10 PM
  2. I know what I need to do but don't know how to.
    By EVGData in forum Programming
    Replies: 2
    Last Post: 04-25-2016, 07:34 AM
  3. Replies: 6
    Last Post: 10-15-2014, 02:24 PM
  4. Replies: 10
    Last Post: 10-29-2013, 10:53 PM
  5. Replies: 12
    Last Post: 09-04-2013, 02:51 AM

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