Results 1 to 6 of 6
  1. #1
    AlamBique is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2012
    Posts
    2

    In Access how do I change data type in importing field from Excel

    Hi there

    I'm using Access 2007 to import data from Excel using VBA. I have this code:

    Private Sub cmd1Importa_Click()
    Dim myRec As DAO.Recordset
    Dim strQry As String
    Dim dbExcel As DAO.Database
    Dim rsExcel As DAO.Recordset

    Set myRec = CurrentDb.OpenRecordset("Tabela1")

    Set dbExcel = OpenDatabase("C:\BasesTestes\Testes\TabelaParaImpo rtar.xlsx", False, True, "Excel 12.0; IMEX=1;")


    Set rsExcel = dbExcel.OpenRecordset("Plan1$")

    Do While Not rsExcel.EOF
    myRec.AddNew
    myRec.Fields("Coisa1") = rsExcel.Fields("Coisa1")
    myRec.Fields("Coisa2") = rsExcel.Fields("Coisa2")
    myRec.Fields("Coisa3") = rsExcel.Fields("Coisa3")
    myRec.Update

    rsExcel.MoveNext
    Loop
    End Sub

    But the ODBC driver reads only the first 16 records and chose for the column 3 (Coisa3) data type String with 255 characters max. And in the worksheet that I have texts with over 255 characters that are not being imported. I can choose the data type of the third column as memo? How?

    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Strings of more than 255 are problematic. Is this a one-time effort or will have to be done regularly? Did you try the import wizard? The wizard has a memo field type designation.

    This code seemed to help another poster with same issue.
    http://www.accessmvp.com/kdsnell/EXC...m#WriteFileRst
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    AlamBique is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2012
    Posts
    2

    I can't

    I can't use wizard. I use the VBA becawse this is a distributed system, operated by several people who do not understand anything dde data bank. They just have to push a button and go.
    O que eu preciso é de uma maneira de definir o campo por VBA como é feito no wizard.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Opening a recordset of Excel range may be like setting a link. I read one article that stated linking truncates fields greater than 255 characters. The recommended solution was to import to a table then the field would not be truncated. Another article stated that the import wizard or DoCmd.TransferSpreadsheet reads first 25 records to determine field type.

    Do this. Use wizard or DoCmd to test full import to a new table. Does the data still truncate? If not, then test APPEND sql of these records to the target table. If this gets data in correctly, solution might be import then append and delete the import table.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I found this site that has lots of info on Access/Excel.


    Ken's Examples for Importing from EXCEL Workbook Files

    http://www.accessmvp.com/KDSnell/EXCEL_Import.htm

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Nice link. At the very end of page, find:


    The truncated text string that you see is because Jet (ACCESS) sees only "short text" (text strings no longer than 255 characters) values in the first 8 - 25 rows of data in the EXCEL sheet, even though you have longer text farther down the rows. What ACCESS and Jet are doing is assuming that the "text" data actually are Text data type, not Memo data type. One of these suggestions should fix the problem:

    1) Insert a dummy row of data as the first row, where the dummy row contains a text string longer than 255 characters in the cell in that column -- that should let Jet (ACCESS) treat that column's values as memo and not text.

    2) Create a blank table into which you will import the spreadsheet's data. For the field that will receive the "memo" data, make its data type "Memo". Jet (ACCESS) then will "honor" the field's datatype when it does the import.

    It's possible to force Jet to scan all the rows and not guess the data type based on just the first few rows. See this article for information about the registry key (see TypeGuessRows and MaxScanRows information): http://www.dicks-blog.com/archives/2004/06/03/external-data-mixed-data-types/ [ NOTE: There are some reports by others that this registry key may not work as expected when using Windows XP SP3 or when using ACCESS 2007. ]

    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Importing Data from Excel to Access
    By dkatorza in forum Import/Export Data
    Replies: 3
    Last Post: 11-05-2011, 09:17 AM
  2. Replies: 1
    Last Post: 09-07-2011, 01:56 PM
  3. Replies: 2
    Last Post: 04-22-2010, 10:40 AM
  4. iMPORTING DATA FROM EXCEL TO ACCESS
    By Rameez in forum Import/Export Data
    Replies: 7
    Last Post: 06-16-2009, 12:05 PM
  5. Type error when importing Excel spreadsheet
    By jlmnjem in forum Database Design
    Replies: 2
    Last Post: 08-27-2006, 07:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums