473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

changing field data types programmatically

Hi i'm trying to import an Excel Spreadsheet into Access2000 but the data
types for two of the fields in my imported table are being identified
incorrectly. Is there a way of using some VB code in Access to change the
data types for the fields from text to a number field or an Integer field.
I'd be grateful for any advice you could give

Many thanks

David
Nov 13 '05 #1
6 6323
"David Gartrell" <da***@davsarg.wanadoo.co.uk> wrote in message
news:d7**********@newsg3.svr.pol.co.uk...
Hi i'm trying to import an Excel Spreadsheet into Access2000 but the data
types for two of the fields in my imported table are being identified
incorrectly. Is there a way of using some VB code in Access to change the
data types for the fields from text to a number field or an Integer field.
I'd be grateful for any advice you could give

Many thanks

David

The solution which gives the most flexibilty is to use Excel automation,
since you can go through the entire contents cell by cell and do any
conversion / error handling on the way. The amount of effort you wish to
spend on this probably depends on whether this is a one-off import or will
be done repeatedly by average pc users (non-nerds).
Nov 13 '05 #2
Unfortunately the columns I want to apply the specific formatting to
will be empty (apart from the field name) at the point that they're
imported into Access. I've already tried using Excel macros to specify
that data type for the fields, but that is lost when I import it into
Access.

I think that the solution for me in this instance lies within Access
but but don't know exactly what to use. I do have some code in another
database that programmatically changes the field name in a table and
I've tried using this as my starting point and have tried a few ideas
but so far without success.

Nov 13 '05 #3
<da***@rightsort.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Unfortunately the columns I want to apply the specific formatting to
will be empty (apart from the field name) at the point that they're
imported into Access. I've already tried using Excel macros to specify
that data type for the fields, but that is lost when I import it into
Access.

I think that the solution for me in this instance lies within Access
but but don't know exactly what to use. I do have some code in another
database that programmatically changes the field name in a table and
I've tried using this as my starting point and have tried a few ideas
but so far without success.

So if the Excel sheet contains no data only the field names, are you
basically trying to create a table structure based on this? This is easy
enough to do but I don't know how you can tell what data type to set based
only on the field name. Perhaps you already know the number of fields, the
data types and just need the names.
Anyway, whatever you need to do, it can certainly be done from Access but I
guess there is a bigger picture to this whole process.
Nov 13 '05 #4
I already know what type of data these fields will be populated with,
for example one field will be populated with a date but this will only
be done via certain processes once the table has been imported into
access.

Basically all I want to be able to do is to use VB to set the data type
for certain fields rather than doing it through the design view of the
table. If anyone knows what the code is for this then please could
they let me know.

Thanks

David.

Nov 13 '05 #5
<da***@rightsort.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I already know what type of data these fields will be populated with,
for example one field will be populated with a date but this will only
be done via certain processes once the table has been imported into
access.

Basically all I want to be able to do is to use VB to set the data type
for certain fields rather than doing it through the design view of the
table. If anyone knows what the code is for this then please could
they let me know.

Thanks

David.

You can use data definition queries, for example:
CurrentDb.Execute "ALTER TABLE MyTable ALTER COLUMN MyColumn INTEGER"
To alter existing columns, but you can do more things with the table if you
use DAO. You cannot convert existing columns but as there is no data, you
might as well re-create a blank table with the required types. In the
function below, the field names are taken from the spreadsheet column
headings. The type is set in a fixed manner - ie column 1 is a long
integer, column 2 is text, etc. You call the function like this:
?CreateTable("C:\MyStuff\MyBook.xls","Sheet1","tbl MyTable")

I did send an e-mail offering to send an example mdb - did it not get
through?
Public Function CreateTable(strExcelPath As String, _
strExcelSheet As String, _
strAccessTable As String) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim tdfLink As DAO.TableDef
Dim tdfLocal As DAO.TableDef
Dim fldLink As DAO.Field
Dim fldLocal As DAO.Field
Dim strLinkTable As String
Dim strFieldName As String
Dim lngCount As Long

strLinkTable = "~tmp" & Format(Now(), "yyyymmddhhnnss")

Set dbs = CurrentDb

Set tdfLink = dbs.CreateTableDef(strLinkTable)

tdfLink.Connect = "Excel 5.0;HDR=YES;IMEX=2;DATABASE=" & strExcelPath

tdfLink.SourceTableName = strExcelSheet & "$"

dbs.TableDefs.Append tdfLink

dbs.TableDefs.Refresh

Set tdfLocal = dbs.CreateTableDef(strAccessTable)

For Each fldLink In tdfLink.Fields

strFieldName = fldLink.Name

lngCount = lngCount + 1

Select Case lngCount

Case 1
Set fldLocal = tdfLocal.CreateField(strFieldName, dbLong)
tdfLocal.Fields.Append fldLocal

Case 2
Set fldLocal = tdfLocal.CreateField(strFieldName, dbText, 50)
tdfLocal.Fields.Append fldLocal

Case 3
Set fldLocal = tdfLocal.CreateField(strFieldName, dbDate)
tdfLocal.Fields.Append fldLocal

Case 4
Set fldLocal = tdfLocal.CreateField(strFieldName, dbCurrency)
tdfLocal.Fields.Append fldLocal

End Select

Next fldLink

dbs.TableDefs.Append tdfLocal

CreateTable = True

Exit_Handler:

On Error Resume Next

dbs.TableDefs.Delete strLinkTable

Set fldLink = Nothing
Set tdfLink = Nothing
Set fldLocal = Nothing
Set tdfLocal = Nothing
Set dbs = Nothing

Application.RefreshDatabaseWindow

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function


Nov 13 '05 #6
Bri

David Gartrell wrote:
Hi i'm trying to import an Excel Spreadsheet into Access2000 but the data
types for two of the fields in my imported table are being identified
incorrectly. Is there a way of using some VB code in Access to change the
data types for the fields from text to a number field or an Integer field.
I'd be grateful for any advice you could give

Many thanks

David


I find the easiest way to do this is to create the table first with the
fields difined how I want them and then Import into that table. So,
existing table, vs new table option in the Wizard. With
TransferSpreadsheet just use the existing Table name and it will import
in. If there are any records that can't be imported (eg. you define the
field as Integer and somehow a record has text in it) then an Import
Errors table is created with the record number and reason for failure in it.

--
Bri

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: amber | last post by:
Hello, I have a report in VB.NET/Crystal Reports. I have a criteria form that users select between 2 different types of grouping (group by category or group by year). Can I programmatically...
13
by: Peter | last post by:
Can anyone tell me how to change the data type of a field in a table created with a make table query? The field is a binary and must be changed to text. alternately does anyone know how to specify...
0
by: me here | last post by:
I have a VBA subroutine that links an MS Excel spreadsheet and copies the data into a local table. This process is controlled by a form that allows users to select the spreadsheet from the file...
2
by: Jozef | last post by:
Hello, Is there a way to change table permissions in VB Code? I can't seem to find much that's concise in the help file. Here's the situation; I have a table in the "data" portion of a split...
14
by: Paul | last post by:
I want to set the page title and/or a form hidden field programatically through ASP.Net. I do not want to use something like... <% sTitle ="My Title" %> <html><title><%=sTitle%></title>..... ...
32
by: deko | last post by:
I have a popup form with a textbox that is bound to a memo field. I've been warned about memo fields so I'm wondering if I should use this code. Is there any risk with changing the form's...
16
by: Mark | last post by:
Hello. I am attempting to use AppendChunk() to write binary data to a memo field in Access 2000. My initial call to AppendChunk() results in a data type conversion error. Any suggestions? Here...
2
by: tfsimes | last post by:
Hi, I am a long time ASP developer learning .NET, so please bear with me. I am trying to find an article or such that will help me understand how to change control properties at runtime based on...
0
by: Mark | last post by:
When I import a file using the Import Spreadsheet Wizard and I get to the Field Options screen where it allows me to choose the fields and the Indexed option, my Data Type option is grayed out....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.