Notes for All Methods

Total Page:16

File Type:pdf, Size:1020Kb

Notes for All Methods

Notes for all methods

The following are equivalent in most cases: strSQL = "UPDATE tblEmployees SET tblEmployees.City = ""Bellevue""" strSQL = "UPDATE tblEmployees SET tblEmployees.City = 'Bellevue'"

 Exaggerated for clarity: = ' Bellevue ' "

Use Chr(34) instead, if there is any chance that a text value being updated includes an apostrophe. For example:

Dim strLastName As String strLastName = "O'Malley" '<---An apostrophe is a reserved character in SQL statements. strSQL = "UPDATE Employees SET Employees.LastName = " _ & Chr(34) & strLastName & Chr(34) & " WHERE EmployeeID = 1"

Additional Information: Action queries: suppressing dialogs, while knowing results http://allenbrowne.com/ser-60.html

Error message: Too few parameters, expected n http://www.mvps.org/access/queries/qry0013.htm

Option Compare Database Option Explicit

Sub Test1() ' Will produce a warning if the user's setting, under Tools > Options ' on the Edit/Find tab includes confirming Action Queries

DoCmd.RunSQL "UPDATE tblEmployees SET tblEmployees.City = ""Renton"""

End Sub

Sub Test2() ' Includes turning off warnings and then immediately turning warnings ' back on. Note: Warnings will not be turned on if the DoCmd.RunSQL ' statement fails for any reason, such as a table being renamed!

DoCmd.SetWarnings False DoCmd.RunSQL "UPDATE tblEmployees SET tblEmployees.City = ""Seattle""" DoCmd.SetWarnings True

End Sub

Sub Test3() On Error GoTo ProcError

' Same as above, but warnings will always be turned back on in the error ' handling code. This is an example of how error handling procedures can ' help prevent problems.

DoCmd.SetWarnings False DoCmd.RunSQL "UPDATE tblEmployees SET tblEmployees.City = ""Tukwila""" DoCmd.SetWarnings True

' Additional code can be inserted here after re-enabling warnings

ExitProc: DoCmd.SetWarnings True Exit Sub ProcError: MsgBox "Error: " & Err.Number & ". " & Err.Description, _ vbInformation, "Error in Test3 Procedure..." Resume ExitProc End Sub

Sub Test4() On Error GoTo ProcError

' This method uses DAO. It requires a reference set to the ' Microsoft DAO 3.x Object Library (Access 97 --> 3.51, Access 2000+ --> 3.6).

Dim db As DAO.Database Dim strSQL As String

Set db = CurrentDb() strSQL = "UPDATE tblEmployees SET tblEmployees.City = ""Lynnwood"""

db.Execute strSQL, dbFailOnError MsgBox db.RecordsAffected & " records were updated.", vbInformation, _ "Your message box title goes here"

ExitProc: ' Cleanup set db=Nothing Exit Sub ProcError: MsgBox "Error: " & Err.Number & ". " & Err.Description, _ vbInformation, "Error in Test4 Procedure..." Resume ExitProc End Sub

Special notes for Sub Test4() dbFailOnError is an optional parameter but, if specified, requires that a reference be set to the DAO Object Library.

You can use CurrentDB.execute, instead of declaring and setting the db object variable. This allows you to get away without a reference set to the DAO Object Library, as long as you are not using the optional dbFailOnError parameter. In addition, you cannot display the number of records affected, since CurrentDB will lose its scope.

Sub Test4b() On Error GoTo ProcError

Dim strSQL As String strSQL = "UPDATE tblEmployees SET tblEmployees.City = ""Lynnwood"""

CurrentDB.Execute strSQL

ExitProc: Exit Sub ProcError: MsgBox "Error: " & Err.Number & ". " & Err.Description, _ vbInformation, "Error in Test4b Procedure..." Resume ExitProc End Sub

Sub Test5() ' This method uses ADO. It requires a reference set to the Microsoft ActiveX ' Data Objects 2.x Library (2.1 is the default library for Access 2000).

On Error GoTo ProcError

Dim strSQL As String Dim lngRecordsAffected As Long strSQL = "UPDATE tblEmployees SET tblEmployees.City = ""Bellevue"""

CurrentProject.Connection.Execute strSQL, lngRecordsAffected, adCmdText

MsgBox lngRecordsAffected & " records were successfully updated.", vbInformation

ExitProc: Exit Sub ProcError: MsgBox "Error: " & Err.Number & ". " & Err.Description, _ vbInformation, "Error in Test5 Procedure..." Resume ExitProc End Sub

Recommended publications