使用两个mdbs来选择进入第三个

时间:2014-07-25 01:53:39

标签: 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的mdb。

抛出的错误是'[c:\ Temp \ ALPA.mdb]'不是有效名称。

[编辑] 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")

3 个答案:

答案 0 :(得分:1)

我更喜欢将表从其他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

答案 1 :(得分:0)

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

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");

答案 2 :(得分:0)

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