473,765 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL using IF THEN ELSE

Hi,

Using MS Access 2000, is it possible to run a UPDATE or INSERT SQL query
using some form
of conditional IF THEN ??

for example:

SELECT * FROM Books
IF EXISTS(Select Books.ID = 1)
THEN (UPDATE Books.Stock VALUES(10))
ELSE (INSERT Books.ID Books.Stock VALUES(1,10)

I've googled for hours and found no soultions.
TIA.
Nov 13 '05 #1
8 34551

"Carl" <mail2carl@_rem ove_yahoo.com> schreef in bericht news:tT******** *********@newsf e6-gui.ntli.net...
Hi,

Using MS Access 2000, is it possible to run a UPDATE or INSERT SQL query
using some form
of conditional IF THEN ??

for example:

SELECT * FROM Books
IF EXISTS(Select Books.ID = 1)
THEN (UPDATE Books.Stock VALUES(10))
ELSE (INSERT Books.ID Books.Stock VALUES(1,10)

I've googled for hours and found no soultions.
TIA.


You need syntax like:
If SomethingIsTrue Then
RunUpdateQuery
ELSE
RunInsertQuery
End if

Or in (air)code:
Dim db as DAO.Database
Dim rst as DAO.recordset
Dim strSQL as string
strSQL="SELECT * FROM Books WHERE ID = 1
Set db=Currentdb
set rst=db.OpenReco rdset(strSQL)
if not rst.EOF Then 'Record exists
rst.edit
.... 'Do your thing here
rst.Update
else 'Create new record
rst.AddNew
rst!BookID = 1
.... 'Do your thing here
rst.Update
end if
rst.close
set rst=Nothing

Arno R

Nov 13 '05 #2
"Carl" <mail2carl@_rem ove_yahoo.com> wrote in
news:tT******** *********@newsf e6-gui.ntli.net:
Using MS Access 2000, is it possible to run a UPDATE or INSERT SQL
query using some form
of conditional IF THEN ??

for example:

SELECT * FROM Books
IF EXISTS(Select Books.ID = 1)
THEN (UPDATE Books.Stock VALUES(10))
ELSE (INSERT Books.ID Books.Stock VALUES(1,10)

I've googled for hours and found no soultions.


Jet SQL does not support multiple SQL statements, or conditional
code of this nature (of course, you can use IIf() for items in a
SELECT clause, though).

In Access, you can only do this with VBA code, as Arno has
suggested.

However, assuming you're updating one table from another one, you
could also do it with two queries that you run in succession that
would not overlap if you use the proper joins. For the UPDATE, you'd
use an inner join between the table1 and table2, which limits the
records updated to those that exist in both tables. For the insert,
you'd want an outer join where you returned the records in your
update table where the ID is Null in the target table. This would
mean that only the records that don't already exist would get
inserted.

Then you could execute both queries in succession and get exactly
the same result as you would with your single query with a
conditional structure. It would also probably be faster, since your
example has to re-execute for each row, whereas the queries I'm
suggesting would operate on sets.

Of course, if your input criteria are not coming from a table, then,
yes, you have to do it in code.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #3
Thanx Arno,David

So VBA procedure is best option in ms access 2000.

I will be executing the query from a perl script using the DBI::ODBC module.
However, I have yet to find a way of running stored procedures or VBA code
from the perl script.
"David W. Fenton" <dX********@bwa y.net.invalid> wrote in message
news:Xn******** *************** ***********@24. 168.128.90...
"Carl" <mail2carl@_rem ove_yahoo.com> wrote in
news:tT******** *********@newsf e6-gui.ntli.net:
Using MS Access 2000, is it possible to run a UPDATE or INSERT SQL
query using some form
of conditional IF THEN ??

for example:

SELECT * FROM Books
IF EXISTS(Select Books.ID = 1)
THEN (UPDATE Books.Stock VALUES(10))
ELSE (INSERT Books.ID Books.Stock VALUES(1,10)

I've googled for hours and found no soultions.


Jet SQL does not support multiple SQL statements, or conditional
code of this nature (of course, you can use IIf() for items in a
SELECT clause, though).

In Access, you can only do this with VBA code, as Arno has
suggested.

However, assuming you're updating one table from another one, you
could also do it with two queries that you run in succession that
would not overlap if you use the proper joins. For the UPDATE, you'd
use an inner join between the table1 and table2, which limits the
records updated to those that exist in both tables. For the insert,
you'd want an outer join where you returned the records in your
update table where the ID is Null in the target table. This would
mean that only the records that don't already exist would get
inserted.

Then you could execute both queries in succession and get exactly
the same result as you would with your single query with a
conditional structure. It would also probably be faster, since your
example has to re-execute for each row, whereas the queries I'm
suggesting would operate on sets.

Of course, if your input criteria are not coming from a table, then,
yes, you have to do it in code.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

Nov 13 '05 #4
"Carl" <mail2carl@_rem ove_yahoo.com> wrote in
news:LC******** *********@newsf e6-gui.ntli.net:
So VBA procedure is best option in ms access 2000.

I will be executing the query from a perl script using the
DBI::ODBC module. However, I have yet to find a way of running
stored procedures or VBA code from the perl script.


Well, you can't do that.

If you can use COM components from perl, then you could automate
Access and do it, but I don't think that makes any sense at all.

I was assuming your Access users were going to do the importing.

I have no suggestions for how you'd do this in perl or via ODBC. I
would think the code structure would be pretty much identical, but
the details would be different.

I don't understand why you'd *want* to do it from perl. Why
manipulate Jet data with non-native tools?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #5
The Access database will be stored on a webserver and the perl script
will be executing all the database queries and returning the results to the
clients web browser.

As I have limited access to the Webserver and no SQL server, Perl and
ODBC are the only soultions at the moment. I could use Perl OLE and ADODB,
but this would only support Windows servers.

The Perl DBI driver is excellent for administering Access databases on
both Unix and Windows webservers.

"David W. Fenton" <dX********@bwa y.net.invalid> wrote in message
news:Xn******** *************** ***********@24. 168.128.90...
"Carl" <mail2carl@_rem ove_yahoo.com> wrote in
news:LC******** *********@newsf e6-gui.ntli.net:
So VBA procedure is best option in ms access 2000.

I will be executing the query from a perl script using the
DBI::ODBC module. However, I have yet to find a way of running
stored procedures or VBA code from the perl script.


Well, you can't do that.

If you can use COM components from perl, then you could automate
Access and do it, but I don't think that makes any sense at all.

I was assuming your Access users were going to do the importing.

I have no suggestions for how you'd do this in perl or via ODBC. I
would think the code structure would be pretty much identical, but
the details would be different.

I don't understand why you'd *want* to do it from perl. Why
manipulate Jet data with non-native tools?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

Nov 13 '05 #6
"Carl" <mail2carl@_rem ove_yahoo.com> wrote in
news:ss******** *******@newsfe4-gui.ntli.net:
The Access database will be stored on a webserver and the perl
script will be executing all the database queries and returning
the results to the clients web browser.

As I have limited access to the Webserver and no SQL server, Perl
and ODBC are the only soultions at the moment. I could use Perl
OLE and ADODB, but this would only support Windows servers.

The Perl DBI driver is excellent for administering Access
databases on both Unix and Windows webservers.


You're *not* administering an Access database -- you're
administering a *Jet* database.

Access is separate from the db engine, and has its own set of
objects that are stored in Jet tables within an MDB file. You may
have used Access to create the Jet database, but if you're using it
only for data storage, you just aren't using Access in any
significant degree.

And on your web server, only Jet is involved.

Now, you don't say how the data is getting to the end users. If
you're sending them the MDB file, you could put VBA code in it along
with a UI and you could have them perform the operations I outlined
by opening the db in Access (and it's meaningfully an Access db if
you're putting code and forms in it).

But, really, all that I outlined was the VBA method for executing
SQL against the data tables. You should be able to do this via ODBC,
using perl's functions to do the comparisons necessary to determine
if a field needs to be updated.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #7
True, I think I will now decide to use Perl functions to do the IF THEN ELSE
code
part of the query and then just execute the required UPDATE or INSERT query.
I'm not going to get involved with the VBA side of it...I dont know VBA all
that well
yet.

The MDB database is permanently stored on the webserver, users will not have
access
to the MDB file itself...only me. The web users (clients) will be accessing
the database
through web pages...simple searches and updates etc....just think of it as
an online
shopping database with Perl doing all the processing.

Thnx for your advice.

"David W. Fenton" <dX********@bwa y.net.invalid> wrote in message
news:Xn******** *************** ***********@24. 168.128.90...
"Carl" <mail2carl@_rem ove_yahoo.com> wrote in
news:ss******** *******@newsfe4-gui.ntli.net:
The Access database will be stored on a webserver and the perl
script will be executing all the database queries and returning
the results to the clients web browser.

As I have limited access to the Webserver and no SQL server, Perl
and ODBC are the only soultions at the moment. I could use Perl
OLE and ADODB, but this would only support Windows servers.

The Perl DBI driver is excellent for administering Access
databases on both Unix and Windows webservers.


You're *not* administering an Access database -- you're
administering a *Jet* database.

Access is separate from the db engine, and has its own set of
objects that are stored in Jet tables within an MDB file. You may
have used Access to create the Jet database, but if you're using it
only for data storage, you just aren't using Access in any
significant degree.

And on your web server, only Jet is involved.

Now, you don't say how the data is getting to the end users. If
you're sending them the MDB file, you could put VBA code in it along
with a UI and you could have them perform the operations I outlined
by opening the db in Access (and it's meaningfully an Access db if
you're putting code and forms in it).

But, really, all that I outlined was the VBA method for executing
SQL against the data tables. You should be able to do this via ODBC,
using perl's functions to do the comparisons necessary to determine
if a field needs to be updated.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

Nov 13 '05 #8
"Carl" <mail2carl@_rem ove_yahoo.com> wrote in
news:JO******** *********@newsf e6-gui.ntli.net:
The MDB database is permanently stored on the webserver, users
will not have access
to the MDB file itself...only me. The web users (clients) will be
accessing the database
through web pages...simple searches and updates etc....just think
of it as an online
shopping database with Perl doing all the processing.


Well, I obviously misunderstoond your entire scenario!

I don't even understand the need to do what I suggested now -- it
was entirely based on the idea of getting website updates out to
users who had Access only, which is why I've programmed this kind of
thing so many times in the past.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
19357
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is able to fill that one out just fine. The second form is multipart/form-data. Unfortunately, I haven't been able to fill that out in a way that makes the server happy. I set up a copy of this form at my web site so that I could see exactly what a...
3
24035
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked tables in the database where the code resides. If we move the database with the data tables to a new directory, the links are no longer valid. I tried to update the links by changing the Connect property and refreshing: Set td = db.TableDefs(0)...
5
7386
by: Colin Anderson | last post by:
I discovered, with great excitement, this article http://www.davison.uk.net/vb2notes.asp when researching methods for emailing from Access via Notes. Unfortunatly, when I run this I get a Run-time error. When I run it on an XP machine it crashes, but on an NT box it just generates an unknown error, handled by the error handler. I have debugged and stepped through the code and have narrowed the issue to the point at which the...
11
6600
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
1
4010
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the live databases on the network. Is there a way, via code, when I get back in-house from being on the road to click a button, and select the backends I want to link to? I would want to delete all the current links and link to the "live"
11
3162
by: youngster94 | last post by:
Hey all, I've written a VB.Net app that creates picture badges complete with barcodes. The problem is that the barcode quality is not good enough to be read by scanners. I'm using the DRAWSTRING function to place the barcode on the image, but no matter what graphics settings (.InterpolationMode/.CompositingQuality etc.) I manipulate, the barcode image remains poor quality. For exaple I'm using a solidbrush that is black, but some of...
6
17205
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically, process and output to the screen in my application when a message arrived. But the problem is how do I read the SMS message immediately when it arrived without my handphone BeEPINg for new message ? I read up the AT commands, but when getting down...
4
8180
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
3
6808
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer> </Root> I would like to retreive "\Root\Customer\Name" out of it. Something like:
3
2097
by: anthonybrough | last post by:
I have an asp page that has a form to collect user data in a form. when the user clicks submit the input is validated. If any fields are not acceptable the user clicks on a button to go back to the original form to correct the input. This all works fine until I try to incorporate a javascript to display a popup calendar which posts the selected date back to a field on the form. This script works fine in itself, however if the page is...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.