Sql 使用两个MDB选择第三个MDB

Sql 使用两个MDB选择第三个MDB,sql,vb6,adodb,Sql,Vb6,Adodb,我在VB6中生成此SQL字符串时遇到问题 Select A.ID, A.AstTp, A.Offset, A.Age, B.LNo, B.ACnt, B.CommCnt Into [LnReg] From [ALPA] In [c:\Temp\ALPA.mdb] As A LEFT OUTER JOIN [ALX] IN [c:\Temp\ALX.mdb] As B On A.ID = B.ID Where (A.AstTp="Sealed") 我的ADO连接是通过LnReg连接到md

我在VB6中生成此SQL字符串时遇到问题

Select A.ID, A.AstTp, A.Offset, A.Age, B.LNo, B.ACnt, B.CommCnt 
Into [LnReg] 
From [ALPA] In [c:\Temp\ALPA.mdb] As A 
LEFT OUTER JOIN 
[ALX] IN [c:\Temp\ALX.mdb] As B On A.ID = B.ID Where (A.AstTp="Sealed")
我的ADO连接是通过LnReg连接到mdb

引发的错误是“[c:\Temp\ALPA.mdb]”不是有效的名称

[EDIT]FROM子句中的语法错误

Select A.ID, A.AstTp, A.Offset, A.Age, B.LNo, B.ACnt, B.CommCnt 
Into [LnReg] 
From [ALPA] In "c:\Temp\ALPA.mdb" As A 
LEFT OUTER JOIN 
[ALX] IN "c:\Temp\ALX.mdb" As B On A.ID = B.ID Where (A.AsTp="Sealed")

尝试使用双引号而不是方括号:

Select A.ID, A.AstTp, A.Offset, A.Age, B.LNo, B.ACnt, B.CommCnt 
Into [LnReg] 
From [ALPA] In "c:\Temp\ALPA.mdb" As A LEFT OUTER JOIN 
     [ALX] IN "c:\Temp\ALX.mdb" As B
     On A.ID = B.ID
Where (A.AstTp="Sealed");

我更喜欢将表从其他mdb文件链接到主mdb文件。我使用这个函数来链接表。使用链接表,您可以执行与使用本地表类似的任何操作

Function AccessLinkToTable(sLinkFromDB As String, sLinkToDB As String, sLinkToTable As String, Optional sNewLinkTableName As String, Optional sPassword As String) As Boolean
    'Inputs      :  sLinkFromDB                 The path to the original database.
    '               sLinkToDB                   The path to the database to link to.
    '               sLinkToTable                The table name to link to in sLinkToDB.
    '               [sNewLinkTableName]         The name of the new link table. sLinkFromDB.
    'Outputs     :  Returns True if succeeded in linking to the table
    'Author      :  Andrew Baker www.vbusers.com
    'Date        :  03/09/2000 14:17
    'Notes       :  Requires a reference to reference to both ADO (MS ActiveX Data Objects) and MSADOX.DLL
    '               (MS ADO Ext. 2.5 DLL and Security).
    'Revisions   : 21.1.2002, Roman Plischke, password
    Dim catDB As ADOX.Catalog
    Dim TblLink As ADOX.Table

    On Error GoTo ErrFailed
    If Len(Dir$(sLinkFromDB)) > 0 And Len(Dir$(sLinkToDB)) > 0 Then
        'Databases exist
        Set catDB = New ADOX.Catalog
        'Open a Catalog on database in which to create the link.
        catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & sLinkFromDB

        Set TblLink = New ADOX.Table
        With TblLink
            'Name the new Table
            If Len(sNewLinkTableName) Then
                .Name = sNewLinkTableName
            Else
                .Name = sLinkToTable
            End If

            'Set ParentCatalog property to the open Catalog.
            'This allows access to the Properties collection.
            Set .ParentCatalog = catDB

            'Set the properties to create the link.
            .Properties("Jet OLEDB:Create Link") = True
            .Properties("Jet OLEDB:Link Datasource") = sLinkToDB
            .Properties("Jet OLEDB:Remote Table Name") = sLinkToTable
            If Len(sPassword) Then
                .Properties("Jet OLEDB:Link Provider String") = "MS Access;Pwd=" & sPassword
            End If
        End With

        'Append the table to the Tables collection.
        catDB.Tables.Append TblLink
        Set catDB = Nothing
        'Set return as success
        AccessLinkToTable = True
    End If
    Exit Function

ErrFailed:
    On Error GoTo 0
    AccessLinkToTable = False
End Function

Function AccessLinkTableUpdate(sLinkDatabasePath As String, sLinkToNewDatabase As String, sLinkTableName As String) As Boolean
    Dim catDB As ADOX.Catalog

    On Error GoTo ErrFailed
    Set catDB = New ADOX.Catalog
    'Open a catalog on the database which contains the table to refresh.
    catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & sLinkDatabasePath
    If catDB.Tables(sLinkTableName).Type = "LINK" Then
        catDB.Tables(sLinkTableName).Properties("Jet OLEDB:Link Datasource") = sLinkToNewDatabase
        AccessLinkTableUpdate = True
    End If
    Set catDB = Nothing
    Exit Function

ErrFailed:
    On Error GoTo 0
    AccessLinkTableUpdate = False
End Function

我现在得到一个“FROM子句中的语法错误”,对问题进行了编辑以反映新的查询。我使用以下方法从dbf到mdb获取一个表,可能与此问题类似???strSQL=“SELECT*INTO[”&table_name&“]from[dBase IV;DATABASE=“&sourceDBpath&”]。“&table_name&]”很抱歉耽搁了您的时间,这要优雅得多,谢谢
Function AccessLinkToTable(sLinkFromDB As String, sLinkToDB As String, sLinkToTable As String, Optional sNewLinkTableName As String, Optional sPassword As String) As Boolean
    'Inputs      :  sLinkFromDB                 The path to the original database.
    '               sLinkToDB                   The path to the database to link to.
    '               sLinkToTable                The table name to link to in sLinkToDB.
    '               [sNewLinkTableName]         The name of the new link table. sLinkFromDB.
    'Outputs     :  Returns True if succeeded in linking to the table
    'Author      :  Andrew Baker www.vbusers.com
    'Date        :  03/09/2000 14:17
    'Notes       :  Requires a reference to reference to both ADO (MS ActiveX Data Objects) and MSADOX.DLL
    '               (MS ADO Ext. 2.5 DLL and Security).
    'Revisions   : 21.1.2002, Roman Plischke, password
    Dim catDB As ADOX.Catalog
    Dim TblLink As ADOX.Table

    On Error GoTo ErrFailed
    If Len(Dir$(sLinkFromDB)) > 0 And Len(Dir$(sLinkToDB)) > 0 Then
        'Databases exist
        Set catDB = New ADOX.Catalog
        'Open a Catalog on database in which to create the link.
        catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & sLinkFromDB

        Set TblLink = New ADOX.Table
        With TblLink
            'Name the new Table
            If Len(sNewLinkTableName) Then
                .Name = sNewLinkTableName
            Else
                .Name = sLinkToTable
            End If

            'Set ParentCatalog property to the open Catalog.
            'This allows access to the Properties collection.
            Set .ParentCatalog = catDB

            'Set the properties to create the link.
            .Properties("Jet OLEDB:Create Link") = True
            .Properties("Jet OLEDB:Link Datasource") = sLinkToDB
            .Properties("Jet OLEDB:Remote Table Name") = sLinkToTable
            If Len(sPassword) Then
                .Properties("Jet OLEDB:Link Provider String") = "MS Access;Pwd=" & sPassword
            End If
        End With

        'Append the table to the Tables collection.
        catDB.Tables.Append TblLink
        Set catDB = Nothing
        'Set return as success
        AccessLinkToTable = True
    End If
    Exit Function

ErrFailed:
    On Error GoTo 0
    AccessLinkToTable = False
End Function

Function AccessLinkTableUpdate(sLinkDatabasePath As String, sLinkToNewDatabase As String, sLinkTableName As String) As Boolean
    Dim catDB As ADOX.Catalog

    On Error GoTo ErrFailed
    Set catDB = New ADOX.Catalog
    'Open a catalog on the database which contains the table to refresh.
    catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & sLinkDatabasePath
    If catDB.Tables(sLinkTableName).Type = "LINK" Then
        catDB.Tables(sLinkTableName).Properties("Jet OLEDB:Link Datasource") = sLinkToNewDatabase
        AccessLinkTableUpdate = True
    End If
    Set catDB = Nothing
    Exit Function

ErrFailed:
    On Error GoTo 0
    AccessLinkTableUpdate = False
End Function