views:

76

answers:

3

Hi,

I am trying to create a Linked table in MS Access linked to Excel sheet. I want to do this through VBscripting.

My Scenario is i will have a excel sheet which will be updated very often. But my script picks up the values from the table in MSAccess which should be a replica of the Excel sheet (Linked table).

So i want to know if there is any code in VBscript wherein i can create a Linked table to ExcelSheet.

Any help is Greatly Appreciated!!

A: 

Yes you are correct. But my question is to "How to link an Excel with MS Access Table using VBScript" for the first time itself. Excel is not already linked.

Guest
A: 

Here is some sample script.

   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

From: http://wiki.lessthandot.com/index.php/Linking_Tables_via_Jet_and_ADO

Remou
A: 

Hi,

Thanks for the reply. I am able to undrestand and implement the 'Link Excel by Sheet Name'

Can you please help me understand the other part 'Link Excel using named range' wherein it uses .properties("Jet OLEDB:Remote Table Name") = "Data_Range" Not sure what is/should the Data_Range be here?

Thanks Again!!

Guest
The note above is a comment and should be added as a comment, I have voted your question up so you should have enough reputation to use comments from now on.
Remou
There are two different approaches, depending on your sheet. If you use named ranges, you can refer to the range by name, eg Data_Range, rather than the full sheet, eg SheetName$, or sheet name and range, eg SheetName$B1:C10. If you do not use named ranges, it is not relevant to you.
Remou
There are two different guest logons here (if you follow the user link, you'll see different IDs), so while the logon used to ask the question has reputation 6, the other logon has only reputation 1. Conclusion: it's a waste of time to interact with people who don't bother to sign up with a proper SO account.
David-W-Fenton