| General
General Table Design Suggestions Check out and Use the "Look Up" Feature for Fields When Designing Tables |
| General Table Design Suggestions | |
There are a number of general design issues with tables which you should be mindful of when you are starting a new database design.
| |
| Setting A Field's Format Via Code Either During Table Creation or Afterwards | |
| 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 formatted 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("PrimaryKey")
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
| |
| | Checkout and Use the "Look Up" Feature for Fields When Designing Tables in Access |
| One of the more user friendly features in
Access 97, 2000, Xp and above is the ability to specify a "Look Up" query for fields in tables, to get a value from another table as an option for the target table's field value.
What this capability provides is the ability set a query as a source for a field in a table, so that you can reference look up tables. (e.g. state abbreviations, specific product types etc.) You can also set a limit to list property for the look up field, which is generally recommended, since you can't respond to an event where the user enters a value directly in the table other than what's in the look up list. (You can respond on a form using vba.) Here's some practical advantages and ways to use this new capability:
| |
| Creating SQL Server Tables using ADOX in Access | |
| When you attempt to create a table using
ADOX on a SQL Server Db from within an Access database using the code shown in
the Access Help file, you may receive an error:
Run-time Error: 3251 Access' base connection does not support table creation because Access uses an intermediate OLEDB provider to access the SQL Server. To successfully create a table on a SQL Server from within Access, you must open a new connection to the server and use that connection on the server's catalog as shown below: Function CreateSQLTbl()
'-------------------------
'Purpose: Creates A new table on a SQL Server Northwind Db
'Accepts: strTable, the name of the new table
'Returns: True (-1) on success, False on failure
'-------------------------
On Error GoTo ErrHandler
Dim objCat As ADOX.Catalog
Dim objTbl As ADOX.Table
Dim objCol As ADOX.Column
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
'Note fill in your server name in the connection string below!!!!
conn.ConnectionString = "Provider=SQLOLEDB.1;Security Info=False;" & _
"Data Source=YourServerNameHere;Integrated Security=SSPI;" & _
"Initial Catalog=Northwind"
conn.Open
Set objCat = New ADOX.Catalog
Set objTbl = New ADOX.Table
Set objCat.ActiveConnection = conn
objTbl.Columns.Append "TestField", adInteger
objTbl.Name = "TestTable"
objCat.Tables.Append objTbl
Set objTbl = Nothing
Set objCat = Nothing
conn.Close
Set Conn = Nothing
ExitCT:
Exit Function
ErrHandler:
CreateSQLTable = False
Resume ExitCT
End Function
| |