I am new to using SQL server express for upsizing an Access (2007-2010) front end.
I wrote a class module that manages the SQL connection. for automating the call of functions, called procedures, and dynamic SQL strings, the last two return successfully a table using a SELECT statement that I attach to a DAO.recordset for further extracting results.
However when trying to do the same for an insert statement I encounter a problem The SQL passthrough query is:
DECLARE @ret int;
BEGIN TRANSACTION T1;
BEGIN TRY
INSERT INTO dbo.[Munten](Munt, Decimalen, Volgorde) VALUES ('XYZ', 4, 99)
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION T1;
END CATCH;
IF @@TRANCOUNT = 0
SET @ret = 0
ELSE
BEGIN
COMMIT TRANSACTION T1;
SET @ret = 1
END;
SELECT @ret as Retval;
that runs fine on SQL management studio returning a one column record but when executing the poassthrough query I get:
"Passtrhough with returnrecords property set to true did not return any record"
Bellow the Access code:
Public Function AddRecord(TblName As String, Connectstring As String, ParamArray Pars()) As Boolean
Dim s As String, I As Integer, str As String, sc As String, sv As String, rs As DAO.Recordset
ProcList.Start procmodule, "AddRecord", TblName, Connectstring, Pars()
On Local Error GoTo ErrHandler
With appdb.CreateQueryDef("")
If Len(Connectstring) = 0 Then
.Connect = StConnectstr
Else
.Connect = Connectstring
End If
For I = 0 To UBound(Pars) ' ubound=-1 if no pars are passed
If I Mod 2 = 0 Then ' list of columns
If I > 0 Then sc = sc & ", "
sc = sc & Pars(I)
Else ' list of values
If I > 1 Then sv = sv & ", "
If IsDate(Pars(I)) Then
sv = sv & DateForSQL(Pars(I))
ElseIf IsNumeric(Pars(I)) Then
sv = sv & ConvDecimal(Pars(I), True)
Else
sv = sv & cSquote & Pars(I) & cSquote
End If
End If
Next
s = "INSERT INTO " & Me.WithSchema(TblName, IsTable) & "(" & sc & ") VALUES (" & sv & ")"
str = EmbedTryCatch(s, .Connect)
.ReturnsRecords = True
.SQL = str
AddRecord = (.OpenRecordset(, dbSQLPassThrough).Fields(0) <> 0)
.Close
End With
Exit_Here:
ProcList.Leave
Exit Function
ErrHandler:
If Error_message() Then Resume 0
Resume Exit_Here
End Function
Private Function EmbedTryCatch(SQLstring As String, Connectstr As String) As String
EmbedTryCatch = "DECLARE @ret BIT;" & vbCrLf _
& "BEGIN TRANSACTION T1;" & vbCrLf _
& "BEGIN TRY" & vbCrLf _
& SQLstring & vbCrLf _
& "END TRY" & vbCrLf _
& "BEGIN CATCH" & vbCrLf _
& "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION T1;" & vbCrLf _
& "END CATCH;" & vbCrLf _
& "IF @@TRANCOUNT = 0" & vbCrLf _
& "SET @ret = 0" & vbCrLf _
& "ELSE" & vbCrLf _
& "BEGIN" & vbCrLf _
& "COMMIT TRANSACTION T1;" & vbCrLf _
& "SET @ret = 1" & vbCrLf _
& "END;" & vbCrLf _
& "SELECT @ret as Retval;"
End Function
Where is the catch ?