- Public Function ExecuteSQL (ByVal sql As String, msgstring as String) as ADODB. Recordset
- Dim CNN as ADODB. Connection ' defines the connection form of CNN for a database, and the function to connect and open the database cnnectstring with the cnnectstring () function is described below
- Dim rst as ADODB. Recordset ' defines a temporary list of data that is taken from the database
- Dim Stokens () as String defines an array that stores SQL statements as arrays
- On Error GoTo Executesql_error ' If an error occurs, the emergency handling
- Okens = Split (SQL) uses the Split function to deposit the SQL statement into an array by word, for example: if the SQL statement is a SELECT * from student, then the result of the run is--sto Kens (0) = "Select", stokents (1) = "*", stokens (2) = "from", Stokens (3) = "Student"
- Set cnn = New ADODB. Connection ' to instantiate CNN
- Cnn. Open connectstring ' connects the database via ConnectString and opens
- If InStr ("Insert,delet,update", ucase$ (Stokens (0))) Then ' determines whether the query operation is done through the SELECT statement
- Cnn. Execute Sql<span style= "White-space:pre" > </span> "performs the operation without returning the result of the operation.
- msgstring = Stokens (0) & "Query Successful"
- Else
- Set rst = New ADODB. Instantiation of the Recordset ' temp list
- Rst. Open trim$ (SQL), CNN, adOpenKeyset, adLockOptimistic ' rst opening,trim& () is the action string to be held; CNN is a string expression, the name of the ODBC data source that is logged in, open in a way 。 ; Dopenkeyset, determines the type of cursor (that is, how to find) to use keyset cursors. You cannot access records deleted by another user, but it is similar to dynamic cursors except that you cannot view records added by other users. Data changes made by other users are still visible; adLockOptimistic, when the data source is being updated, the system does not lock other users ' actions, and other users can add, delete, and change the data.
- Set ExecuteSQL = RST ' assigns the resulting temporary list to the ExecuteSQL function, especially it returns and assigns it further to the MRC
- msgstring = "Query to" & rst. RecordCount & "Records"
- End If
- Utesql_exit: ' An operation to empty the temporary list before the function exits.
- Set rst = Nothing
- Set cnn = Nothing
- Exit Function
- Utesql_error: ' Error ID, handling when error occurs
- msgstring = "Query Error:" & _
- Err.Description
- Resume Executesql_exit
- Function
The introduction of the ConnectString () function
The meaning of each code of the Executssql function