
Remember to set your object references to NOTHING after you have finished writing the code to control them with. Excel Error Load FormĪn automation error could occur within Excel if you load a form while another object (like the worksheet or range object) are still referenced. Make sure any object variables that you are referring to in your code are still valid when you call the property and methods that you are controlling them with. NET.The Object Has Disconnected from Its ClientĪn automation error could occur when you are referring to a workbook or worksheet via a variable, but the variable is no longer active. NET Database Programming for information on database programming in VB. Note that this example requires references to the ADO and ADOX object libraries. "*** Error executing SELECT statement ***" & vbCrLf _ Set rs = m_DBConnection.Execute(cmd,, adCmdText) Private Function ExecuteQuery(ByVal cmd As String) As String ' Execute a query command and return ' the results or failure string.

"*** Error executing command ***" & vbCrLf & _ Private Function ExecuteNonQuery(ByVal cmd As String) As _ ' Execute a non-query command and return ' a success or failure string. If InStr(UCase$(commands(i)), "SELECT") Then ' Break the script into semi-colon ' delimited commands.įor i = LBound(commands) To UBound(commands) This tool can correctly connect to SQL Server or MSDE databases, aligns query results in nice columns, etc.

NET Database Programming shows how to build a more powerful tool using VB. For each record, the program displays the record's field values. It then loops through the Recordset's records. The routine loops through the Recordset's columns listing them. Subroutine ExecuteQuery calls the Connection object's Execute method to run a SELECT statement. Subroutine ExecuteNonQuery simply calls the Connection object's Execute method to run a non-SELECT statement. If the command does not contain the word SELECT, the program calls subroutine ExecuteNonQuery to execute it without retrieving any results.

If the command contains the word SELECT, the program calls subroutine ExecuteQuery to execute it and retrieve the results. If the command is not blank, the code looks for the string SELECT in the command. For each command, the program removes carriage returns, line feeds, and spaces to see if the command is blank. The cmdExecute_Click event handler splits a SQL script into its semi-colon delimited commands. VB Helper: HowTo: Execute ad hoc SQL scripts with ADOīuild database, DB, ADO, SQL, ad hoc, query, script, query
