Results 1 to 6 of 6
  1. #1
    krasi_e_d is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    2

    Insert all rows to access database

    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

    Last edited by June7; 10-06-2012 at 12:28 PM. Reason: mod edit to use CODE tags and indentation

  2. #2
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Your code as shown above inserts only one record to MS Access - what does the code that does not work look like, and where does it fail? To find out, turn off the error handling (comment out On Error...), and use debug mode to show you (and us) where the error is.

    John

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    Presume code is behind Excel.

    I don't see code that involves more than one row.

    Show the code you attempted for multiple rows.

    iRecAffected is declared and used but how would it ever not equal 0? I don't see it being incremented.

    I don't understand this line: oCm.Execute iRecAffected

    Could you just create Access link to the Excel sheet?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    krasi_e_d is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    2
    Quote Originally Posted by John_G View Post
    Your code as shown above inserts only one record to MS Access - what does the code that does not work look like, and where does it fail? To find out, turn off the error handling (comment out On Error...), and use debug mode to show you (and us) where the error is.

    John
    Quote Originally Posted by June7 View Post
    Presume code is behind Excel.

    I don't see code that involves more than one row.

    Show the code you attempted for multiple rows.

    iRecAffected is declared and used but how would it ever not equal 0? I don't see it being incremented.

    I don't understand this line: oCm.Execute iRecAffected

    Could you just create Access link to the Excel sheet?
    Guys,
    I gave you different code this is the code, which I try:
    Code:
    Option Compare Database
    Private Sub importExcelData()
    Dim xlApp As Excel.Application
    
    Dim xlBk As Excel.Workbook
    
    Dim xlSht As Excel.Worksheet
    Dim dbRst As Recordset
    
    Dim dbs As Database
    
    Dim SQLStr As String
    
    Set dbs = CurrentDb
    
    Set xlApp = Excel.Application
    
    Set xlBk = xlApp.Workbooks.Open("C:\Users\ked\Desktop\last-padej\clear-file.xls")
    
    Set xlSht = xlBk.Sheets(1)
    
    'SQLStr = "CREATE TABLE excelData(columnOne TEXT, columnTwo TEXT, columnTre TEXT)"
    'DoCmd.SetWarnings False
    
    'DoCmd.RunSQL (SQLStr)
    
    Set dbRst = dbs.OpenRecordset("excelData")
    
    dbRst.AddNew
    
    xlSht.Range("a13").Select
    
    dbRst.Fields(0).Value = xlSht.Range("a13").Value
    
    xlSht.Range("B2").Select
    
    dbRst.Fields(1).Value = xlSht.Range("B2").Value
    dbRst.Update
    dbRst.Close
    dbs.Close
    
    xlBk.Close
    End Sub

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    Did you debug (see link at bottom of my post for guidelines)? What line is triggering the error? Still don't see code for mulitiple rows.

    Want to provide the Excel file for analysis?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    If this is the exact code you are using, it won't work because you have not created the table ExcelData - the commands to do that are commented out. As a result, the OpenRecordset command fails. Does the error message you get indicate that is the line causing the problem?

    John

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Insert a ".mdf" database into Access
    By Philosophaie in forum Access
    Replies: 4
    Last Post: 08-28-2012, 01:11 AM
  2. insert multiple rows in access
    By learning_graccess in forum Queries
    Replies: 1
    Last Post: 03-31-2012, 10:15 AM
  3. Insert x number of rows based on value
    By opopanax666 in forum Programming
    Replies: 4
    Last Post: 10-26-2010, 03:26 AM
  4. how to insert image in database
    By sbglobal in forum Programming
    Replies: 1
    Last Post: 08-26-2010, 08:43 AM
  5. Insert an image in database
    By microbert in forum Database Design
    Replies: 1
    Last Post: 06-22-2009, 01:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums