Solved

Access, IF EXISTS, error 3129

Posted on 2010-11-15
7
969 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 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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 70

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 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