简体   繁体   English

将访问表从 Excel VBA 导出到 dBase 文件?

[英]Export Access Table to dBase file from Excel VBA?

I have an Excel "Application" where users add/edit/etc.我有一个 Excel“应用程序”,用户可以在其中添加/编辑/等。 data.数据。 When ready, they export this data, the final deliverable needing to be a dBase file.准备好后,他们导出这些数据,最终的交付物需要是一个 dBase 文件。 Since Excel 2007 no longer has the Save As dBase feature, I've created the following code to export my data to an Access Table.由于 Excel 2007 不再具有“另存为 dBase”功能,因此我创建了以下代码以将数据导出到 Access 表。

Is there any way in my VBA in Excel to go ahead and transfer the Access Table then into a dBase file?在 Excel 中的 VBA 中是否有任何方法可以继续将访问表传输到 dBase 文件中? Or do I need to do that step from Access itself?还是我需要从 Access 本身执行那一步?

I'm trying to keep everything in Excel to make future modification as easy as possible.我试图将所有内容都保留在 Excel 中,以使将来的修改尽可能容易。 Any help is appreciated.任何帮助表示赞赏。 If possible, would even be fine with doing from Access if the process can be automated in-sync with my Export process.如果可能的话,如果该过程可以与我的导出过程同步自动化,那么从 Access 中执行甚至可以。

Sub Export()
Dim dbConnection As ADODB.Connection
Dim dbFileName As String
Dim dbRecordset As ADODB.Recordset
Dim xRow As Long, xColumn As Long
Dim LastRow As Long

'Go to the worksheet containing the records you want to transfer.
Worksheets("FeedSamples").Activate
'Determine the last row of data based on column A.
LastRow = Cells(Rows.Count, 1).End(xlUp).row
'Create the connection to the database.
Set dbConnection = New ADODB.Connection
'Define the database file name
dbFileName = "\\agfiles\public\ITSD_ApDev\James Scurlock\Personal Project Notes\FeedSampleResults.accdb"
'Define the Provider and open the connection.
With dbConnection
    .Provider = "Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFileName & _
    ";Persist Security Info=False;"
    .Open dbFileName
End With
'Create the recordset
Set dbRecordset = New ADODB.Recordset
dbRecordset.CursorLocation = adUseServer
dbRecordset.Open Source:="ImportedData", _
ActiveConnection:=dbConnection, _
CursorType:=adOpenDynamic, _
LockType:=adLockOptimistic, _
Options:=adCmdTable
'Loop thru rows & columns to load records from Excel to Access.
'Assume row 1 is the header row, so start at row 2.
'ACCESS COLUMNS MUST BE NAMED EXACTLY THE SAME AS EXCEL COLUMNS
For xRow = 2 To LastRow
    dbRecordset.AddNew
    'Assume this is an 8-column (field) table starting with column A.
    For xColumn = 1 To 69
        dbRecordset(Cells(1, xColumn).value) = Cells(xRow, xColumn).value
    Next xColumn
    dbRecordset.Update
Next xRow

'Close the connections.
dbRecordset.Close
dbConnection.Close
'Release Object variable memory.
Set dbRecordset = Nothing
Set dbConnection = Nothing
'Optional:
'Clear the range of data (the records) you just transferred.
'Range("A2:H" & LastRow).ClearContents
MsgBox "Test"
Dim access As access.Application
Set access = "\\agfiles\public\ITSD_ApDev\James Scurlock\Personal Project Notes\FeedSampleResults.accdb"
access.DoCmd.OpenTable "ImportedData"
access.DoCmd.TransferDatabase acExport, "dBASE IV", "C:\", acTable, "ImportedData", "TEST.DBF"
DoCmd.Close acTable, "ImportedData"

'CREATE dBASE FILE!

End Sub

You could connect to the target Dbase IV file through ADO: ADO.NET OleDB and very old dBASE IV file您可以通过 ADO 连接到目标 Dbase IV 文件: ADO.NET OleDB 和非常旧的 dBASE IV 文件

Once you have a connection to Dbase IV, you can use the same code that you use to load data from excel to access to load the data into Dbase IV instead.连接到 Dbase IV 后,您可以使用用于从 excel 加载数据的相同代码来访问将数据加载到 Dbase IV 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM