新手急求:用Command控件建立一个Access数据库

zhangxiaoli 2006-03-03 03:00:05
我想用一个command控件建立一个access数据库表格。这个控件可以实现:按照要求的路径和要求的文件名,建立一个access数据表格,有37个字段。谁能发一下这个控件的源代码?不胜感激!
Private Sub Command8_Click()
CommonDialog1.ShowSave
CommonDialog1.Filter = "Microsoft Access MDB(*.mdb)"



End Sub
...全文
70 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
bbhere 2006-03-03
Private Sub Command8_Click()
CommonDialog1.ShowSave
CommonDialog1.Filter = "Microsoft Access MDB(*.mdb)"
Dim Cat As ADOX.Catalog
Dim strSql As String
Dim fs As FileSystemObject
Dim strFName As String

strFName = CommonDialog1.FileName
Set fs = New FileSystemObject
If fs.FileExists(strFName) Then fs.DeleteFile (strFName)
Set Cat = New ADOX.Catalog
Cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFName
Set mCn = New Connection
mCn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFName & ";Persist Security Info=False"
'建立内容信息表
strSql = " CREATE TABLE [内容信息]" & _
" ( 标题 varchar(100) not NULL CONSTRAINT U_标题 PRIMARY KEY CLUSTERED ," & _
" 记录数 int NULL, " & _
" 考试类别描述 varchar(100) NULL," & _
" 考试类别 varchar(50) NULL, " & _
" 考试年月 varchar(50) NULL , " & _
" 表名 varchar(50) NULL ," & _
" 机构描述 varchar(100) null, " & _
" 机构 VarChar(50) null ," & _
" 发送日期 DateTime null," & _
" 系统信息 int not null," & _
" Version varchar(20) null)"
mCn.Execute (strSql)
End Sub
回复
使用ADOX动态创建数据库,表,字段.暂时还没有写完整,但此部份己能正常使用。
TCreateDataBase.cls

''''欢迎你下载使用本代码,本份代码由程序太平洋提供下载学习之用
''''声明:
''''1.本站所有代码的版权归原作者所有,如果你使用了在本站下载的源代码
'''' 引起的一切纠纷(后果)与本站无关,请您尊重原作者的劳动成果!
''''2.若本站在代码上有侵权之处请您与站长联系,站长会及时更正。
''''论坛:http://www.5ivb.net
''''Email:dapha@etang.com
''''CopyRight 2001-2005 By dapha.net
''''整理时间:2004-7-31 22:10:30

Option Explicit
Public Enum DataBaseVer
ACCESS97 = 1
ACCESS2000 = 2
End Enum
Private obj_Cat As ADOX.Catalog
Private obj_table As ADOX.Table
Dim obj_col As Column
Private m_DBName As String ''''数据库名称
Private m_DBVer As DataBaseVer ''''数据库版本
Private strConnection As String ''''数据库连接驱动
Public Property Let DataBaseName(ByVal value As String)
m_DBName = value
End Property
Public Property Let SetDataBaseVer(ByVal value As DataBaseVer)
m_DBVer = value
End Property
Public Function CreateDataBase(Optional DataBaseName As String = "", Optional p_DBVer As DataBaseVer, Optional OverWrite As Boolean = False) As Boolean
''''DataBaseName 数据库名称
''''数据库版本 1.ACCESS97 2.ACCESS2000
''''OverWrite 是否重写原有的数据库 False 不需要 True 需要
On Error GoTo errorhand
If p_DBVer > 0 Then m_DBVer = p_DBVer
If Len(Trim(DataBaseName)) > 0 Then m_DBName = DataBaseName
Select Case m_DBVer
Case 1
strConnection = GetDBConnection
Case 2
strConnection = GetDBConnection(True)
Case Else
Err.Raise 30001, "TCreateDataBase", "数据库选择参数未选"
End Select
If Len(Trim(m_DBName)) = 0 Then
Err.Raise 30002, "TCreateDataBase", "文件名错误---空的文件名"
End If
obj_Cat.Create strConnection
CreateDataBase = True
Exit Function
errorhand:
If OverWrite Then
Kill m_DBName
Resume ''''返回错误发生处
Else
Err.Raise 30006, "TCreateDataBase", "数据库己存在"
End If
CreateDataBase = False
End Function
Public Function CrateTable(ByVal TableName As String, Optional OverWrite As Boolean = False)
On Error GoTo errhand:
If obj_Cat Is Nothing Then
Err.Raise 30003, "TCreateDataBase----CreateTable", "对象不存在"
Exit Function
End If
Set obj_table = New Table
obj_table.Name = TableName
obj_Cat.Tables.Append obj_table

Exit Function
errhand:

End Function
Private Function GetDBConnection(Optional ByVal value As Boolean = False) As String
If value Then
GetDBConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & m_DBName & ";"
Else
GetDBConnection = "Provider=Microsoft.Jet.OLEDB.3.51;" & _
"Data Source=" & m_DBName & ";"
End If
End Function
Private Sub Class_Initialize()
Set obj_Cat = New ADOX.Catalog

End Sub
Public Function CreateColumn(ByVal ColName As String, ByVal pType As DataTypeEnum, _
Optional ByVal Size As Integer = 255, Optional ByVal AutoInc As Integer = 0, _
Optional ByVal Nullable As Integer = 0, _
Optional ByVal Defaultvalue As Variant = vbEmpty) As Boolean
''''ColName 列名
''''pType 数据类型
''''Size 类型大小
''''AutoInc 如果是int型,是否设置为自动增长
''''Nullable是否允许为空 0,默认(必填项) 1 允许为空
''''Defaultvalue 默认值 由于时间原因,只能这样应付着了 需要和类型配合否则有可能出错
''''如:.CreateColumn "Age", adInteger, , , , 4 Integer型,默认值 4
'''' .CreateColumn "UserName", adVarWChar, 20, , , "wangfeng" 文本型,默认值 wangfeng
On Error GoTo CreateColumn_Error
Set obj_col = New ADOX.Column
With obj_col
.Name = ColName
.Type = pType
If .Type >= adVarChar Then .DefinedSize = Size
Set .ParentCatalog = obj_Cat
If .Type = adInteger Then .Properties("Autoincrement") = (AutoInc = 1)
.Properties("Nullable") = (Nullable = 1)
If Defaultvalue <> vbEmpty Then
.Properties("Default").value = Defaultvalue
End If
End With
obj_table.Columns.Append obj_col
CreateColumn = True
Exit Function
CreateColumn_Error:
CreateColumn = False
'''' Err.Raise 30004, "TCreateDataBase----CreateColumn", "未知错误"
End Function
Private Sub Class_Terminate()
Set obj_col = Nothing
Set obj_table = Nothing
Set obj_Cat = Nothing
End Sub


使用此类,需要引ADOX对象

示例:
'欢迎你下载使用本代码,本份代码由程序太平洋提供下载学习之用
'声明:
'1.本站所有代码的版权归原作者所有,如果你使用了在本站下载的源代码
' 引起的一切纠纷(后果)与本站无关,请您尊重原作者的劳动成果!
'2.若本站在代码上有侵权之处请您与站长联系,站长会及时更正。
'论坛:http://www.5ivb.net
'Email:dapha@etang.com
'CopyRight 2001-2005 By dapha.net
'整理时间:2004-7-31 22:10:30

Option Explicit
Private Sub Command1_Click()
On Error GoTo errhand
Dim i As Integer, j As Integer
Dim obj_Create As TCreateDataBase
Set obj_Create = New TCreateDataBase
With obj_Create
.DataBaseName = "c:\wf.mdb"
.SetDataBaseVer = ACCESS2000
.CreateDataBase , , True '参数三 是否覆盖
.CrateTable "UserInfo"
.CreateColumn "ID", adInteger, , 1 '自动增长
.CreateColumn "UserName", adVarWChar, 20, , , "wangfeng" '长度为20
.CreateColumn "Age", adInteger, , , , 24
.CreateColumn "Address", adVarWChar, , , 1 '必填 否
For i = 1 To 10
.CrateTable "Test" & CStr(i)
For j = 1 To 10
.CreateColumn "Test" & CStr(j), adInteger
Next
Next
End With
Set obj_Create = Nothing
MsgBox "数据库建立成功!", vbInformation, "提示"
Exit Sub
errhand:
MsgBox Err.Description
End Sub
回复
faysky2 2006-03-03
参考:用ADOX来创建mdb数据库和数据表

'菜单“工程”-->"引用"-->Microsoft ADO Ext.2.7 for DDL ado Security
Private Sub Form_Load()
Dim fPath As String
CommonDialog1.Flags = &H1000
CommonDialog1.Filter = "Microsoft Access MDB(*.mdb)|*.mdb"
CommonDialog1.ShowOpen
fPath = CommonDialog1.FileName
If Trim(fPath) = "" Then Exit Sub
Dim cat As ADOX.Catalog
Set cat = New ADOX.Catalog
cat.Create ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\newdata.mdb;Jet OLEDB:DataBase password=12345")

MsgBox "数据库已经创建成功!"
Dim tbl As New ADOX.Table
tbl.ParentCatalog = cat
tbl.Name = "MyTable"

'增加一个自动增长的字段
Dim col As ADOX.Column
Set col = New ADOX.Column
col.ParentCatalog = cat
col.Type = ADOX.DataTypeEnum.adInteger ' // 必须先设置字段类型
col.Name = "id"
col.Properties("Jet OLEDB:Allow Zero Length").Value = False
col.Properties("AutoIncrement").Value = True
tbl.Columns.Append col, ADOX.DataTypeEnum.adInteger, 0

'增加一个文本字段
Dim col2 As ADOX.Column
Set col2 = New ADOX.Column
col2.ParentCatalog = cat
col2.Name = "Description"
col2.Properties("Jet OLEDB:Allow Zero Length").Value = False
tbl.Columns.Append col2, ADOX.DataTypeEnum.adVarChar, 25

'增加一个货币型字段
Dim col4 As ADOX.Column
Set col4 = New ADOX.Column
col4.ParentCatalog = cat
col4.Type = ADOX.DataTypeEnum.adCurrency
col4.Name = "xx"
tbl.Columns.Append col4, ADOX.DataTypeEnum.adCurrency

'增加一个OLE字段
Dim col5 As ADOX.Column
Set col5 = New ADOX.Column
col5.ParentCatalog = cat
col5.Type = ADOX.DataTypeEnum.adLongVarBinary
col5.Name = "OLD_FLD"
tbl.Columns.Append col5, ADOX.DataTypeEnum.adLongVarBinary

'增加一个数值型字段
Dim col3 As ADOX.Column
Set col3 = New ADOX.Column
col3.ParentCatalog = cat
col3.Type = ADOX.DataTypeEnum.adDouble
col3.Name = "ll"
tbl.Columns.Append col3, ADOX.DataTypeEnum.adDouble
Dim p As ADOX.Property
For Each p In col3.Properties
Debug.Print p.Name & ":" & p.Value & ":" & p.Type & ":" & p.Attributes
Next

'设置主键
tbl.Keys.Append "PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "id", "", ""
cat.Tables.Append tbl
MsgBox "数据库表:" + tbl.Name + "已经创建成功!"
Set tbl = Nothing
Set cat = Nothing
End Sub
回复
相关推荐
发帖

1188

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
帖子事件
创建了帖子
2006-03-03 03:00
社区公告
暂无公告