|
2#
楼主 |
发表于 2002-11-25 17:52:00
|
只看该作者
http://ourworld.compuserve.com/homepages/attac-cg/AtblTip.htm#FORMAT
Sub: Setting A Field's Format Via Code Either During Table Creation or Afterwards
Detail: When you are creating a table using code you may want to set a field's format or number of decimal places.
Alternately if you run a make table query using already formated fields as an input, you will find that the new table does not carry over the formatting of your input fields. Therefore in each situation, you need to set the format for the field.
The format and decimal places properties of a field do not exist until they are created, so if you query a field's "format" property before it is created, you'll get an error saying there is no such property. So here's some code which creates a simple table, and then sets the format and decimal places properties for a couple of fields. You can strip out the code for the format section to create a new function for setting the format for a table after running a make table query.
(Note: This code does not work using ADO in Access 2000, in fact there is no similar ability to set decimal places or formatting of fields if you use ADO as the data access method of your db in Access 2000. You must create a temporary reference to or use DAO as the data access method in Access 2000 to support this capability.)
Function acg_CreateTable(strTable As String) As Integer
'-------------------------
'Purpose: Creates A new table and sets field format
'Accepts: strTable, the name of the new table
'Returns: True (-1) on success, False on failure
'-------------------------
On Error GoTo ErrCT
Dim TDB As Database
Dim fld1 As Field, fld2 As Field, fld3 As Field
Dim fFormat2 As Property, fFormat3 As Property
Dim idxTbl As Index
Dim idxFld As Field
Dim Newtbl As TableDef
Dim Newtbl2 As TableDef
acg_CreateTable = True
'First Create the table
Set TDB = CurrentDb()
Set Newtbl = TDB.CreateTableDef(strTable)
Set fld1 = Newtbl.CreateField("MyStringField", dbText, 75)
Newtbl.Fields.Append fld1
Set fld2 = Newtbl.CreateField("MyNumberField", dbDouble)
Newtbl.Fields.Append fld2
Set fld3 = Newtbl.CreateField("MyDateTimeField", dbDate)
Newtbl.Fields.Append fld3
TDB.TableDefs.Append Newtbl
'Create an index for our table. Need to use a new tabledef
'object for the table or it doesn't work
Set Newtbl2 = TDB.TableDefs(strTable)
Set idxTbl = Newtbl2.CreateIndex("rimaryKey")
idxTbl.Primary = -1
idxTbl.Unique = -1
Set idxFld = idxTbl.CreateField("MyStringField")
idxTbl.Fields.Append idxFld
Newtbl2.Indexes.Append idxTbl
'Format the single field to have two decimal places
'and the datetime field to be a medium time.
'Note that decimal places has no space in the name
Set fld2 = Newtbl2.Fields("MyNumberField")
Set fFormat2 = fld2.CreateProperty("Format", dbText, "Fixed")
fld2.Properties.Append fFormat2
Set fFormat2 = fld2.CreateProperty("DecimalPlaces", dbByte, 2)
fld2.Properties.Append fFormat2
Set fld3 = Newtbl2.Fields("MyDateTimeField")
Set fFormat3 = fld3.CreateProperty("Format", dbText, "Medium Time")
fld3.Properties.Append fFormat3
TDB.Close
ExitCT:
Exit Function
ErrCT:
If Err <> 91 Then TDB.Close
acg_CreateTable = False
Resume ExitCT
End Function
|
|