473,756 Members | 3,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing field data types programmaticall y

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 6395
"David Gartrell" <da***@davsarg. wanadoo.co.uk> wrote in message
news:d7******** **@newsg3.svr.p ol.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 programmaticall y 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***@rightsor t.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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 programmaticall y 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***@rightsor t.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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.Execu te "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\MyBoo k.xls","Sheet1" ,"tblMyTable ")

I did send an e-mail offering to send an example mdb - did it not get
through?
Public Function CreateTable(str ExcelPath 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.CreateTable Def(strLinkTabl e)

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

tdfLink.SourceT ableName = strExcelSheet & "$"

dbs.TableDefs.A ppend tdfLink

dbs.TableDefs.R efresh

Set tdfLocal = dbs.CreateTable Def(strAccessTa ble)

For Each fldLink In tdfLink.Fields

strFieldName = fldLink.Name

lngCount = lngCount + 1

Select Case lngCount

Case 1
Set fldLocal = tdfLocal.Create Field(strFieldN ame, dbLong)
tdfLocal.Fields .Append fldLocal

Case 2
Set fldLocal = tdfLocal.Create Field(strFieldN ame, dbText, 50)
tdfLocal.Fields .Append fldLocal

Case 3
Set fldLocal = tdfLocal.Create Field(strFieldN ame, dbDate)
tdfLocal.Fields .Append fldLocal

Case 4
Set fldLocal = tdfLocal.Create Field(strFieldN ame, dbCurrency)
tdfLocal.Fields .Append fldLocal

End Select

Next fldLink

dbs.TableDefs.A ppend tdfLocal

CreateTable = True

Exit_Handler:

On Error Resume Next

dbs.TableDefs.D elete strLinkTable

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

Application.Ref reshDatabaseWin dow

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
TransferSpreads heet 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
2890
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 change the grouping of a single report, based on the user's selection? Or do I have to create another report, with different grouping, and call it when user selects different grouping? Thanks, amber
13
2915
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 the field type when running a make table query? Thanks, Sven
0
2136
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 system. I actually import from two different named ranges from each workbook so for every import the following subroutine is used twice. The database is used to collate data collected from different surveys entered by remote users into an Excel...
2
4354
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 database. The table is a list of values that shouldn't be accidentally changed by the user, so I've taken the administer and delete permissions off. I'm using the regular workgroup file, with the default Admin user name, so there is no funky...
14
3486
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>..... I want to completely seperate the code from presentation. I would like to do the same thing for a value of a hidden form field but
32
3689
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 RecordSource like this? Am I asking for trouble doing this with a memo field? Thanks in advance. Private Sub cmdNextNote_Click() Dim lngNid As Long If Me.Dirty Then DoCmd.RunCommand acCmdSaveRecord
16
4883
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 is a VBA function which demonstrates the problem. Thanks -Mark
2
1812
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 the value in a field. For instance, if I want to add some text to a field in a GridView based on a boolean value in an un-shown field being returned. Another example would be if I want to bind a Listbox to a table of people, however I want to...
0
1519
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. Shouldn't I be able to change data types at that stage for each field as opposed to waiting until the data is uploaded and changing in the table properties? Can someone tell me how to turn that option on. Save a lot of time. Thanks Mark
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.