Make a Database quickly and easily using the ADOX class below.
Note: the default engine type (type 5) creates an Access 2000 compatable database.
Example use: Creates a Customer DB with an Address Table in the working directory.
(To create the Database in a different folder, add an absolute path to the dbName property)
Dim myDataBase
Set myDataBase = New adox
myDataBase.engine = 4
myDataBase.dbName = "Customers"
myDataBase.addNumericColumn "customerID"
myDataBase.tableName = "Address"
myDataBase.addTextColumn "number",8
myDataBase.addTextColumn "street",32
myDataBase.addTextColumn "city",16
myDataBase.addTextColumn "state",2
myDataBase.addTextColumn "zip",9
myDataBase.primaryKey = "customerID"
myDataBase.create
Set myDataBase = NOTHING
The ADOX Class...
[START...]
Class adox
Private adInteger
Private adVarWChar
Private adLongVarWChar
Private m_oDB
Private m_name
Private m_ready
Private m_tableName
Private m_PK
Private i_Key
Private m_recordset
Private m_engine
Public Property Let engine(iType)
'Engine Types:3 = pre 97, 4 = 97, 5 = 2000
If (iType =3) Or (iType =4) Then
m_engine = iType
Else
m_engine = 5
End If
End Property
Public Property Get engine
engine = m_engine
End Property
Private Property Let columns(icolumn)
m_columns = icolumn
End Property
Public Property Get columns()
columns = m_columns
End Property
Public Property Let primaryKey(sname)
m_PK = sname
End Property
Public Property Get primaryKey()
primaryKey = m_PK
End Property
Public Property Let dbName(sname)
m_name = sname
End Property
Public Property Get dbName()
dbName = m_name
End Property
Public Property Let tableName(sname)
m_tableName = sname
End Property
Public Property Get tableName()
name = m_tableName
End Property
Public Property Let ready(sready)
m_ready = sready
End Property
Public Property Get ready()
ready = m_ready
End Property
Public Sub addNumericColumn(sName)
m_recordset = m_recordset & "adInteger" & vbtab & "0" & vbtab & sName & vbcrlf
End Sub
Public Sub addMemoColumn(sName)
m_recordset = m_recordset & "adLongVarWChar" & vbtab & "0" & vbtab & sName & vbcrlf
End Sub
Public Sub addTextColumn(sName,iLength)
If iLength = "" Then
iLength = 60
End If
m_recordset = m_recordset & "adVarWChar" & vbtab & cstr(iLength) & vbtab & sName & vbcrlf
End Sub
Public Sub create()
Dim sCreateString,s,oT,v
sCreateString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbName & ".mdb" & ";Jet OLEDB:Engine Type=" & cstr(m_engine) & ";"
m_oDB.create sCreateString
set oT = createObject("ADOX.Table")
oT.name = m_tableName
v = Split(m_recordset,vbcrlf)
For i = 0 To UBound(v)
If v(i) <> "" Then
w = Split(v(i),vbtab)
If w(0) = "adVarWChar" Then
oT.columns.Append CStr(w(2)), CInt(adVarWChar), CInt(w(1))
ElseIf w(0) ="adLongVarWChar" Then
oT.Columns.Append CStr(w(2)), CInt(adLongVarWChar)
Else
oT.Columns.Append CStr(w(2)), CInt(adInteger)
End IF
End If
Next
s = primaryKey
If s <>"" Then
s = "PK_" & s
oT.Keys.Append s, 1, primaryKey
End If
m_oDB.Tables.Append oT
set m_oT = Nothing
msgbox "db has been created"
End Sub
Private Sub Class_Initialize
set m_oDB = createObject("ADOX.Catalog")
adInteger = 3
adVarWChar = 202
adLongVarWChar = 203
m_tableName = "Table1"
m_engine = 5
End Sub
Private Sub Class_Terminate
set m_oDB = Nothing
End Sub
End Class
[...END]
- Copyright 2003 Xpounded -
All JS code developed by Xpounded is developed in RHSCoder.
All VBS code developed by Xpounded is developed in VBS QuickDev IDE, or RHSCoder. Privacy Statement Feedback