I am using VB6 and MSSQL - Adv.Beginner Level. From the msdn website I have copied the code for a function called ExecuteSQL (see below). I was using this function just fine for returning recordsets but now I want it to execute an INSERT query. I am confused as to how to call the function when it is not returning a recordset. I don't need it to return anything - just execute. The only way it seems to allow me to use it is by assigning it as a datasource for a recordset (MySource.datasource = ExecuteSQL(MySQL, MyMsg). While this gives me what I need (runs the query), I really don't need to create this recordset variable yet when I don't assign it I get the message that = is expected.
Can anyone clarify this for me.
---------------------------------------
Code from ---------------------------------------
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
'executes SQL and returns Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = rst.RecordCount & _
" records found from SQL"
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgBox "There are no records for the given Criteria"
MsgString = "ExecuteSQL Error: " & _
"There are no records"
Resume ExecuteSQL_Exit
cnn.Close
End Function
Can anyone clarify this for me.
---------------------------------------
Code from ---------------------------------------
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
'executes SQL and returns Recordset
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
Set cnn = New ADODB.Connection
cnn.Open ConnectString
If InStr("INSERT,DELETE,UPDATE", _
UCase$(sTokens(0))) Then
cnn.Execute SQL
MsgString = sTokens(0) & _
" query successful"
Else
Set rst = New ADODB.Recordset
rst.Open Trim$(SQL), cnn, _
adOpenKeyset, _
adLockOptimistic
rst.MoveLast 'get RecordCount
Set ExecuteSQL = rst
MsgString = rst.RecordCount & _
" records found from SQL"
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cnn = Nothing
Exit Function
ExecuteSQL_Error:
MsgBox "There are no records for the given Criteria"
MsgString = "ExecuteSQL Error: " & _
"There are no records"
Resume ExecuteSQL_Exit
cnn.Close
End Function