【发布时间】:2013-06-03 20:35:33
【问题描述】:
我正在尝试创建一个 dBase 文件,因为我的 Export vba 代码中的 .TransferDatabase() 方法似乎需要该文件存在(不断收到“字段不适合记录”。假设由于文件不存在)。
我在网上遇到了一种使用 vba 的方法:
http://edndoc.esri.com/arcobjects/8.3/samples/geodatabase/creating%20data/createnewdbf.htm
Public Function createDBF(strName As String, _
strFolder As String, _
Optional pFields As IFields) As ITable
' createDBF: simple function to create a DBASE file.
' note: the name of the DBASE file should not contain the .dbf extension
'
On Error GoTo EH
' Open the Workspace
Dim pFWS As IFeatureWorkspace
Dim pWorkspaceFactory As IWorkspaceFactory
Dim fs as object
Dim pFieldsEdit As esriCore.IFieldsEdit
Dim pFieldEdit As esriCore.IFieldEdit
Dim pField As IField
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(strFolder) Then
MsgBox "Folder does not exist: " & vbCr & strFolder
Exit Function
End If
Set pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0)
' if a fields collection is not passed in then create one
If pFields Is Nothing Then
' create the fields used by our object
Set pFields = New esriCore.Fields
Set pFieldsEdit = pFields
pFieldsEdit.FieldCount = 1
'Create text Field
Set pField = New Field
Set pFieldEdit = pField
With pFieldEdit
.Length = 30
.Name = "TextField"
.Type = esriFieldTypeString
End With
Set pFieldsEdit.Field(0) = pField
End If
Set createDBF = pFWS.CreateTable(strName, pFields, Nothing, Nothing, "")
Exit Function
EH:
MsgBox Err.Description, vbInformation, "createDBF"
End Function
但是当试图运行它只是为了测试以下行时收到“编译错误:未定义用户定义的类型”:
Public Function createDBF(strName As String, _
strFolder As String, _
Optional pFields As IFields) As ITable
另一个线程(从 2003 年开始...)建议使用查询将我的访问表导出到 dBase 文件:
http://www.pcreview.co.uk/forums/creating-dbf-file-using-access-vba-code-t1077674.html
但我的语法似乎已关闭,收到“查询中的语法错误。查询子句不完整:
SELECT ImportedData.*
INTO "...filePath\Personal Project Notes\" "dBase IV;"
FROM ImportedData;
如果有人有任何想法来解决这些问题,甚至是不同的解决方案,我都会倾听。再次尝试创建 dBase 文件,因为我的以下导出代码将 .TransferDatabase() 方法标记为“字段不适合记录”:
导出代码:
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 = "...filePath\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 acx As access.Application
Set acx = New access.Application
acx.OpenCurrentDatabase ("...filePath\Personal Project Notes\FeedSampleResults.accdb")
acx.DoCmd.TransferDatabase acExport, "dBase IV", "...filePath\Personal Project Notes\", acTable, "ImportedData", "TEST.DBF"
acx.CloseCurrentDatabase
End Sub
文件路径正确,但当前 TEST.DBF 不存在。我最初假设该方法为我创建了文件...
编辑:
经过一些测试,我将 Access 表缩小到 15 个 TEXT 字段,长度为 255 个字符。运行 Access gui-Export 时,我成功地创建了一个包含 15 个字段的 dBase IV 文件。因为我有我的 .DBF 文件,所以我将它放在我的 .TransferDatabase() 文件路径中,并且我在 Excel 中的 vba 代码从 Excel 成功执行到 Access 到 dBase 文件!
我现在只需要弄清楚导出 69 条记录而不是 15 条记录时的问题......
有什么想法吗?
【问题讨论】:
-
只是为了好玩开始在 Access 中使用 gui-export-to-dBase 并玩弄我的领域。由于我不断收到“字段不适合记录”错误,我尝试删除一些字段。我目前共有 69 个字段(不包括 ID 字段)全部设置为 Text - 255 个字符。当我删除除 11 个字段之外的所有字段时,导出到 dBase IV 有效!?有人对此有想法吗?知道 dBase 字段/文件的限制吗?这是我第一次尝试 dBase 文件。
-
你原来的问题是什么?你想
.transferdatabase()从什么到什么? -
由于 Excel 2007 不再支持导出到 .DBF,我必须在 Excel 中记录,导出到 Access,我正在尝试从我的 Excel vba 代码中将 Access 表导出到 dBase文件。 .TransferDatabase() 给了我一个“字段不适合记录”错误。在 255 个字符的 11 个字段中,我的代码现在将成功地从 Excel 执行到 Access,再到 dBase 文件。尝试导出所有 69 个字段,但仍然给我“字段不适合记录”错误。
-
有一个 SaveDBF 插件可以将 Excel 恢复为 .dbf 功能HERE
-
另外,
dBasechar 字段的长度不是 254,而不是 255?
标签: vba excel ms-access-2007 syntax-error dbase