循环访问Access数据库并使用Excel VBA执行基本操作

时间:2018-11-09 15:37:22

标签: excel vba excel-vba ms-access

我一直通过excel执行这些操作,但是我正在处理的原始数据对于excel来说是压倒性的。我转移到访问作为数据库,但我试图找到类似的功能。我仍在使用excel作为界面。

本质上是将数据分解为样本。提取数据样本,对其进行分析,然后继续进行下一个样本。我想对Access数据做同样的事情。我正在寻找的是如何使用for循环对样本执行操作(求和,乘法,除法,最大,最小...)。另外,最大值的ID和最小值的ID是什么。我还要处理大量原始数据,因此较少的代码行是最佳的。朝正确方向的任何指针将不胜感激。

Excel中的代码

For CoUnTer = 1 To FindRecordCount

        Set Circ_Rng = WS.Range(WS.Cells(CoUnTer , 5), WS.Cells(CoUnTer  + TempTimeFrame - 1, 5))
        Set DataPoint_Rng = WS.Range(WS.Cells(CoUnTer , 1), WS.Cells(CoUnTer  + TempTimeFrame - 1, 1))
        Set DataPoint_Circ_Rng = WS.Range(WS.Cells(CoUnTer , 8), WS.Cells(CoUnTer  + TempTimeFrame - 1, 8))
        Set DataPoint_SQ_Rng = WS.Range(WS.Cells(CoUnTer , 9), WS.Cells(CoUnTer  + TempTimeFrame - 1, 9))
        '---------------------------
        MaxPoint = WorksheetFunction.Max(CircPressure_Rng)
        Row_At_MaxPressure = WS.Columns(5).Cells.Find(MaxPoint, After:=WS.Cells(3, 5), SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlNext).Row

    Sigma_X_Sigma_Y = WorksheetFunction.Sum(Circ_Rng) * WorksheetFunction.Sum(DataPoint_Rng)
    Sigma_XY = WorksheetFunction.Sum(DataPoint_Circ_Rng)
    Sigma_X2 = WorksheetFunction.Sum(DataPoint_SQ_Rng)
    Min_X = WorksheetFunction.Min(DataPoint_Rng)

访问数据代码: 此代码获取数据样本。我所缺少的是如何像在excel中一样对它们进行处理。

Sub GetAccessData()


Dim DBFullName As String
Dim Connect As String, Source As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim Col As Long
Dim x As Long
Application.ScreenUpdating = False

'DataBase Path
DBFullName = WB_Path & "\RawData - Template.accdb"

'Open the Connection
Set Connection = New ADODB.Connection
Connect = "Provider=Microsoft.ACE.OLEDB.12.0;"
Connect = Connect & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Connect

'Create a RecordSet
Set Recordset = New ADODB.Recordset
' Client-side cursor
Recordset.CursorLocation = adUseClient

With Recordset
    Source = "SELECT * FROM  RawData WHERE [ID] BETWEEN " & StartofData & " AND " & EndofData
    .Open Source:=Source, ActiveConnection:=Connection
    On Error Resume Next

End With
EndofData = Recordset.RecordCount

Set Recordset = Nothing
Connection.Close
Set Connection = Nothing
'Application.ScreenUpdating = True
'Application.EnableEvents = True

End Sub

1 个答案:

答案 0 :(得分:0)

正如您所看到的,Excel在某些方面更好,而Access在某些方面更好。有时,您可以用Access替代Excel,反之亦然,但是通常每个人都应按预期用途使用它。这里可能有一些前进的方法,但是我倾向于将您的Excel内容留在Excel中,并通过Access控制Excel。您可以使用后期绑定或早期绑定来控制另一个应用程序中的一个。这是您要考虑的两个选项。

‘EARLY BINDING
Option Compare Database
Option Explicit ' Use this to make sure your variables are defined

' One way to be able to use these objects throughout the Module is to Declare them
' Here and not in a Sub

Private objExcel As Excel.Application
Private xlWB As Excel.Workbook
Private xlWS As Excel.Worksheet

Sub Rep()

Dim strFile As String

strFile = "C:\your_path_here\YourExcelFile.xls"

' Opens Excel and makes it Visible
Set objExcel = New Excel.Application
objExcel.Visible = True

'Opens up the Workbook
Set xlWB = objExcel.Workbooks.Open(strFile)

'Sets the Workseet to the last active sheet - Better to use the commented version and use the name of the sheet.
Set xlWS = xlWB.ActiveSheet
'Set xlWS = xlWB("Sheet2")

With xlWS ' You are now working with the Named file and the named worksheet


End With

'Do Close and Cleanup
End Sub


 
‘LATE BINDING
Sub ControlExcelFromAccess()

' No reference to a type library is needed to use late binding.
' As long as the object supports IDispatch, the method can
' be dynamically located and invoked at run-time.

' Declare the object as a late-bound object
  Dim oExcel As Object
  Dim strFile As String

  strFile = "C:\your_path_here\YourExcelFile.xls"

  Set oExcel = CreateObject("Excel.Application")

' The Visible property is called via IDispatch
  oExcel.Visible = True

  Set xlWB = oExcel.Workbooks.Open(strFile)

'Call code here . . .

Set oExcel = Nothing

End Sub

只需将您的代码复制/粘贴到此代码中,如有必要,请进行一些小调整,就可以了!