V
VXH
my sample have 3 form: form1, form2, form 3.
Here is code in form1 to create new database by VBA. It will make a new file
"name.mdb" and new table in it.
----
Private Sub cmdOk_Click()
Dim db As DAO.Database
Dim tdfDsach As DAO.TableDef
Dim rs As DAO.Recordset
Dim NameFile As String
sAppPath = Me.Application.CurrentProject.Path
txtNameFile.SetFocus
NameFile = txtNameFile.Text
Set wrkDefault = DBEngine.Workspaces(0)
If Not (Dir(sAppPath & "\" & "DATA" & "\" & NameFile) <> "") Then
On Error Resume Next
Set db = wrkDefault.CreateDatabase(sAppPath & "\" & "DATA" & "\" & NameFile,
dbLangGeneral)
Else
Set db = OpenDatabase(sAppPath & "\" & "DATA" & "\" & NameFile)
End If
Set tdfDsach = dbKhoa.CreateTableDef("tbDsach")
tdfDsach.Fields.Append tdfDsach.CreateField("Khoa_thi", dbLong)
tdfDsach.Fields.Append tdfDsach.CreateField("Ngay_thi", dbDate)
tdfDsach.Fields.Append tdfDsach.CreateField("So_tt", dbLong)
....
tdfDsach.Fields!So_tt.Attributes = dbAutoIncrField
db.TableDefs.Append tdfDsach
db.Close
DoCmd.Close acForm, "form1"
DoCmd.OpenForm "form2", acNormal, , , , acWindowNormal
Here is code in form1 to create new database by VBA. It will make a new file
"name.mdb" and new table in it.
----
Private Sub cmdOk_Click()
Dim db As DAO.Database
Dim tdfDsach As DAO.TableDef
Dim rs As DAO.Recordset
Dim NameFile As String
sAppPath = Me.Application.CurrentProject.Path
txtNameFile.SetFocus
NameFile = txtNameFile.Text
Set wrkDefault = DBEngine.Workspaces(0)
If Not (Dir(sAppPath & "\" & "DATA" & "\" & NameFile) <> "") Then
On Error Resume Next
Set db = wrkDefault.CreateDatabase(sAppPath & "\" & "DATA" & "\" & NameFile,
dbLangGeneral)
Else
Set db = OpenDatabase(sAppPath & "\" & "DATA" & "\" & NameFile)
End If
Set tdfDsach = dbKhoa.CreateTableDef("tbDsach")
tdfDsach.Fields.Append tdfDsach.CreateField("Khoa_thi", dbLong)
tdfDsach.Fields.Append tdfDsach.CreateField("Ngay_thi", dbDate)
tdfDsach.Fields.Append tdfDsach.CreateField("So_tt", dbLong)
....
tdfDsach.Fields!So_tt.Attributes = dbAutoIncrField
db.TableDefs.Append tdfDsach
db.Close
DoCmd.Close acForm, "form1"
DoCmd.OpenForm "form2", acNormal, , , , acWindowNormal