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
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
解决方案 »
- 关于跟踪读写内存问题(汇编)续帖NO1
- 怎样给Toolbar1_ButtonClick(ByVal Button As mscomctllib.Button)传参数
- 大家救命啊,像CSDN论坛上的导航条是怎么做的?急用
- 关于VB与数据库的问题!
- 谁有installshield6.22 的安装密码?
- 关于listview控件中itmX.SubItems(i) = rs.Fields.Item(i)的问题
- 为什么经过vb字符串的space()设定大小,赋值之后,再用trim()取消其前后空格,取其长度要比实际一?
- 关于BOM表!高手请进
- 请问我写的程序里用到了WINSOCK控件,但到其他的机器上运行时没有注册WINSOCK控件就不能运行,怎么办
- 求大神帮忙excelvba方面的
- 程序如下: 可是运行后ReCenter_Array根本没有传给Arr,哪儿出了毛病?
- 帮我看看这段代码(help)
如下代码显示如何通过 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.用OCBC或ADO执行SQL语句创建SQL SERVER数据库,首先要建立一个到MASTER数 据库的连接。
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
正在研究。
ADO具体怎么做讲思路就行:.(,,,,,.,,
ADOX是什么?
-、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调用。
谢谢 popocy(海啸) 、dbcontrols(泰山__抛砖引玉)
在最后我会给你们分的,一定
首先要引用: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
我在 EXCEL2000的宏中
用ADO扩展建了ACCESS数据库及数据表后,对数据表进行添加或更新时,出 现 "提供者不支持应用程序要求的操作"的错误. 请问这是什么原因?谢谢.