Solved

Update Table with VBA in Access

Posted on 2014-09-17
4
346 Views
Last Modified: 2014-09-17
Syntax error in the following VBA Update Statement:

DoCmd.RunSQL "UPDATE tbl_BiWeek" _
& "SET [Paid] = " & "Yes" _
& "WHERE ([PayPeriodStart] = " & Me.lstPayPeriods.Column(2, intRow) & _
"AND [PayPeriodEnd] = " & Me.lstPayPeriods.Column(3, intRow) & ")"

Field [Paid] is a text short text field
Using Access 2013
0
Comment
Question by:hopelessinsalem
  • 2
4 Comments
 
LVL 36

Expert Comment

by:PatHartman
ID: 40328845
The easiest way to resolve errors of this nature is to build the SQL into a string.

Dim strSQL as String

strSQL =  "UPDATE tbl_BiWeek" _
 & "SET [Paid] = " & "Yes" _
 & "WHERE ([PayPeriodStart] = " & Me.lstPayPeriods.Column(2, intRow) & _
 "AND [PayPeriodEnd] = " & Me.lstPayPeriods.Column(3, intRow) & ")"
DoCmd.RunSQL strSQL

That way, if you have an error that isn't immediately obvious, you can print the string to the debug window and copy and paste it into the QBE where you will be able to "see" it and will probably get better error messages.

But, the problem is probably with the Paid field.   String values need to be enclosed in quotes.  So --
& "SET [Paid] = 'Yes'" _
might solve the problem
If PayEnd is a date, you have more syntax errors to come.
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40328852
Your list columns are considered text once they get into the list, so you must format those as dates if the [PayPeriodStart] and [PayPeriodEnd] fields are date fields.

Dim strSQL as string
With me.lstPayPeriods
     strSQL = "UPDATE tbl_BiWeek SET [Paid] = " & chr$(34) & "Yes" & chr$(34) _
                  & "WHERE ([PayPeriodStart] = #" & .Column(2, intRow) & "# " _
                  & "AND [PayPeriodEnd] = #" & .Column(3, intRow) & "#)"
end with
debug.print strSQL
currentdb.execute strsql, dbfailonerror
0
 

Author Closing Comment

by:hopelessinsalem
ID: 40328891
Thank you so much, both of you.   Second response worked well
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40328938
BTW, I use a function for wrapping text and dates.  The function looks like (from memory):
Public Function fnQuotes(QuoteWhat as Variant, _
                         Optional Delimit as String = """") as String

    IF ISNULL(QuoteWhat) THEN 
        fnQuotes = "NULL"
    ELSE
        fnQuotes = Delimit _
                 & Replace(QuoteWhat, Delimit, Delimit & Delimit) _
                 & Delimit
    End If

End Function

Open in new window

If you pass it a NULL, it will return "NULL", so that you can simply pass a variable and use this to insert NULL into a SQL string.  The Delimit feature allows you to determine what you want to wrap your text with.  So for text, I generally use the default, unless I'm formatting a string for a SQL Server pass-thru query, in which case I pass in the single quote.  For dates, you simply pass in the "#" value.  The advantage of this is that it also replaces instances of either the " or ' within the body of the text you are trying to wrap.  With this function, your SQL string would look like:
strSQL = "UPDATE tbl_BiWeek SET [Paid] = " & fnQuotes("Yes") _
       & " WHERE ([PayPeriodStart] = " & fnQuotes(.Column(2, intRow), "# ") _
       & " AND [PayPeriodEnd] = " & fnQuotes(.Column(3, intRow), "#") & ")"

Open in new window

It is probably just me, but I find that easier to read than the other techniques.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question