Solved

Update Table with VBA in Access

Posted on 2014-09-17
4
335 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 34

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now