1
votes

I'm trying to create a dBase file since it seems the .TransferDatabase() method in my Export vba code requires the file to exist (keep getting "Field will not fit in record". Assuming due to the file not existing).

I came across one method online using 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

but when trying to run it just for testing out the below line receives "Compile error: User-defined type not defined":

Public Function createDBF(strName As String, _
                      strFolder As String, _
             Optional pFields As IFields) As ITable

Another thread (from 2003...) suggested a query could be used to Export my Access Table to dBase file:

http://www.pcreview.co.uk/forums/creating-dbf-file-using-access-vba-code-t1077674.html

but my syntax appears to be off, receiving "Syntax error in query. Incomplete query clause:

SELECT ImportedData.*
INTO "...filePath\Personal Project Notes\" "dBase IV;"
FROM ImportedData;

If anyone has any ideas to fix these or even a different solution I'm all ears. Again, trying to create the dBase file since my following export code flags the .TransferDatabase() method saying "Field will not fit in record":

Export Code:

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

The filepath is correct, but currently TEST.DBF does not exist. I was originally assuming the method created the file for me...

EDIT:

After some testing, I shrunk my Access table down to 15 fields TEXT at 255 characters. When running the Access gui-Export I was successfully able to create a dBase IV file with my 15 fields. Since I then had my .DBF file, I placed it in my .TransferDatabase() file path and my vba code in Excel executed successfully from Excel to Access to dBase file!

I now just need to figure out what the issue is when exporting 69 records versus 15....

Any thoughts?

1
Just for kicks began using the gui-export-to-dBase in Access and playing around with my fields. Since I keep getting the "Field will not fit in record" error, I tried deleting a few fields. I currently have a total of 69 fields (discluding the ID field) all set as Text - 255 chars. When I deleted all but 11 fields, the export to dBase IV worked!? Anyone have ideas on this? Know the limitations of dBase fields/files? This is my first attempt at dBase files ever.Analytic Lunatic
What's your original problem? You're trying to .transferdatabase() from what to what?Skip Intro
Since Excel 2007 no longer supports export to .DBF, I have to take records in Excel, Export to Access, and I'm trying from my Excel vba code to then Export the Access Table into a dBase file. The .TransferDatabase() was giving me a "Field will not fit in record" error. With 11 fields at 255 characters, my code will now execute successfully from Excel, to Access, into dBase file. Trying to export all 69 fields though still gives me the "Field will not fit in record" error.Analytic Lunatic
There is an SaveDBF add-in that restores the Excel to .dbf functionality HERESkip Intro
Also, aren't dBase char fields length 254, rather than 255?Skip Intro

1 Answers

1
votes

The following procedure worked for me in Access 2010.

I keep a working directory called C:\__tmp\ for messing around with things. I created a new subfolder named dbfTest which starts out empty:

C:\__tmp\dbfTest>dir
 Volume in drive C has no label.
 Volume Serial Number is 2A3E-1929

 Directory of C:\__tmp\dbfTest

2013-06-07  10:49    <DIR>          .
2013-06-07  10:49    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  305,532,461,056 bytes free

Then I ran the following VBA code in Access to export my "Clients" table to "CLIENTS.DBF"

Sub dbfExportTest()
DoCmd.TransferDatabase acExport, "dBase IV", "C:\__tmp\dbfTest\", acTable, "Clients", "CLIENTS"
End Sub

When that finished my dbfTest folder contained the following:

C:\__tmp\dbfTest>dir
 Volume in drive C has no label.
 Volume Serial Number is 2A3E-1929

 Directory of C:\__tmp\dbfTest

2013-06-07  10:55    <DIR>          .
2013-06-07  10:55    <DIR>          ..
2013-06-07  10:55             5,942 CLIENTS.DBF
2013-06-07  10:55               863 CLIENTS.DBT
2013-06-07  10:55                59 CLIENTS.INF
2013-06-07  10:55             6,144 CLIENTS.MDX
               3 File(s)         12,145 bytes
               2 Dir(s)  305,532,440,576 bytes free

My [Clients] table in Access has the following structure:

FieldName  Type
---------  ----------
ID         AutoNumber
LastName   Text(255)
FirstName  Text(255)
Email      Text(255)
Selection  Yes/No
intCol     Number(Long Integer)
dtmCol     Date/Time
Comments   Memo

Edit re: comment

dBASE_III and dBASE_IV files apparently have a maximum record length of 4,000 bytes (ref: here). This suggests that a dBASE_IV file could not hold more than 15 Text(254) fields. Possible workarounds include:

  • change the table structure in Access to shorten Text(255) fields if they are not using all 255 characters, or

  • convert Text() fields to Memo prior to exporting, since dBASE, like Access, only stores a link to the Memo field in the main table (10 bytes for dBASE).