Code ExampleStart Visual Basic 4.0. If it is already running, from the File menu, click New Project. Add a command button, data control, and a DBGrid control on Form1. Set the DataSource property of the DBGrid control to the Data control. Copy the following code sample to the Form1 code window: 
      Option Explicit
      Dim rs1 As Recordset
      Dim rs2 As Recordset
      Dim db As Database
      Dim td As TableDef
      Dim fl As Field      Private Sub Command1_Click()
         Dim iFields As Integer, iRecords As Integer         ' Create the database.
         Set db = CreateDatabase("C:\test.mdb", dbLangGeneral)
         Set td = db.CreateTableDef("Table1")         'Now that the database is created, add fields to the database
         For iFields = 1 To 5 'The last number can be changed.
            Set fl = td.CreateField("Field " & CStr(iFields), dbInteger)
            td.Fields.Append fl
         Next iFields         db.TableDefs.Append td         ' Now that the database has fields, add records through a
         ' recordset.
         Set rs1 = db.OpenRecordset("Table1", dbOpenTable)
         For iRecords = 1 To 10  ' For each row
            rs1.AddNew           ' add a new record.            For iFields = 1 To 5      ' For each field in the record
               rs1("Field " & CStr(iFields)) = iFields  ' add a number.
            Next iFields            rs1.Update
         Next iRecords         ' Close both the recordset and database.
         rs1.Close
         db.Close         ' Populate the DBGrid control with the contents of the Recordset.
         Set db = OpenDatabase("C:\test.mdb")
         Set rs1 = db.OpenRecordset("Select * from Table1")
         Set Data1.Recordset = rs1         Command1.Visible = False
      End Sub      Private Sub Form_Load()
         If Dir("C:\test.mdb") = "" Then
            Command1.Caption = "Create Database"
            Command1.Visible = True
         End If      End Sub

解决方案 »

  1.   

    创建数据库范例
    如下代码显示如何通过 Create 方法创建新的 Jet 数据库。Sub CreateDatabase()    Dim cat As New ADOX.Catalog
        cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\new.mdb"End Sub
      

  2.   

    1.用DAO执行SQL语句创建ACCESS数据库。
    2.用OCBC或ADO执行SQL语句创建SQL SERVER数据库,首先要建立一个到MASTER数 据库的连接。
      

  3.   

    写拉一个创建Access97数据库的函数,你试一试!
    Private Function CreateAccessDB(DBName As String, CoverFlag As Boolean) As Boolean
    Dim wrkDefault As Workspace '需要引用Micrsoft DAO3.15 Object Library (Dao350.dll)
    Dim dbsNew As Database
    Dim prpLoop As Property
    On Error GoTo Err_Fun
        ' Get default Workspace.
        Set wrkDefault = DBEngine.Workspaces(0)    ' Make sure there isn't already a file with the name of
        ' the new database.
        If Dir(DBName) <> "" Then
            If CoverFlag Then
                Kill DBName
            Else
                CreateAccessDB = True
                Exit Function
            End If
        End If
        ' Create a new encrypted database with the specified
        ' collating order.
        Set dbsNew = wrkDefault.CreateDatabase(DBName, dbLangGeneral, dbEncrypt)
        CreateAccessDB = True
    Exit Function
    Err_Fun:
        CreateAccessDB = False
        Set wrkDefault = Nothing
    End Function
      

  4.   

    以上是DAO和ADOX创建的ACCESS数据库,SQL-SERVER的需要安装SQL客户端,引用SQL-DMO
      

  5.   

    我正在研究,我只能用ADO,DAO我从来没有用过。
    正在研究。
      

  6.   

    朋友们:
        ADO具体怎么做讲思路就行:.(,,,,,.,,
      

  7.   

    Dim  cat  As  New  ADOX.Catalog
    ADOX是什么?
      

  8.   

    这些代码我测试过了:
    -、ACCESS:
    Sub CreateDatabaseX()   Dim wrkDefault As Workspace
       Dim dbsNew As DATABASE
       Dim prpLoop As Property   ' Get default Workspace.
       Set wrkDefault = DBEngine.Workspaces(0)   ' Make sure there isn't already a file with the name of 
       ' the new database.
       If Dir("NewDB.mdb") <> "" Then Kill "NewDB.mdb"   ' Create a new encrypted database with the specified 
       ' collating order.
       Set dbsNew = wrkDefault.CreateDatabase("NewDB.mdb", _
          dbLangGeneral, dbEncrypt)   With dbsNew
          Debug.Print "Properties of " & .Name
          ' Enumerate the Properties collection of the new 
          ' Database object.
          For Each prpLoop In .Properties
             If prpLoop <> "" Then Debug.Print "  " & _
                prpLoop.Name & " = " & prpLoop
          Next prpLoop
       End With   dbsNew.CloseEnd Sub
    二、SQL SERVER
    本例创建名为 Products 的数据库,并指定单个文件。指定的文件成为主文件,并会自动创建一个 1 MB 的事务日志文件。因为主文件的 SIZE 参数中没有指定 MB 或 KB,所以主文件将以兆字节为单位进行分配。USE master
    GO
    CREATE DATABASE Products
    ON 
    ( NAME = prods_dat,
       FILENAME = 'c:\program files\microsoft sql server\mssql\data\prods.mdf',
       SIZE = 4,
       MAXSIZE = 10,
       FILEGROWTH = 1 )
    GO
     再用ADO调用。
      

  9.   

    我想直接用ADO创建ACCESS数据库,因为我不会用DAO。另外我不想在一个数据库的基础去再创建数据库
    谢谢 popocy(海啸) 、dbcontrols(泰山__抛砖引玉) 
    在最后我会给你们分的,一定
      

  10.   

    用ADO来扩展建数据库
    首先要引用:Microsoft ADO Ext.27 for DLL and securiy 
    Dim strDB As New ADOX.Catalog
    Dim strTab01 As New ADOX.Table
    dim DBPATH_Name as string 
    DBPath_Name = App.Path & "\" & Num_Dig_J & ".mdb"
    strDB.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath_Name
    strTab01.Name = "yh" '表名
    strTab01.Columns.Append "YHXM", adVarWChar, 14 '字段名
    strTab01.Columns.Append "YHDH", adVarWChar, 14 '同上
    strDB.Tables.Append strTab01
    Mircosoft.jet.oledb.4.0代表office 2000
    Mircosoft.jet.oledb.3.51代表office 97 
      

  11.   

    sonicdater 兄台,
      我在 EXCEL2000的宏中
      用ADO扩展建了ACCESS数据库及数据表后,对数据表进行添加或更新时,出 现 "提供者不支持应用程序要求的操作"的错误. 请问这是什么原因?谢谢.