主题:[原创]动态创建数据库和数据表时出现的问题
在VB6.0中,为了动态地创建数据库,编了一个测试程序,在一个标准窗体上放了三个命令按钮、一个DataGrid表格和一个通用对话框控件,程序如下:
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.3.51;" '不能把这里的3.51改为4.0
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
Private Sub Command2_Click()
Set DataGrid1.DataSource = rs
End Sub
Private Sub Command3_Click()
rs.UpdateBatch
End Sub
在运行时,当输入数据库名之后,出现如上图所示错误,调试时,错误位置出现在“Cat.Create pstr '创建数据库”这句上,请各位高手帮忙解决一下,万分感谢
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.3.51;" '不能把这里的3.51改为4.0
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
Private Sub Command2_Click()
Set DataGrid1.DataSource = rs
End Sub
Private Sub Command3_Click()
rs.UpdateBatch
End Sub
在运行时,当输入数据库名之后,出现如上图所示错误,调试时,错误位置出现在“Cat.Create pstr '创建数据库”这句上,请各位高手帮忙解决一下,万分感谢