Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 从Excel向Access提交数据_Sql Server_Vba_Excel_Ms Access - Fatal编程技术网

Sql server 从Excel向Access提交数据

Sql server 从Excel向Access提交数据,sql-server,vba,excel,ms-access,Sql Server,Vba,Excel,Ms Access,我每天收到多个excel文件。每一份都是相同的,包含施工工头的每日日志。在这些excel表格上,我有一个表格,领班每天用新信息更新表格。我希望能够在access中汇集这些表中的所有数据,并使用access reports功能为工头组创建每日报告 如果没有更好的方法,我如何从多个电子表格导入或导出这些信息到access上的一个表中 如果这也可以由工头在SQL server中远程完成,那也很高兴知道 编辑:我面临的特殊困难是access仅允许我选择表格所在的工作表,而不是表格本身。工作表中包含的信息

我每天收到多个excel文件。每一份都是相同的,包含施工工头的每日日志。在这些excel表格上,我有一个表格,领班每天用新信息更新表格。我希望能够在access中汇集这些表中的所有数据,并使用access reports功能为工头组创建每日报告

如果没有更好的方法,我如何从多个电子表格导入或导出这些信息到access上的一个表中

如果这也可以由工头在SQL server中远程完成,那也很高兴知道


编辑:我面临的特殊困难是access仅允许我选择表格所在的工作表,而不是表格本身。工作表中包含的信息比表中的信息更多。导入时是否有选择特定表的方法?另外,我希望能够在VBA中编程,这样我就可以按excel工作表上的一个按钮导出到我的access数据库中。我使用这段代码

Sub ExportToAccess()
    Dim PathOfAccess As String, myFn As String
    Dim strConn As String, strSQL As String


    PathOfAccess = "C:\Users\USER\Documents\Database1.accdb"
    myFn = ThisWorkbook.FullName

    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & PathOfAccess & ";"

    Set cn = CreateObject("ADODB.Connection")

    cn.Open strConn

strSQL = "INSERT  INTO table1  select * from [Sheet1$] IN '' " _
  & "[Excel 8.0;HDR=YES;IMEX=2;DATABASE=" & myFn & "]"

cn.Execute strSQL


End Sub

Sub ImportFromAccess()

    Dim Rs As Object
    Dim strConn As String, strSQL As String
    Dim i As Integer
    Dim Ws As Worksheet
    Dim PathOfAccess As String


    PathOfAccess = "C:\Users\USER\Documents\Database1.accdb"

    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & PathOfAccess & ";"

    Set Rs = CreateObject("ADODB.Recordset")

    strSQL = "SELECT * FROM table1 "
    Rs.Open strSQL, strConn

    Set Ws = ActiveSheet

    If Not Rs.EOF Then
         With Ws
            .Range("a1").CurrentRegion.ClearContents
            For i = 0 To Rs.Fields.Count - 1
               .Cells(1, i + 1).Value = Rs.Fields(i).Name
            Next
            .Range("a" & 2).CopyFromRecordset Rs
        End With
    End If
    Rs.Close
    Set Rs = Nothing
End Sub

您可以轻松地将数据从Excel导出到Access

Sub DAOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim db As Database, rs As Recordset, r As Long
    Set db = OpenDatabase("C:\FolderName\DataBaseName.mdb") 
    ' open the database
    Set rs = db.OpenRecordset("TableName", dbOpenTable) 
    ' get all records in a table
    r = 3 ' the start row in the worksheet
    Do While Len(Range("A" & r).Formula) > 0 
    ' repeat until first empty cell in column A
        With rs
            .AddNew ' create a new record
            ' add values to each field in the record
            .Fields("FieldName1") = Range("A" & r).Value
            .Fields("FieldName2") = Range("B" & r).Value
            .Fields("FieldNameN") = Range("C" & r).Value
            ' add more fields if necessary...
            .Update ' stores the new record
        End With
        r = r + 1 ' next row
    Loop
    rs.Close
    Set rs = Nothing
    db.Close
    Set db = Nothing
End Sub
“这在EXCEL中运行

现在,如果您想从多个Excel文件(我想是在一个文件夹中)导入数据,可以尝试下面的脚本

Dim strPathFile As String, strFile As String, strPath As String
 Dim strTable As String
 Dim blnHasFieldNames As Boolean

' Change this next line to True if the first row in EXCEL worksheet
 ' has field names
 blnHasFieldNames = False

' Replace C:\Documents\ with the real path to the folder that
 ' contains the EXCEL files
 strPath = "C:\Documents\"

' Replace tablename with the real name of the table into which 
 ' the data are to be imported
 strTable = "tablename"

 strFile = Dir(strPath & "*.xls")
 Do While Len(strFile) > 0
       strPathFile = strPath & strFile
       DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
             strTable, strPathFile, blnHasFieldNames

' Uncomment out the next code step if you want to delete the 
 ' EXCEL file after it's been imported
 '       Kill strPathFile

       strFile = Dir()
 Loop
' THIS RUNS IN ACCESS

用T-SQL编写一个存储过程,并将其放在计时器上。如果这些工作表符合表的布局,Access的
import
功能非常简单。您面临的特殊困难是什么?我面临的特殊困难是访问权限仅允许我选择表格所在的工作表,而不是表格本身。工作表中包含的信息比表中的信息更多。导入时是否有选择特定表的方法?另外,我希望能够用VBA编程,这样我就可以按excel工作表上的按钮导出到access数据库。工作表上的“表”是什么意思?是指范围吗?