Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

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..
0
dennis_hs
Asked:
dennis_hs
1 Solution
 
Kelvin SparksCommented:
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
 
Bhavesh ShahLead AnalysistCommented:
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
 
Kelvin SparksCommented:
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
Kelvin SparksCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
dennis_hsAuthor Commented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now