Solved

Update Table with VBA in Access

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

747 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

10 Experts available now in Live!

Get 1:1 Help Now