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 cat.Create pstr '创建数据库 Dim tbl As New Table cat.ActiveConnection = pstr tbl.Name = "MyTable" '表的名称 tbl.Columns.Append "编号", adInteger '表的第一个字段 tbl.Columns.Append "姓名", adVarWChar, 8 '表的第二个字段 tbl.Columns.Append "住址", adVarWChar, 50 '表的第三个字段 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.Update End Sub |