VB通过代码对Access数据库的操作

前端之家收集整理的这篇文章主要介绍了VB通过代码对Access数据库的操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转csdn帖子中faysky2、lihonggen0的答案,不过自己还未测试,先记录下来
用ADOX:


'菜单“工程”--> "引用 "--> Microsoft   ADO   Ext.2.7   for   DDL   ado   Security 
Private   Sub   Form_Load() 

        Dim   cat   As   ADOX.Catalog 
        Set   cat   =   New   ADOX.Catalog 
        '在当前目录下建立名为newdata的Access数据库 
        cat.Create   ( "Provider=Microsoft.Jet.OLEDB.4.0;Data   Source= "   +   App.Path   &   "\newdata.mdb "   +   "; ") 
        MsgBox   "数据库已经创建成功! " 
End   Sub


可以用ADO来建表和删表:
'菜单“工程”--> "引用 "--> Microsoft ADO Ext.2.7 for DDL ado Security

'建表: 
Private   Sub   Command1_Click() 
        Dim   cn   As   New   ADODB.Connection 
        cn.ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data   Source= "   &   App.Path   &   "newdata.mdb;Persist   Security   Info=False " 
        cn.Open 
        cn.Execute   "CREATE   TABLE   [aaa]([学生姓名]Text(20),[年龄]Integer,[成绩]Double) " 
        cn.Close 
End   Sub 
'删表: 
Private   Sub   Command2_Click() 
        Dim   cn   As   New   ADODB.Connection 
        cn.ConnectionString= "Provider=Microsoft.Jet.OLEDB.4.0;Data   Source== "   &   App.Path   &   "newdata.mdb;Persist   Security   Info=False " 
        cn.Open 
        cn.Execute   "DROP   TABLE   [aaa] " 
        cn.Close 
End   Sub

'********************************************************* 
'*   名称:TableExists 
'*   功能:判断表是否存在(表名) 
'*   用法:TableExists(表名)   adoCN是一个sql的连接 
'********************************************************* 
Public   Function   TableExists(findTable   As   String)   As   Boolean 
        Dim   rstSchema   As   New   ADODB.Recordset 
        Set   rstSchema   =   adoCN.OpenSchema(adSchemaTables) 
        rstSchema.Find   "TABLE_NAME= ' "   &   findTable   &   " ' " 
        If   rstSchema.EOF   Then 
            TableExists   =   False 
        Else 
            TableExists   =   True 
        End   If 
        rstSchema.Close 
End   Function
原文链接:https://www.f2er.com/vb/260598.html

猜你在找的VB相关文章