VBA创建数据库(MS SQL 2000)
Public Sub 创建数据库()
Dim cnn As New ADODB.Connection
Dim cnnStr As String, SQL As String
'创建与SQL Server数据库服务器中指定数据库连接的Connection对象
cnnStr = "Provider=SQLOLEDB.1;" _
& "User ID=sa;" _
& "Data Source=THTFCOMPUTER;" _
& "Initial Catalog=进销存管理"vba数据库编程
cnn.ConnectionString = cnnStr
On Error Resume Next
cnn.Open
On Error GoTo 0
'判断数据库是否存在
If cnn.State = adStateOpen Then
MsgBox "数据库已经存在!", vbInformation, "检查数据库"
cnn.Close
Set cnn = Nothing
Exit Sub
End If
'建立与SQL Server数据库服务器的连接
cnnStr = "Provider=SQLOLEDB.1; User ID=sa; Data Source=THTFCOMPUTER"
cnn.ConnectionString = cnnStr
cnn.Open
'执行SQL语句创建数据库
SQL = "create database 进销存管理"
cnn.Execute SQL
'关闭与SQL Server数据库服务器的连接
cnn.Close
'建立与SQL Server数据库服务器数据库的连接
cnnStr = "Provider=SQLOLEDB.1;" _
& "User ID=sa;" _
& "Data Source=THTFCOMPUTER;" _
& "Initial Catalog=进销存管理"
cnn.ConnectionString = cnnStr
cnn.Open
'创建数据表“供货商信息”
SQL = "create table 供货商信息" _
& "(供货商编码 varchar(10) not null,供货商名称varchar(40) not null," _
& "通讯地址 varchar(30) not null, varchar(6) not null," _
& " varchar(14) not null,传真号码 varchar(14) not null," _
& "联系人 varchar(10) not null,联系人电话 varchar(14) not null," _
& "联系人Email varchar(50) not null,备注
varchar(50))"
cnn.Execute SQL
'创建数据表“商品信息”
SQL = "create table 商品信息" _
& "(商品编码 varchar(10) not null,商品名称
varchar(20) not null," _
& "商品规格 varchar(10) not null,计量单位 varchar(10) not null," _
& "最高库存 int not null,最低库存 int not null,备注varchar(50))"
cnn.Execute SQL
'创建数据表“进货信息”
SQL = "create table 进货信息" _
& "(进货编码 varchar(10) not null,供货商编码
varchar(10) not null," _
& "商品编码 varchar(10) not null,商品名称 varchar(20) not null," _
& "商品规格 varchar(10) not null,计量单位 varchar(10) not null," _
& "进货数量 int not null,进货单价 real not null," _
& "进货日期 datetime not null,备注 varchar(50))"
cnn.Execute SQL
'创建数据表“销售信息”
SQL = "create table 销售信息" _
& "(销售编码 varchar(10) not null,商品编码
varchar(10) not null," _
& "商品名称 varchar(20) not null,商品规格 varchar(10) not null," _
& "计量单位 varchar(10) not null,销售数量 int not null," _
& "销售单价 real not null,销售日期 datetime not null,备注 varchar(50))"
cnn.Execute SQL
'创建数据表“库存信息”
SQL = "create table 库存信息" _
& "(商品编码 varchar(10) not null,商品名称
varchar(20) not null," _
& "商品规格 varchar(10) not null,计量单位 varchar(10) not null," _
& "库存数量 int not null,库存单价 real not null,库存
金额 real not null)"
cnn.Execute SQL
MsgBox "数据库创建成功!", vbInformation, "创建数据库"
cnn.Close
Set cnn = Nothing
End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。