Avatar of dennis_hs
dennis_hsFlag for Denmark

asked on 

Access, IF EXISTS, error 3129

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..
Microsoft AccessSQL

Avatar of undefined
Last Comment
dennis_hs
Avatar of Kelvin Sparks
Kelvin Sparks
Flag of New Zealand image

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
Avatar of Bhavesh Shah
Bhavesh Shah
Flag of India image

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,
ASKER CERTIFIED SOLUTION
Avatar of Kelvin Sparks
Kelvin Sparks
Flag of New Zealand image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Kelvin Sparks
Kelvin Sparks
Flag of New Zealand image

Note dates must be enclosed in # in Access. Also watch the format you pass dates into Access - it may well try and "Americanise" them
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
Avatar of dennis_hs
dennis_hs
Flag of Denmark image

ASKER

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

]
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo