Change Access field type from number to text in VBA.

Posted on 2007-10-04
Last Modified: 2013-11-27
I have imported an excel file into access.  One field is being read as numeric if the user entered only numbers into that column, but in fact in the Access application it is a text field.  When I try to join the imported table field (imported as "number") to the related field ("Text") it of course does not work.  How can I programatically change the field type from number to text after I import the table and before I need to join it to related tables?  Thank you!
Question by:emcullen
    LVL 65

    Accepted Solution

    One of the problems with Excel data imports is that Access has to *ASSUME* the data type of a column based on the first value in that column.  
    If it's numeric, then it assumes the column is numeric.

    >How can I programatically change the field type from number to text after I import the table
    CStr(YourNumberColumn) will return each value as text.

    LVL 65

    Expert Comment

    by:Jim Horn
    Another thought here.. most established developers will import Excel data into Access into a 'temporary' table with all text fields.  (I usually prefix these with timp_)
    That way, all data makes it into Access without any data conversion errors.

    Then, you can run a bunch of SELECT / UPDATE queries to 'scrub' your data, making sure dates are dates, numbers are numbers, etc.
    Then when you're satisfied your data is good, INSERT into your ultimate destination table(s), then delete all from the temp table.

    Hope this helps.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now