Solved

Access, IF EXISTS, error 3129

Posted on 2010-11-15
7
956 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
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 125 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 68

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now