Hello, people
I use this vba code to insert data in access database 2007. It's working for one row, but when I try to use for more rows and columns give me "Type mismatch".
Code:
Sub Simple_SQL_Insert_Data()
Dim cn As ADODB.Connection '* Connection String
Dim oCm As ADODB.Command '* Command Object
Dim oWS As Worksheet
Set oWS = ActiveSheet
'Dim sName As String
Dim sLocation As String
Dim iRecAffected As Integer
On Error GoTo ADO_ERROR
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\ked\Desktop\last-padej\Database6.accdb;Persist Security Info=False"
cn.ConnectionTimeout = 40
cn.Open
sName = oWS.Range("e14").Value
sLocation = oWS.Range("f14").Value
'sName = "Krishna Vepakomma"
'sLocation = "Narayanaguda"
Set oCm = New ADODB.Command
oCm.ActiveConnection = cn
oCm.CommandText = "Insert Into SampleTable (UserName, Location) Values ('" & sName & "','" & sLocation & "')"
oCm.Execute iRecAffected
If iRecAffected = 0 Then
MsgBox "No records inserted"
End If
If cn.State <> adStateClosed Then
cn.Close
End If
Application.StatusBar = False
If Not oCm Is Nothing Then Set oCm = Nothing
If Not cn Is Nothing Then Set cn = Nothing
ADO_ERROR:
If Err <> 0 Then
'Debug.Assert Err = 0
MsgBox Err.Description
Err.Clear
Resume Next
End If
End Sub