This content originally appeared on DEV Community and was authored by Wild Cat
Common procedures
'Common Setting
Private Const ServerName As String = "myServerName"
Private Const DatabaseName As String = "myDatabaseName"
'SQL Server Authentication Mode Setting
Private Const UserID As String = "myID"
Private Const Password As String = "myPassword"
Public Sub OpenConnection(ByRef cn As ADODB.Connection)
cn.ConnectionTimeout = 100 '100 seconds
'# SQL Server Authentication Mode
cn.ConnectionString = "Provider=SQLOLEDB;" & _
"Data Source=" & ServerName & ";" & _
"Initial Catalog=" & DatabaseName & ";" & _
"USER ID=" & UserID & ";" & _
"PASSWORD=" & Password & ";"
'# Windows Authentication Mode
'cn.ConnectionString = "Provider=SQLOLEDB;" & _
' "Data Source=" & ServerName & ";" & _
' "Initial Catalog=" & DatabaseName & ";" & _
' "Integrated Security=SSPI;"
cn.Open
End Sub
Public Sub OpenRecordsetToRead(ByRef cn As ADODB.Connection, _
ByRef rs As ADODB.Recordset, _
ByVal sql As String)
rs.CursorLocation = adUseClient
rs.CursorType = adOpenForwardOnly
rs.LockType = adLockReadOnly
rs.ActiveConnection = cn
rs.Source = sql
rs.Open
End Sub
Public Sub OpenRecordsetToUpdate(ByRef cn As ADODB.Connection, _
ByRef rs As ADODB.Recordset, _
ByVal sql As String)
rs.CursorLocation = adUseClient
rs.CursorType = adOpenDynamic
rs.LockType = adLockPessimistic
rs.ActiveConnection = cn
rs.Source = sql
rs.Open
End Sub
Public Sub CloseRecordset(ByRef rs As ADODB.Recordset)
If Not rs Is Nothing Then
If rs.State = adStateOpen Then rs.Close
Set rs = Nothing
End If
End Sub
Public Sub CloseConnection(ByRef cn As ADODB.Connection)
If Not cn Is Nothing Then
If cn.State = adStateOpen Then cn.Close
Set cn = Nothing
End If
End Sub
Select
Sub GetRecordset()
On Error GoTo ErrHandler
Dim sql As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
sql = "SELECT * FROM SampleTable"
Call OpenConnection(cn)
Call OpenRecordsetToRead(cn, rs, sql)
If rs Is Nothing Or (rs.BOF And rs.EOF) Then
Exit Sub
End If
Do Until rs.EOF
Debug.Print rs.Fields(0).Value 'Show 1st filed of table
Debug.Print rs.Fields(1).Value 'Show 2nd filed of table
rs.MoveNext
Loop
Call CloseRecordset(rs)
Call CloseConnection(cn)
Exit Sub
ErrHandler:
Call CloseRecordset(rs)
Call CloseConnection(cn)
Debug.Print "ErrNumber:" & Err.Number & " " & Err.Description
End Sub
Update, Insert or Delete
Public Sub ExecuteSQL()
On Error GoTo ErrHandler
Dim cn As New ADODB.Connection
Dim sql As String
Call OpenConnection(cn)
sql = "INSERT INTO SampleTable (No, FirstName, LastName) Values(1,'John','Smith')"
'cn.BeginTrans '#Begin transaction
cn.Execute sql
'cn.CommitTrans '#Commit transaction
Call CloseConnection(cn)
Exit Sub
ErrHandler:
'cn.RollbackTrans '#Rollback
Call CloseConnection(cn)
Debug.Print "ErrNumber:" & Err.Number & " " & Err.Description
End Sub
This content originally appeared on DEV Community and was authored by Wild Cat
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
![](https://www.radiofree.org/wp-content/plugins/print-app/icon.jpg)
APA
MLA
Wild Cat | Sciencx (2024-10-13T01:15:19+00:00) Connect MS Access to SQL Server using ADO. Retrieved from https://www.scien.cx/2024/10/13/connect-ms-access-to-sql-server-using-ado/
" » Connect MS Access to SQL Server using ADO." Wild Cat | Sciencx - Sunday October 13, 2024, https://www.scien.cx/2024/10/13/connect-ms-access-to-sql-server-using-ado/
HARVARDWild Cat | Sciencx Sunday October 13, 2024 » Connect MS Access to SQL Server using ADO., viewed ,<https://www.scien.cx/2024/10/13/connect-ms-access-to-sql-server-using-ado/>
VANCOUVERWild Cat | Sciencx - » Connect MS Access to SQL Server using ADO. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/13/connect-ms-access-to-sql-server-using-ado/
CHICAGO" » Connect MS Access to SQL Server using ADO." Wild Cat | Sciencx - Accessed . https://www.scien.cx/2024/10/13/connect-ms-access-to-sql-server-using-ado/
IEEE" » Connect MS Access to SQL Server using ADO." Wild Cat | Sciencx [Online]. Available: https://www.scien.cx/2024/10/13/connect-ms-access-to-sql-server-using-ado/. [Accessed: ]
rf:citation » Connect MS Access to SQL Server using ADO | Wild Cat | Sciencx | https://www.scien.cx/2024/10/13/connect-ms-access-to-sql-server-using-ado/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.