Export data from Excel to Access - Error: Argument not Optional

后端 未结 1 955
[愿得一人]
[愿得一人] 2021-01-17 03:21

I\'m trying to export data from Excel 2010 to Access 2010 with a VBA code on my excel file when I press a button. I just want to export the data from \"Water Quality\" sheet

1条回答
  •  时光说笑
    2021-01-17 04:03

    Here are a few examples for inserting all the data at once:

    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=z:\docs\test.accdb"
    
    ''Late binding, so no reference is needed
    Set cn = CreateObject("ADODB.Connection")
    
    cn.Open strCon
    
    ''Create a table called ATable
    scn = "[Excel 8.0;HDR=YES;DATABASE=" & ActiveWorkbook.FullName & "]"
    strSQL = "SELECT * INTO ATable " _
           & "FROM " & scn & ".[Sheet7$A1:C4]"
    
    ''Execute the statement
    cn.Execute strSQL
    
    ''Insert into a table called ATable
    scn = "[Excel 8.0;HDR=YES;DATABASE=" & ActiveWorkbook.FullName & "]"
    strSQL = "INSERT INTO ATable " _
           & "SELECT * FROM " & scn & ".[Sheet7$A1:C4]"
    
    ''Execute the statement
    cn.Execute strSQL
    
    ''Insert into a table with no column header in Excel, 
    ''the fields are [afield],[atext],[another]
    scn = "[Excel 8.0;HDR=NO;DATABASE=" & ActiveWorkbook.FullName & "]"
    strSQL = "INSERT INTO ATable ([afield],[atext],[another]) " _
           & "SELECT F1 As afield, F2 As AText, F3 As another FROM " _
           & scn & ".[Sheet7$A1:C4]"
    
    ''Execute the statement
    cn.Execute strSQL
    

    0 讨论(0)
提交回复
热议问题