登录说明 | 升级会员 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 行业资讯

.mdb.access数据库VBA-利用SQL语句创建数据库和数据表

时间:2025-01-20 08:51:19  来源:  作者:

利用SQL语句创建数据库和数据表

首先引用:microsoft activeX data objects 2.X library 和 microsoft ado ext.2.x for ddl and security”yPS检测VBA
代码:yPS检测VBA
public sub1_3()yPS检测VBA
dim mycat as new adox.catalog  ‘定义ADOX的Catalog对象变量yPS检测VBA
Dim mycmd as new adodb.command  ‘定义Command对象变量yPS检测VBA
dim mydata as string  ‘定义数据库名称变量yPS检测VBA
dim mytable as string  ‘定义数据表名称变量yPS检测VBA
dim SQL as stringyPS检测VBA
 yPS检测VBA
‘设置要创建的数据库名称(包括完整路径)yPS检测VBA
mydata=thisworkbook.path & “\学生成绩管理.mdb”yPS检测VBA
 yPS检测VBA
‘设置要创建的数据表名称yPS检测VBA
mytable=”期末成绩”yPS检测VBA
 yPS检测VBA
‘删除已经存在的数据库文件yPS检测VBA
on error resume nextyPS检测VBA
kill mydatayPS检测VBA
on error goto 0yPS检测VBA
 yPS检测VBA
‘创建数据库文件yPS检测VBA
mycat.create “provider=microsoft.jet.oledb.4.0;Data source=” & mydatayPS检测VBA
 yPS检测VBA
‘设置数据库连接yPS检测VBA
set mycmd.activeconnection=mycat.activeconnectionyPS检测VBA
 yPS检测VBA
‘设置创建数据表的SQL语句yPS检测VBA
 SQL = "CREATE TABLE " & myTable _yPS检测VBA
        & "(学号 text(10),姓名 text(6),性别 text(1),班级 text(10)," _yPS检测VBA
        & "数学 Single,语文 Single,物理 Single,化学 Single," _yPS检测VBA
        & "英语 Single,总分 Single)"yPS检测VBA
‘利用execute方法创建数据表yPS检测VBA
with mycmdyPS检测VBA
   .commandtext=sqlyPS检测VBA
   .execute, , adcmdtextyPS检测VBA
end withyPS检测VBA
 yPS检测VBA
‘释放变量yPS检测VBA
set mycat=nothingyPS检测VBA
set mycmd=nothingyPS检测VBA
‘弹出信息yPS检测VBA
 msgbox “创建数据库成功!” & vbcrlf & “数据库文件名为:” & mydata & vbcrlf & “数据表名称为:” & mytable & vbcrlf & “保存位置:” & thisworkbook.path,vbinformation,”创建数据库”yPS检测VBA
end subyPS检测VBA
 

注:有两种方法来创建数据表:

   ·利用ADODB.Command对象的commandtext属性和execute方法:yPS检测VBA
dim mycmd as new adodb.commandyPS检测VBA
set mycmd.activeconnection=mycat.activeconnectionyPS检测VBA
with mycmdyPS检测VBA
  .commandtext=SQLyPS检测VBA
  .execute, , adcmdtextyPS检测VBA
end withyPS检测VBA
   ·利用ADODB.Connection对象的execute方法来生成几个记录集yPS检测VBA
Dim cnn as new adodb.connectionyPS检测VBA
dim rs as new adodb.recordsetyPS检测VBA
set cnn=mycat.activeconnectionyPS检测VBA
set rs=cnn.execute(sql)yPS检测VBA
 
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
合肥住建口检测单位招聘,高级工程师+协会九大专项证书,人员全职兼职均可
合肥住建口检测单位招
关于印发安徽省建设工程专业技术资格评审标准条件的通知(图文)
关于印发安徽省建设工
一般溶液有效期一览表
一般溶液有效期一览表
2022年一建《机电》真题解析
2022年一建《机电》真
相关文章
    无相关信息
栏目更新
栏目热门