473,386 Members | 1,720 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,386 software developers and data experts.

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 34504

"Carl" <mail2carl@_remove_yahoo.com> schreef in bericht news:tT*****************@newsfe6-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.OpenRecordset(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@_remove_yahoo.com> wrote in
news:tT*****************@newsfe6-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********@bway.net.invalid> wrote in message
news:Xn**********************************@24.168.1 28.90...
"Carl" <mail2carl@_remove_yahoo.com> wrote in
news:tT*****************@newsfe6-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@_remove_yahoo.com> wrote in
news:LC*****************@newsfe6-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********@bway.net.invalid> wrote in message
news:Xn**********************************@24.168.1 28.90...
"Carl" <mail2carl@_remove_yahoo.com> wrote in
news:LC*****************@newsfe6-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@_remove_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********@bway.net.invalid> wrote in message
news:Xn**********************************@24.168.1 28.90...
"Carl" <mail2carl@_remove_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@_remove_yahoo.com> wrote in
news:JO*****************@newsfe6-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
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...
3
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...
5
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...
11
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...
1
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...
11
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...
6
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,...
4
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
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>...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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...
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...
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
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,...
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...

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.