473,473 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Update Query (Running in VBA)

153 New Member
Good morning fellow programmers.

Just a quick question I hope...

I'm trying to run some SQL from VBA and I'm getting an error....I think it's Syntax?

This is the code:

[PHP] Dim stSQL2

stSQL2 = "UPDATE [AddedToOutlook] INNER JOIN [tblContracts] ON [AddedToOutlook].DatabaseReferenceNumber = [tblContracts].DatabaseReferenceNumber" & _
"Set [AddedToOutlook].AddedToOutlook = False" & _
"Where [tblContracts].RenewalDate < current_date"

DoCmd.RunSQL stSQL2[/PHP]

I'm basically trying to use SQL to sort of set a primary key (the numbers actually directly correspond, but due to some things I'm doing with the VBA code I can't set a primary key)...anyways based on that "primary key" wherever the field Renewal date is less than the current date, I want to set the boolean field "AddedToOutlook" in the table AddedToOutlook (I know I should have called it tblAddedToOutlook) equal to False because I then have the program change the date and it's now recognized as a new entry (although the database reference number remains the same).
Jan 9 '07 #1
5 4207
PEB
1,418 Recognized Expert Top Contributor
I think no space btwn DatabaseReferenceNumberSet [AddedToOutlook].AddedToOutlook = FalseWhere [tblContracts].RenewalDate < current_date"

and missing ";"

The Inner joins aren't my favorite thing in Updates...

But this is only at first view!
Jan 9 '07 #2
Kosmos
153 New Member
hhmm well perhaps...is there a way that I can take something out of the VBA like for example...I'll run a loop and find out, in vba, where renewaldate < date and each time that is true (in an if statement nested in an until .EOF loop), then it will run SQL based on that coordinate? like where renewaldate<date then update At [AddedToOutlook].(recSet1.Fields("DatabaseReferenceNumber"))??

I know you can do something like Forms!UserForm!txtFilterMainName and take an actual value from a form...so I would assume you can carry over a value from your VBA code somehow, as well?
Jan 9 '07 #3
Kosmos
153 New Member
Actually...since the program adds new entries to AddedToOutlook, based on the above code...if I could do something like that...I guess it would be easier to do something along the lines of Delete FieldAddedToOutlook From [AddedToOutlook] Where [AddedToOutlook].(recSet1.Fields("DatabaseReferenceNumber"))?
Jan 9 '07 #4
NeoPa
32,556 Recognized Expert Moderator MVP
Good morning fellow programmers.

Just a quick question I hope...

I'm trying to run some SQL from VBA and I'm getting an error....I think it's Syntax?

This is the code:

[PHP] Dim stSQL2

stSQL2 = "UPDATE [AddedToOutlook] INNER JOIN [tblContracts] ON [AddedToOutlook].DatabaseReferenceNumber = [tblContracts].DatabaseReferenceNumber" & _
"Set [AddedToOutlook].AddedToOutlook = False" & _
"Where [tblContracts].RenewalDate < current_date"

DoCmd.RunSQL stSQL2[/PHP]

I'm basically trying to use SQL to sort of set a primary key (the numbers actually directly correspond, but due to some things I'm doing with the VBA code I can't set a primary key)...anyways based on that "primary key" wherever the field Renewal date is less than the current date, I want to set the boolean field "AddedToOutlook" in the table AddedToOutlook (I know I should have called it tblAddedToOutlook) equal to False because I then have the program change the date and it's now recognized as a new entry (although the database reference number remains the same).
The ( ; ) is not necessary but the spaces before Set & Where are crucial (normally placed at the end of the preceeding lines).
Expand|Select|Wrap|Line Numbers
  1.     stSQL2 = "UPDATE [AddedToOutlook] INNER JOIN [tblContracts] ON [AddedToOutlook].DatabaseReferenceNumber = [tblContracts].DatabaseReferenceNumber " & _
  2.              "Set [AddedToOutlook].AddedToOutlook = False " & _
  3.              "Where [tblContracts].RenewalDate < Date()"
Jan 9 '07 #5
Kosmos
153 New Member
Ahh okay. Thanks.

I actually made up a way to do this in perhaps a very strange way lol.

I knew you could take fields form the form into a query but I didn't know if you could take anything from the VBA code behind the form.

So I created a text box that wasn't visible called txtReference and used that to do the following query:

DELETE *
FROM AddedToOutlook
WHERE AddedToOutlook.DatabaseReferenceNumber=Forms!UserF orm!txtReference;


Perhaps a bit messy...but at least when I leave they'll still need me for consulting :) lol
Jan 9 '07 #6

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

Similar topics

9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
5
by: Don Seckler | last post by:
I have an update query that runs when a report closes. I have several reports that will need to run the update query with diferent criteria. I'd like to simply make the criteria change in the...
6
by: Nicolae Fieraru | last post by:
Hi All, I was trying to update a field in a table, based on the results from a query. The table to be updated is tblCustomers and the query is qrySelect. This query has two parameters, provided...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
1
by: Aaron | last post by:
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; ...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
8
by: kepston | last post by:
I have a situation where I need to record scrap quantities for parts that we sell and parts that are supplied, in a single table. I have a UNION query that combines the part numbers successfully,...
4
by: m.wanstall | last post by:
I have a crosstab query that compiles data for Months of the year. I have a stacked select query on top of that crosstab query that uses the latest 2 months data and exports it to a fixed length...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.