cyberp
asked on
creating Access tables dynamically
i'm trying to create a Access table dynamically at runtime.
i'm using createtabledef but i want to know how to create a primary key then add 5 records at runtime and fixed the primary key so it cannot be CHANGED, added or deleted.
Futhermore, i want most of the other fields to accept zero length string ? How to declare that in the code ?
i'm displaying the table using DBgrid with data control.
other problem, when i'm running the program. I encoutered this error, "Item not found in this collection."
what does that mean ?
and the debuggere highlights this line below :
Dim db As Database
Set db = DBEngine (0)(0)
please...help ASAP.
thanks a lot
i'm using createtabledef but i want to know how to create a primary key then add 5 records at runtime and fixed the primary key so it cannot be CHANGED, added or deleted.
Futhermore, i want most of the other fields to accept zero length string ? How to declare that in the code ?
i'm displaying the table using DBgrid with data control.
other problem, when i'm running the program. I encoutered this error, "Item not found in this collection."
what does that mean ?
and the debuggere highlights this line below :
Dim db As Database
Set db = DBEngine (0)(0)
please...help ASAP.
thanks a lot
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Cyberp
i have not tried the method u have listed above before. I used the method i gave. It is quick to set up. Try my sample code in a dummy project and just create a dummy database. Then as above u have options to set the field values to null in the create statement.
also go into vb and there is a nice sample there also check the vbonline, unfortunately i do not have my source code with me at the moment.
Hope this helps
Craig
i have not tried the method u have listed above before. I used the method i gave. It is quick to set up. Try my sample code in a dummy project and just create a dummy database. Then as above u have options to set the field values to null in the create statement.
also go into vb and there is a nice sample there also check the vbonline, unfortunately i do not have my source code with me at the moment.
Hope this helps
Craig
ASKER
btw, do u think u can help me with the error i mentioned above.
the funny thing is... i used the same code in 2 forms and i only changed the parameter for the table name. the second one works...while the main one ..doesn;t .
**************************
Set the value of "DBName", then call
"CreateDB" to create the database
Place the following in a Module:
public DBName as string 'Path of database
public MainDB as Database
private MainTD as Table
private MainFD as Field
private MainID as Index
Public Sub CreateDB()
'Create the database using DBName as the database name
Set MainDB = DBEngine.Workspaces(0).Cre
BeginTrans
'Create a table in the database
Set MainTD = MainDB.CreateTableDef("nam
'Create the fields
Call CreateFieldDB("Path", dbText, 255)
'Create an index
Call CreateIndexDB("IdPath","Pa
'Append the table to the database
MainDB.TableDefs.Append MainTD
CommitTrans
End Sub
Private Sub CreateFieldDB(FieldName As String, FieldType As String, FieldLength As Integer)
On Error GoTo ErrHandle
'Create the fields for the Client Information table
Set MainFD = MainTD.CreateField(FieldNa
MainFD.AllowZeroLength = True
MainTD.Fields.Append MainFD
Exit Sub
ErrHandle:
Select Case Err.Number
Case 3219 'AllowZeroLength cannot be True
Resume Next 'Leave as is...AllowZeroLength=False
End Select
End Sub
Private Sub CreateIndexDB(strIndexName
'Create an index
Set MainID = MainTD.CreateIndex(strInde
MainID.Fields.Append MainID.CreateField(strFiel
MainID.Unique = bUnique
MainTD.Indexes.Append MainID
End Sub