2011-05-02 75 views
1

我需要一個代碼,其中用戶(粘貼數據表中的數據後),當他點擊一個按鈕時,數據將被插入到表中(我正在考慮在這裏使用SQL語句)。除此之外,該表中的一個字段必須使用DCount函數進行計算。所以我需要計算每條記錄。讓我知道如果這個想法不明確,我需要進一步解釋。另外,如果有更好的想法,請告訴。謝謝!按批次插入數據?

+0

說明數據表粘貼的功能:用戶從Excel使用的數據,例如? – Alex 2011-05-04 14:00:38

+0

是的,來自Excel的數據將被粘貼到Access數據表。我有點解決它。事實證明,當數據從excel粘貼到訪問時存在問題。你必須使用剪貼板粘貼數據才能訪問。 – Ali 2011-05-05 03:18:16

+0

爲什麼不直接導入電子表格並避免強迫用戶手動完成這麼多工作? – 2011-05-06 03:57:28

回答

0

爲什麼不創建一些VBA來從電子表格中提取數據,將其插入到表格中,然後再次關閉excel。所有的按鈕點擊

看看下面的一個例子

Dim objApp As Excel.Application 
Dim objBook As Excel.Workbook 
Dim objSheet As Excel.Worksheet 
Dim sSQL As String 
Dim Path As String 

Set db = CurrentDb() 

Set objBook = Workbooks.Add(Template:=SheetLocation) 'Your excel spreadsheet file goes here where "SheetLocation is typed 
      Set objApp = objBook.Parent 
       Set objSheet = objBook.Worksheets("Sheet1") 'Name of sheet you want to View 
        objBook.Windows(1).Visible = True 
        objApp.Visible = True 

      objApp.DisplayAlerts = False 




    Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long 
' connect to the Access database 
Set cn = CurrentProject.Connection 

' open a recordset 
Set rs = New ADODB.Recordset 
rs.Open "INSERT YOUR TABLE NAME HERE", cn, adOpenKeyset, adLockOptimistic, adCmdTable 
' all records in a table 
r = 2 ' 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("INSERT THE TABLE FIELD TO DUMP DATA IN HERE") = Range("A" & r).Value 
     .Fields("REPEAT AS ABOVE") = Range("B" & r).Value 
     .Fields("REPEAT AS ABOVE") = Range("C" & r).Value 
     .Fields("REPEAT AS ABOVE") = Range("D" & r).Value 
     .Fields("REPEAT AS ABOVE") = Range("E" & r).Value 
     .Fields("REPEAT AS ABOVE") = Range("F" & r).Value 
     .Fields("REPEAT AS ABOVE") = Range("G" & r).Value 
     .Fields("REPEAT AS ABOVE") = Range("H" & 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 
cn.Close 
Set cn = Nothing 
    objApp.Quit 
+1

這不是按照原始問題的要求,而是按照批次逐行進行。只要您使用適當的連接字符串,當您可以通過SQL獲取數據時,自動化Excel也是沒有意義的。 – 2011-05-07 20:42:37