Discussion Locked

CREATE TABLE with access

0 helpful
13 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm trying to create a table, and add fields in an existing table, with an input mask and default values.

So, my field should look like this:

NbForfPro1: double, decimal places: 2, input mask 99.999,99
AIGS: Yes/No, default Yes

Thanks in advance ;)

Peter from Belgium.

13 replies

by pgo

Oh, did I mention that I try to do all this from VB6?
And that decimal doesn't actually work, and has to be single?

Didn't think so, so here it is :p

by pgo

hmm, I thought I updated the question...

I'm trying to do this through VB6, but I have no idea how. In MySQL or something similar it's with single(7,2) and default "xxx" or something like that. But that doesn't work in Access...

I've spent quite some time looking for an answer and I'm beginning to think there isn't one...

by pgo

hmm, I thought I updated the question...

I'm trying to do this through VB6, but I have no idea how. In MySQL or something similar it's with single(7,2) and default "xxx" or something like that. But that doesn't work in Access...

I've spent quite some time looking for an answer and I'm beginning to think there isn't one...

by mqpasta

Well, for VB6 chek Format or Format$ function. It is so eassy just need to read some lines.

Hope, you will solve your problem.

Thanks

by pgo

Can't seem to figure out how to take the format of a field in VB6 into my access database.

Anyways, I've wasted some more time looking for a solution, and I saw somewhere that it could be done by using simple, standard sql-code: "create table [table] ( [field] decimal(7,2) default 3,05 );"
Tried it, didn't work. Is there a problem with such an expression in Access '97? (at least, I think it was 97, not 100% sure)

by pgo

Can't seem to figure out how to take the format of a field in VB6 into my access database.

Anyways, I've wasted some more time looking for a solution, and I saw somewhere that it could be done by using simple, standard sql-code: "create table [table] ( [field] decimal(7,2) default 3,05 );"
Tried it, didn't work. Is there a problem with such an expression in Access '97? (at least, I think it was 97, not 100% sure)

by litehouse

I think I have your solution. Here is some code:

----------------

Dim db As DAO.Database
Dim td As New DAO.TableDef
Dim fd As New DAO.Field
Dim po As DAO.Property

Set db = OpenDatabase("c:\usr\access\test1.mdb", True, False)
Set td = db.CreateTableDef("TestTable") 'Create the table definition
Set fd = td.CreateField("TestField1", dbSingle)

fd.DefaultValue = "999.99"
td.Fields.Append fd
Set fd = td.CreateField("TestField2", dbText)
fd.DefaultValue = "9999999"
td.Fields.Append fd
db.TableDefs.Append td
'I found that trying to create the InputMask field before the table has been appended doesn't work
'It seems to only work afterwards.
'Not that InputMask only works with Text and Date fields, NOT Single.
Set po = db.TableDefs("TestTable").Fields("TestField2").CreateProperty("InputMask", dbText, "99.999,99")
db.TableDefs("TestTable").Fields("TestField2").Properties.Append po

by litehouse

Oops! Type in my comments... Meant to say the following:
'Note that InputMask only works with Text and Date fields, NOT Single.

by pgo

For some weird reason, db remains empty in your example...
Tried playing with the database name, tried adding a workspace...

by litehouse

What version of DAO are you referencing? The code I submitted earlier was meant to run with DAO 3.5. Did you change the name of the DB in the OpenDatabase method to be the name of the CURRENT database you want modified? Note that you MUST have the db CLOSED while running this code. The code will open the DB in exclusive mode, this is why you must have the DB closed.

Thanks,
Litehouse

by pgo

DOA 3.6

All connections are closed.

Of course I changed the name :p

by rrv

Hi friend. I have done this few years back. So i dont have code. Solution is simple. In normal Databases we use datatypes like string, integer, single, double etc. But when you are creating a table/fields in Access using VB or any programming language you have to use the "Access" syntax. It is nothing to do with VB. The access syntax is just "Number". In the properties of field mention the decimal places count. That is all. You can find sample code by searching "How to programmatically create table in Access". You have to reference DAO for this.
Good luck

This discussion is closed to new replies.