Article ID: 177138
Article Last Modified on 12/5/2003
Option Explicit
Dim Cn As ADODB.Connection
Dim Cmd1 As ADODB.Command
Dim strConn, strQSQL As String
Private Sub Form_Load()
' Open connection. Set Cn = New ADODB.Connection
' Change the connect string to match your setup
strConn = "driver={SQL Server};server=MyServer;" & _
"uid=<username>;pwd=<strong password>;database=pubs"
Set Cn = New ADODB.Connection
Cn.Open strConn
Cn.CursorLocation = adUseClient
'Cmd1 executes a SQL Insert
strQSQL = "Insert INTO Stores (stor_id, stor_name, " & _
"stor_address, city) VALUES(?,?,?,?)"
Set Cmd1 = New ADODB.Command
With Cmd1
.ActiveConnection = Cn
.CommandText = strQSQL
.CommandType = adCmdText
.Parameters.Append .CreateParameter("", adChar, adParamInput, 4)
.Parameters.Append .CreateParameter("", adVarChar, adParamInput, 40)
.Parameters.Append .CreateParameter("", adVarChar, adParamInput, 40)
.Parameters.Append .CreateParameter("", adVarChar, adParamInput, 20)
End With
End Sub
Private Sub cmdInsert_Click()
MousePointer = vbHourglass
Cn.BeginTrans
Cmd1(0) = "101"
Cmd1(1) = "Store One"
Cmd1(2) = "123 Oak St."
Cmd1(3) = "Seattle"
Cmd1.Execute
Cn.BeginTrans 'Comment out and this code will run and if you do so,
'Make sure that you delete the record that this sample
'adds to the database.
Cmd1(0) = "102"
Cmd1(1) = "Store Two"
Cmd1(2) = "123 Main St."
Cmd1(3) = "Tacoma"
Cmd1.Execute
Cn.CommitTrans 'Comment out and this code will run.
Cn.CommitTrans
MousePointer = vbDefault
End Sub
Private Sub Form_Unload(Cancel As Integer)
Cn.Close
Set Cn = Nothing
Set Cmd1 = Nothing
End SubKeywords: kbinfo kbinterop kbdatabase KB177138