Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access, IF EXISTS, error 3129

Posted on 2010-11-15
7
Medium Priority
?
978 Views
Last Modified: 2012-05-10
Hello.

Have some trouble with an SQL line.

If a user exists it should update PhoneEndDate, else insert a new record.

This works fine in SQL Analyzer:

IF EXISTS (SELECT * FROM tblTable1 WHERE UserID = 555555)
UPDATE tblTable1 SET PhoneEndDate = '13-11-2010' WHERE UserID = 555555
ELSE INSERT INTO tblTable1 (UserID , PhoneType, PhoneStartDate) VALUES (555555,1,'14-11-2010')

But when I put this en Access, I get runtime-error 3129:

SQLSelectUser = "IF EXISTS (SELECT * FROM tblTable1 WHERE UserID = " & sUserId & ") UPDATE tblTable1 SET PhoneEndDate = '" & sUserDate & "') WHERE UserID = " & sUserId & " ELSE INSERT INTO tblTable1 (UserID, PhoneType, PhoneStartDate) VALUES (" & sUserId & "," & sPhoneType & ",'" & sPhoneStartDate & "')"

Any idea why?



Thanks..
0
Comment
Question by:dennis_hs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 34134514
Are you running this in Access as VBA (in which case it will fail) or passing it to SQL Server (in which case you must uuse SQL Server syntax?

Kelvin
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 34134601
I dont think,you can use if exists.
ALTERNATE WAY IS RUN BOTH QUERY AS SHOWN BELOW.

UPDATE tblTable1 SET PhoneEndDate = '" & sUserDate & "') WHERE UserID = " & sUserId & "

INSERT INTO tblTable1 (UserID, PhoneType, PhoneStartDate)
SELECT " & sUserId & "," & sPhoneType & ",'" & sPhoneStartDate & "'
FROM tblTable1
WHERE UserID <> " & sUserId & "


IF THERE IS WAY THEN YOU WILL SURELY GET ANSWER,
0
 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 500 total points
ID: 34134799
The Access VBA ay would be:

IF DCount( "*", "tblTable1", "[UserID] = " & sUserId) = 0 Then
  SQLSelectUser = "INSERT INTO tblTable1 (UserID, PhoneType, PhoneStartDate) VALUES (" & sUserId & "," & sPhoneType & ",#" & sPhoneStartDate & "#)"
ELSE
   SQLSelectUser = "UPDATE tblTable1 SET PhoneEndDate = #" & sUserDate & "#) WHERE UserID = " & sUserId
END IF
Docmd.RUNSQL SQLSelectUser

Kelvin
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 34134804
Note dates must be enclosed in # in Access. Also watch the format you pass dates into Access - it may well try and "Americanise" them
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 34387629
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0
 
LVL 1

Author Comment

by:dennis_hs
ID: 34407798
Sorry for the long wait, but I've been away from work.

Thanks kelvinsparks.

A couple of issues with null though. But this is how it works now:
If DCount("*", "tblTable1", "[UserID]= '" & sUserId & "'") = 0 Then
    SQLSelectUser = "INSERT INTO tblTable1 (UserID, PhoneType, PhoneStartDate) VALUES (" & sUserId & "," & sPhoneType & ",'" & sPhoneStartDate & "')"
Else
    SQLSelectUser = "UPDATE tblTable1 SET PhoneEndDate='" & sUserDate & "' WHERE UserID= '" & sUserId & "'"
End If

DoCmd.RunSQL SQLSelectUser

Open in new window

]
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

688 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