【问题标题】:How to reference rows and columns in Access VBA如何在 Access VBA 中引用行和列
【发布时间】:2017-12-03 06:11:31
【问题描述】:

我有一个 22 列和大约 50 行的 excel 模板。该模板将填充字段报告数据,我想将每个字段报告存储在同一类型模板(22 列 + 一个 ID 列)中的 access 数据库中。如何像在 Excel 中一样使用 VBA 来引用 access 中的行和列?

例如,在 excel 中,为了将行从一个工作簿复制到另一个工作簿,我会这样做:

Workbook("WorkbookName1").Worksheets("Worksheet 1").Range("A4:V4").End(xlDown).Copy _ Destination:= ThisWorkbook.Worksheets(1).Range("A4") .Range(End(xlDown)))

【问题讨论】:

  • 不清楚你在问什么。是WHERE 子句,即WHERE ID = 1234.?
  • 我没有太多的 Access 知识,但我知道足够的 Excel VBA 。我希望我可以像使用工作表一样使用访问权限。比如说,数据库可能有 100 万条记录,我只想通过检测最后一条记录(最后一个 ID)并在其末尾从 Excel 报告中导入 50 条左右的记录来继续将报告数据转储到它上面。对数据库的查询将由其他人完成,我只想接触 VBA,而不是 SQL。
  • 这似乎是 IMO 的失败案例。不接触 SQL 就无法使用数据库对象。

标签: excel ms-access vba copy


【解决方案1】:

Access 和 Excel 非常不同。我认为您想将多个 Excel 文件(模板)中的数据自动加载到您的 Access 表中,对。查看下面的脚本。

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

从 Access 运行代码。它将从文件夹中的所有 Excel 文件中导入数据。

【讨论】:

  • 我将开始使用您提供的代码。非常感谢,非常感谢,相信我。总是很难开始。
  • 确实,入门是最难的部分。你现在正在路上。
猜你喜欢
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 2020-01-01
相关资源
最近更新 更多