水平有限,给你一个代码。不过好像有些复杂,但是能够在已有的数据库中动态创建表。数据库是Access的。
Dim cat As New ADOXCatalog '动态创建盘点表用
Dim pstr As String
Dim tb As New Table
pstr = "Provider=MicrosoftJetOLEDB40;Data Source=" & AppPath & "\Datamdb" '不能把这里的40改为351
catActiveConnection = pstr
tbName = "店库存盘点明细" '表的名称
tbColumnsAppend "产品编号", adVarWChar, 50 '以下是表的字段
tbColumnsAppend "类别", adVarWChar, 50
tbColumnsAppend "年份", adVarWChar, 50
tbColumnsAppend "季节", adVarWChar, 50
tbColumnsAppend "入库日期", adDate
tbColumnsAppend "入库件数", adInteger
tbColumnsAppend "客退件数", adInteger
tbColumnsAppend "在途件数", adInteger
tbColumnsAppend "FROM", adVarWChar, 50
tbColumnsAppend "销售/出库时间", adDate
tbColumnsAppend "销售件数", adInteger
tbColumnsAppend "调出件数", adInteger
tbColumnsAppend "次品退仓件数", adInteger
tbColumnsAppend "TO", adVarWChar, 50
tbColumnsAppend "节余数量", adInteger
tbColumnsAppend "外借", adInteger
tbColumnsAppend "维修", adInteger
tbColumnsAppend "备注", adVarWChar, 50
'==============以下设置字段允许为空==============
tbColumns("入库日期")Attributes = adColNullable
tbColumns("入库件数")Attributes = adColNullable
tbColumns("客退件数")Attributes = adColNullable
tbColumns("在途件数")Attributes = adColNullable
tbColumns("FROM")Attributes = adColNullable
tbColumns("销售/出库时间")Attributes = adColNullable
tbColumns("销售件数")Attributes = adColNullable
tbColumns("调出件数")Attributes = adColNullable
tbColumns("次品退仓件数")Attributes = adColNullable
tbColumns("TO")Attributes = adColNullable
tbColumns("节余数量")Attributes = adColNullable
tbColumns("外借")Attributes = adColNullable
tbColumns("维修")Attributes = adColNullable
tbColumns("备注")Attributes = adColNullable
catTablesAppend tb '建立数据表
MsgBox "盘点明细表创建完成!", vbInformation, "创建表"
Set tb = Nothing
Set cat = Nothing
'引用对象库“Microsoft ActiveX Data Objects 28 Library”
'引用对象库“Microsoft ADO Ext 28 For DDL Security”,简称为ADOX
path1 = Dir(AppPath & "\datmdb") '检测文件是否存在
If path1 = "" Then
Text1Text = "数据库datmdb不存在,请先建立数据库~!"
Exit Sub
End If
Dim cat As New ADOXCatalog '不用cat用另外一个名字也可以
Dim pstr As String
Dim tb1 As New Table
pstr = "Provider=MicrosoftJetOLEDB40;" '不能把这里的40改为351
pstr = pstr & "Data Source=" & AppPath & "\datmdb"
pstr = pstr & ";Jet OLEDB:Database Password=123"
catActiveConnection = pstr
tb1Name = "学生信息" '表的名称
tb1ColumnsAppend "姓名", adVarWChar, 20 '表的第一个字段
tb1ColumnsAppend "年龄", adInteger '表的第二个字段
tb1ColumnsAppend "性别", adVarWChar, 2 '表的第三个字段
tb1ColumnsAppend "出生年月", adDate '表的第四个字段
catTablesAppend tb1 '建立数据表
连接数据库的方法简单说下(ACCESS)
Dim cn As New ADODBConnection
If Right(AppPath, 1) = "\" Then
Path = AppPath
Else
Path = AppPath & "\"
End If
cnStr = "Provider=MicrosoftJetOLEDB40;Data Source=" & Path & "数据库mdb;Persist Security Info=False;Jet OLEDB:Database Password=数据库密码"
cnConnectionString = cnStr
cnOpen
连接好数据库后
建立多个数据集实现对多个表的 *** 作
dim rs1 As New ADODBRecordset
rs1Open "select from 表名1" , cn, adOpenKeyset, adLockOptimistic
'如将TEXT1的值给“表名1”的第一个字段
rs1(0)=text1'在数据库的字段中,是从“0”开始编号的
'下面是将TEXT2的值给“表2”的第一个字段
dim rs2 As New ADODBRecordset
rs2Open "select from 表名2" , cn, adOpenKeyset, adLockOptimistic
rs2(0)=text2
'最后别忘了更新数据库和关闭数据集
rs1update
rs2update
rs1close
rs2close
以上就是关于vb中新建数据库表的问题全部的内容,包括:vb中新建数据库表的问题、VB如何在已有的Access数据库中创建数据表、vb中如何在同一数据库里多表添加字段等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)