标题: 在后台数据库创建并链接到本地很奇怪的问题 [打印本页] 作者: 盗到稻 时间: 2008-10-20 11:13 标题: 在后台数据库创建并链接到本地很奇怪的问题 Public Function CreateTable(sTableName As String)
'建立表并完成链接
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strSQL As String
strSQL = "CREATE TABLE [" & sTableName & "] (Number1 String(6), Name2 text(8), Tel3 text(14),Tel4 text(14));"
If cnn.State = adStateOpen Then cnn.Close
cnn.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & CurrentProject.path & "\source.mdb;" '不能用Provider 与Data Source关键字
cnn.Execute strSQL
cnn.Close
Set cnn = Nothing
'建立完成,接下的代码是完成链接
'++++++++++++++++++++++++++++++++++
'Dim dbs As DAO.Database
Dim tdl As DAO.TableDef
Set dbs = CurrentDb
Set tdl = dbs.CreateTableDef(sTableName) '新表名
tdl.C & CurrentProject.path & "\source.mdb;" '链接源
tdl.SourceTableName = sTableName '链接表
DoEvents
dbs.TableDefs.Append tdl '这里第一次会出错,要第二次才能过去,或者出错时点“调试”,再点“继续运行”按钮才能通过
dbs.Close
Set dbs = Nothing
End Function
‘把建立链接的DAO代码换成如下的ADOX代码,执行到相关的那个地方也是一样出错,要二次才能通过
On Error GoTo Errh
Dim DB As ADOX.Catalog
Dim tblLink As ADOX.Table
Set DB = New ADOX.Catalog
DB.ActiveConnection = CurrentProject.Connection
Set tblLink = New ADOX.Table
With tblLink
.Properties("Jet OLEDB:Remote Table Name") = TableName
End With
DB.Tables.Append tblLink '也是这里
Set tblLink = Nothing
Set DB = Nothing
Exit Function
Errh: