2010-08-23 3 views
1

Excel 시트에 연결된 MS Access에서 연결된 테이블을 만들려고합니다. VBscripting을 통해이 작업을 수행하려고합니다.MS Access의 테이블에 Excel 시트 연결 - VBScript 사용

내 시나리오 내 시나리오는 매우 자주 업데이트되는 Excel 시트가 있습니다. 그러나 내 스크립트는 엑셀 시트 (링크 된 테이블)의 복제본 인 MSAccess의 테이블에서 값을 선택합니다.

VBscript에 Excel 시트에 연결된 테이블을 만들 수있는 코드가 있는지 알고 싶습니다.

+2

이 조금 이상한 소리가 난다. Excel이 이미 링크되어 있으면 새 값이 표시됩니다. 시트는 한 번만 연결해야합니다. 이 접근 방식으로 극복하려는 문제는 무엇입니까? – Fionnuala

답변

1

다음은 몇 가지 샘플 스크립트입니다.

Dim cn ''As ADODB.Connection 
    Dim ct ''As ADOX.Catalog 
    Dim tbl ''As ADOX.Table 

    Dim strLinkXL ''As String 
    Dim strMDB ''As String 

    strLinkXL = "C:\Docs\LTD.xls" 
    strMDB = "C:\Docs\LTD.mdb" 

    ''Create Link... 
    Set cn = CreateObject("ADODB.Connection") 
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ 
      "Data Source=" & strMDB & ";" & _ 
      "Persist Security Info=False" 

    Set ct = CreateObject("ADOX.Catalog") 
    Set ct.ActiveConnection = cn 

    Set tbl = CreateObject("ADOX.Table") 
    Set tbl.ParentCatalog = ct 


    ''Link Excel using named range 
    Set tbl = CreateObject("ADOX.Table") 
    Set tbl.ParentCatalog = ct 

    With tbl 
    .Name = "LinkTableXLRange" 
    .properties("Jet OLEDB:Link Provider String") = "Excel 8.0;DATABASE=" _ 
     & strLinkXL & ";HDR=Yes" 
    ''The named range 
    .properties("Jet OLEDB:Remote Table Name") = "Data_Range" 
    .properties("Jet OLEDB:Create Link") = True 
    End With 

    ''Append the table to the tables collection 
    ct.Tables.Append tbl 
    Set tbl = Nothing 

    ''Link Excel by sheet name 
    Set tbl = CreateObject("ADOX.Table") 
    Set tbl.ParentCatalog = ct 

    With tbl 
    .Name = "LinkTableXLSheet" 
    .properties("Jet OLEDB:Link Provider String") = "Excel 8.0;DATABASE=" _ 
      & strLinkXL & ";HDR=Yes" 
    ''Note the use of $, it is necessary 
    .properties("Jet OLEDB:Remote Table Name") = "Sheet2$" 
    .properties("Jet OLEDB:Create Link") = True 
    End With 

    ''Append the table to the tables collection 
    ct.Tables.Append tbl 
    Set tbl = Nothing 

은에서 : http://wiki.lessthandot.com/index.php/Linking_Tables_via_Jet_and_ADO