⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 利用代码动态创建数据库并显示
💻 FRM
字号:
VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "MSDATGRD.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7560
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   7560
   StartUpPosition =   3  '窗口缺省
   Begin MSDataGridLib.DataGrid DataGrid1 
      Height          =   2655
      Left            =   0
      TabIndex        =   3
      Top             =   480
      Width           =   7575
      _ExtentX        =   13361
      _ExtentY        =   4683
      _Version        =   393216
      HeadLines       =   1
      RowHeight       =   15
      BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ColumnCount     =   2
      BeginProperty Column00 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column01 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      SplitCount      =   1
      BeginProperty Split0 
         BeginProperty Column00 
         EndProperty
         BeginProperty Column01 
         EndProperty
      EndProperty
   End
   Begin MSComDlg.CommonDialog CommonDialog1 
      Left            =   5760
      Top             =   2520
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.CommandButton Command4 
      Caption         =   "更新"
      Height          =   375
      Left            =   5040
      TabIndex        =   2
      Top             =   0
      Width           =   2535
   End
   Begin VB.CommandButton Command3 
      Caption         =   "查看"
      Height          =   375
      Left            =   2520
      TabIndex        =   1
      Top             =   0
      Width           =   2535
   End
   Begin VB.CommandButton Command1 
      Caption         =   "创建数据库和表"
      Height          =   375
      Left            =   0
      TabIndex        =   0
      Top             =   0
      Width           =   2535
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim cat As New ADOX.Catalog '不用cat用另外一个名字也可以
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim pstr As String '定义该变量是为了后面的书写方便
Private Sub Command1_Click()
Dim fm As String 'fm变量用来获取用户输入的文件名
CommonDialog1.Filter = "MDB文件(*.mdb)|*.mdb|AllFiles(*.*)|*.*|"
CommonDialog1.FilterIndex = 1
CommonDialog1.InitDir = "D:\Jthpaper"
CommonDialog1.Flags = 6
CommonDialog1.Action = 2
If CommonDialog1.FileName = "" Then
MsgBox "你必须输入一个文件名,请重新保存一次!"
Exit Sub
Else
fm = CommonDialog1.FileName
End If
pstr = "Provider=Microsoft.Jet.OLEDB.4.0;" '不能把这里的4.0改为3.51
pstr = pstr & "Data Source=" & fm & ";"
pstr = pstr & "Jet OLEDB:Database Password=NovaLws"
cat.Create pstr '创建数据库
Dim tbl As New Table
cat.ActiveConnection = pstr
tbl.Name = "ComSet" '表的名称
tbl.Columns.Append "编号", adInteger '表的第一个字段
tbl.Columns.Append "姓名", adVarWChar, 8 '表的第二个字段
tbl.Columns.Append "住址", adVarWChar, 50 '表的第三个字段
tbl.Columns.Append "数字", adSingle
cat.Tables.Append tbl '建立数据表
conn.Open pstr
rs.CursorLocation = adUseClient
rs.Open "MyTable", conn, adOpenKeyset, adLockPessimistic
rs.AddNew '往表中添加新记录
rs.Fields(0).Value = 9801
rs.Fields(1).Value = "孙悟空"
rs.Fields(2).Value = "广州市花果山"
rs.Fields(3).Value = 120
rs.Update
End Sub
Private Sub Command3_Click()
Set DataGrid1.DataSource = rs
End Sub
Private Sub Command4_Click()
rs.UpdateBatch
End Sub
'例如:你在窗体中再增加一些文本框和组合框,供用户输入或选择数据表的名称、字段的名称、字段的宽度和小数位数。
'这样,用户就可以自主地决定数据库的所有内容了,程序的灵活性也就大大提高了。

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -