4

I am trying to create a linked (or imported) table in Access with a SQL Server backend. Basically the business users periodically need a copy of the table [SQL Rulesnew] (yes, with the space, sigh) so we want to give them a little Access 2003 tool that will do the job on demand.

I did try using Docmd.TransferDataBase acTable but no luck

here is the code I am using:

Sub getData()

Dim sConnStr As String
Dim oTable As TableDef
Dim sDestinationTable As String
Dim dbs As Database
Dim tbl As DAO.TableDef
Dim tblLinked As DAO.TableDef       

    sDestinationTable = "SQL Rulesnew"
    Set dbs = CurrentDb

    ' source table name has a SPACE (rolleyes!)
    CurrentDb.CreateTableDef sDestinationTable  

    ' got the below from a Data Link File (UDL)
    sConnStr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MYDBNAME;Data Source=MYSERVERNAME"

    ' the below also failed!
    'DoCmd.TransferDatabase acLink, "ODBC Database", "ODBC;Driver={SQL Server};Server=Fos;Database=Hermes_Rep;Trusted_Connection=Yes", acTable, "[Report SQLRulesnew]", "SQLRules" & VBA.Format(Now, "ddmmyyyy")

    'If DCount("*", "MSysObjects", "[Name]='[SQL Rulesnew]' AND [Type] In (1, 4, 6)") > 0 Then
    If IsTable(sDestinationTable) Then
       DoCmd.DeleteObject acTable, sDestinationTable
    End If

    Set tblLinked = dbs.CreateTableDef(sDestinationTable)
    Debug.Print "Linking the " & sDestinationTable
    tblLinked.Connect = sConnStr
    tblLinked.SourceTableName = sDestinationTable
    dbs.TableDefs.Append tblLinked
    tblLinked.RefreshLink

End Sub

Function IsTable(sTblName As String) As Boolean
    'does table exists and work ?
    'note: finding the name in the TableDefs collection is not enough,
    '      since the backend might be invalid or missing
Dim x
    On Error GoTo Coventry
    x = DCount("*", sTblName)
    IsTable = True
    Exit Function

Coventry:
    Debug.Print Now, sTblName, Err.Number, Err.Description
    IsTable = False
End Function

unfortunately I get an error could not find installable ISAM on the line dbs.TableDefs.Append tblLinked

what should I do?

thanks Philip

2
  • Do you already have a linked SQL Server table? If so, check the connect property of the tabledef for a working connection string. Commented Apr 3, 2013 at 9:28
  • I found the answer and posted it below Commented Apr 3, 2013 at 9:47

1 Answer 1

6

I found the answer through trial and error...

Basically my table names in Access can have a space without using the [square brackets], so the below command works fine (after deleting any existing object):

DoCmd.TransferDatabase _
  acImport, _
  "ODBC Database", _
  "ODBC;Driver={SQL Server};Server=Fos;Database=Hermes;Trusted_Connection=Yes", _
  acTable, _
  "sourceTable", _
  "targetTable"
Sign up to request clarification or add additional context in comments.

2 Comments

sometimes, while running non-english windows you might get error 2507, change "ODBC Database" for "ODBC" (works with French). (Thanks to tony gil: stackoverflow.com/questions/5858366/…)
This doesn't work real good if you're trying to automate it, try this instead: stackoverflow.com/a/38266765/18149

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.