2017-08-04 161 views
0

我有一個快速的問題。所以我將所有數據庫存儲在訪問中(這是本地的),然後使用Excel的電源查詢從訪問中導入數據。但是我希望在Excel電子表格中進行的任何更改(即我從訪問導入信息)直接使用電源查詢進行訪問?有什麼辦法嗎?Excel Power Query

在此先感謝!

+1

我猜也許是在VBA爲平變化子說,只要提交紙張保存的更改訪問或做。微軟應用程序之間的互動是強大的,通過VBA,所以非常肯定會有辦法。 –

回答

0

我不認爲這是一個好主意,但你可以嘗試這樣的概念。

Sub ImportFromAccess() 
    Dim conn As ADODB.Connection 
    Dim myRecordset As ADODB.Recordset 
    Dim strConn As String 

    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\your_path_here\Northwind.mdb" 

    Set myRecordset = New ADODB.Recordset 

    FocusRow = ActiveCell.Row 
    With myRecordset 
     .Open "SELECT * FROM PersonInformation WHERE ID=" & Worksheets("Sheet1").Range("A2").Value, _ 
     strConn, adOpenKeyset, adLockOptimistic 
     ' This assumes that ID is a number field. If it is a text field, use 
     ' .Open "SELECT * FROM PersonInformation WHERE ID='" & Worksheets("Sheet1").Range("A2").Value & "'", _ 
     strConn, adOpenKeyset, adLockOptimistic 

     .Fields("ID").Value = Worksheets("Sheet1").Range("A" & FocusRow).Value 
     .Fields("FName").Value = Worksheets("Sheet1").Range("B" & FocusRow).Value 
     .Fields("LName").Value = Worksheets("Sheet1").Range("C" & FocusRow).Value 
     .Fields("Address").Value = Worksheets("Sheet1").Range("D" & FocusRow).Value 
     .Fields("Age").Value = Worksheets("Sheet1").Range("E" & FocusRow).Value 
     .Update 
     .Close 
    End With 
    Set myRecordset = Nothing 
    Set conn = Nothing 
End Sub 

AND

Sub UpdateRecordsInAccess() 
    Dim rng As Range 
    Dim r As Long 
    Dim conn As ADODB.Connection 
    Dim strConn As String 
    Dim strSQL As String 

    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ 
     "C:\your_path_here\Northwind.mdb" 
    Set conn = New ADODB.Connection 
    conn.Open strConn 

    With Worksheets("Sheet1") 
     lastrow = .Range("A2").End(xlDown).Row 
     lastcolumn = .Range("A2").End(xlToRight).Column 
     Set rng = .Range(.Cells(lastrow, 1), .Cells(lastrow, lastcolumn)) 
    End With 

     'therow = 1 

     For i = 2 To lastrow 
      'r = rng.Row 
      'If r > 1 Then 
       strSQL = "UPDATE PersonInformation SET " & _ 
        "FName='" & Worksheets("Sheet1").Range("B" & i).Value & "', " & _ 
        "LName='" & Worksheets("Sheet1").Range("C" & i).Value & "', " & _ 
        "Address='" & Worksheets("Sheet1").Range("D" & i).Value & "', " & _ 
        "Age=" & Worksheets("Sheet1").Range("E" & i).Value & " WHERE " & _ 
        "ID=" & Worksheets("Sheet1").Range("A" & i).Value 
       conn.Execute strSQL 
      'End If 
      'r = r + 1 
     Next i 


    conn.Close 
    Set conn = Nothing 
End Sub 

enter image description here

enter image description here

+0

我試圖按照你的建議,但是當我實現更新訪問時,它顯示一個錯誤說應用程序定義或對象錯誤。 – shaziya

+0

工具>參考> Microsoft Active X數據對象2.8庫 – ryguy72

+0

我這樣做,但它說應用程序定義或對象錯誤,我張貼我目前正在處理的代碼 – shaziya

0

子UpdateRecordsInAccess() 昏暗RNG作爲範圍 「點心R作爲龍 昏暗康恩作爲ADODB.Connection 函數創建作爲字符串 昏暗STRSQL作爲字符串

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\username\Desktop\DatabaseResplann.mdb;" 
Set conn = New ADODB.Connection 
conn.Open strConn 


With Worksheets("Sheet1") 
    lastrow = .Range("A2").End(xlDown).Row 
    lastcolumn = .Range("A2").End(xlToRight).Column 
    Set rng = .Range(.Cells(lastrow, 1), .Cells(lastrow, lastcolumn)) 
End With 

    'therow = 1 

    For i = 2 To lastrow 
     'r = rng.Row 
     'If r > 1 Then 
      strSQL = "UPDATE Allocation SET " & _ 
       "Resource Name='" & Worksheets("Sheet1").Range("B" & i).Value & "', " & _ 
       "Child PID='" & Worksheets("Sheet1").Range("C" & i).Value & "', " & _ 
       "Fct wk#='" & Worksheets("Sheet1").Range("D" & i).Value & "', " & _ 
       "Fct Hrs='" & Worksheets("Sheet1").Range("E" & i).Value & "', " & _ 
       "Fct %='" & Worksheets("Sheet1").Range("F" & i).Value & "', " & _ 
       "Comment='" & Worksheets("Sheet1").Range("G" & i).Value & " WHERE " & _ 
       "Resource ID='" & Worksheets("Sheet1").Range("A" & i).Value 
      conn.Execute strSQL 
     'End If 
     'r = r + 1 
    Next i 


conn.Close 
Set conn = Nothing 

末次

相關問題