473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Error in SQL update code in VBA

When I enter a date in a field named 'Payment Date' in a form derived from a table named 'Bookings', I want that same date to be added to all other records that share the same Invoice Number 'Inv No'.

I have tried the following code but it seems to glitch at the end of the 'SET' line.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Payment_Date_BeforeUpdate(Cancel As Integer)
  2. Dim strSQL As String
  3. Dim SelectedDate As Date
  4. Dim SelectedInvoice As Long
  5.  
  6. SelectedDate = [Payment Date]
  7. SelectedInvoice = [Inv No]
  8. strSQL = "UPDATE [Bookings] " & _
  9.          "SET    [Payment Date] = " & SelectedDate & _
  10.          "WHERE ([Inv No] = SelectedInvoice)"
  11. Call CurrentDb.Execute(strSQL)
  12. End Sub
  13.  
Where have I gone wrong pse, and does anyone know a good link for learning VBA/SQL syntax?!
Oct 1 '10 #1

✓ answered by nico5038

Hmm, guess it's a typo, change SelectedInvoice) into SelectedInvoice

Nic;o)

9 1671
nico5038
3,080 Expert 2GB
A date needs to be surrounded by #'s signs so use:

Expand|Select|Wrap|Line Numbers
  1.  strSQL = "UPDATE [Bookings] " & _
  2.           "SET    [Payment Date] = #" & SelectedDate & _
  3.           "# WHERE ([Inv No] = SelectedInvoice)"
  4.  
Nic;o)
Oct 1 '10 #2
nico5038
3,080 Expert 2GB
Oops, the SelectedInvoice should be concatenated, so use:

Expand|Select|Wrap|Line Numbers
  1.  strSQL = "UPDATE [Bookings] " & _
  2.           "SET    [Payment Date] = #" & SelectedDate & _
  3.           "# WHERE ([Inv No] = " & SelectedInvoice)
  4.  
Nic;o)
Oct 1 '10 #3
colintis
255 100+
Just a bit of supplement based on nico5038's answer. To make sure the date is updated correctly into the table (e.g. The date 05/09/2010 5th Sept, 2010, may confused the database known as 09/05/2010 9th May, 2010), you need to clarify the format to suit the database's standard. Update the line for getting the selecteddate value
Expand|Select|Wrap|Line Numbers
  1. SelectedDate = format([Payment Date], "mm/dd/yyyy")
Oct 4 '10 #4
Apologies for delay in getting back.
I have tried the recommended code, ie:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Payment_Date_BeforeUpdate(Cancel As Integer)
  2. Dim strSQL As String
  3. Dim SelectedDate As Date
  4. Dim SelectedInvoice As Long
  5. SelectedDate = Format([Payment Date], "dd-mmm-yy")
  6. SelectedInvoice = [Inv No]
  7. strSQL = "UPDATE [Bookings] " & _
  8.          "SET    [Payment Date] = #" & SelectedDate & _
  9.          "# WHERE ([Inv No] = " &  SelectedInvoice)
  10. Call CurrentDb.Execute(strSQL)
  11. End Sub
  12.  
and I get 'Compile error: Expected: End of Statement' with the cursor resting on the final bracket of the 'Where' line.
Any further suggestions please?
Oct 14 '10 #5
nico5038
3,080 Expert 2GB
Hmm, guess it's a typo, change SelectedInvoice) into SelectedInvoice

Nic;o)
Oct 14 '10 #6
That did it, although I had to remove the leading bracket too. Thanks for help. Can you recommend a good place to find an explanation of the use of punctuation in SQL/VBA, concatenation etc please?

Nick
Oct 14 '10 #7
nico5038
3,080 Expert 2GB
Normally I use the Access Help (when VBA is active) to check for the syntax of functions and statements.

Success with your application !

Nic;o)
Oct 14 '10 #8
NeoPa
32,556 Expert Mod 16PB
A couple of points :
  1. Colintis point of formatting the literal date is valid, only it doesn't depend on the database. It always requires the SQL standard format which is m/d/yyyy (See Literal DateTimes and Their Delimiters (#)).
  2. The end of the line, to match the opening parenthesis of the original version, should end : SelectedInvoice & ")"

    Losing the opening parenthesis is a workable alternative of course.
Oct 14 '10 #9
NeoPa
32,556 Expert Mod 16PB
Nico:
Normally I use the Access Help (when VBA is active) to check for the syntax of functions and statements.
Access Help also provides assistance for the Jet-SQL side of things. See Finding Jet SQL Help.
Oct 14 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: paul | last post by:
Hi everyone, I'm having problems displaying a date in the correct format and I was wondering if anyone knew how to help. I'm using pear to extract a date from our back end database (the date is...
0
by: George | last post by:
I have two tables and want to find the Maximum date for a given GIN. I have been able to produce the result in Sybase but I am having problems in Oracle. Example of my query in Sybase. create...
8
by: Michael C | last post by:
Hi all, I'm still having problems with VS.NET 2003 running on my XP machine. I finally decided I might as well uninstall completely and reinstall from scratch. But lo and behold it won't let...
4
by: Charles | last post by:
I am still having problems with sessions crossing over from other sessions. What I mean by this is that Mary may get just some or all of Renaeā€™s information. I think that what is going on with...
11
by: Rick Mogstad | last post by:
I know this is probably not the group for this, but i know that these groups are fairly active, and that the people are usually willing to help. Is anyone else experiencing this? The news...
7
by: Ron | last post by:
Hi All, It's taking a long, LONG time for me to see a post from here (and other NGs too, for that matter--which is why I think it may be me...). Sometimes it takes 2-3 minutes after clicking...
2
by: l46kok | last post by:
Hello. This is the first time I'm encountering vectors and I'm having problems. I want to create a vector filled with objects, but as soon as I try to invoke a function within the object, ...
1
by: tshad | last post by:
Some of my regular expressions don't seem to work correctly in IE7. I am on asp.net 1.1. I have a site that has been up for a while and I started having problems on machines with IE7. IE5 and...
3
by: i80and | last post by:
I'm working on a basic web spider, and I'm having problems with the urlparser. This is the effected function: ------------------------------ def FindLinks(Website): WebsiteLen = len(Website)+1...
3
by: Blondie1966 | last post by:
Ok Im still having problems, Im wanting the name that I enter as the truck name to always be the same, so if the user is asked to enter the truck name it must be one thing, and if anything other it...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.