title: Error when trying to use AttachDSNLessTable function
body: <p>I am trying to connect to a SQL Server 2008 using a dsn-less connection. I'm trying to use the <code>AttachDSNLessTable</code> routine but have not been able to get it to work. I get the message </p>
  
  <blockquote>
    <p>Run-time error '2425' The expression your entered has a function name that microsoft Access can't not find</p>
  </blockquote>
  
  <p>I have a macro named AutoExec that runs code AttachDSNLessTable(). </p>
  
  <p>I wrote In the AutoExec Macro :</p>
  
  <pre><code>AttachDSNLessTable ("Land", "dbo.Land", "ABZAL", "Pdata", "sa", "294285")
  </code></pre>
  
  <p>AttachDSNLessTable Code:</p>
  
  <p>Option Compare Database</p>
  
  <pre><code>'//Name     :   AttachDSNLessTable
  '//Purpose  :   Create a linked table to SQL Server without using a DSN
  '//Parameters
  '//     stLocalTableName: Name of the table that you are creating in the current database
  '//     stRemoteTableName: Name of the table that you are linking to on the SQL Server database
  '//     stServer: Name of the SQL Server that you are linking to
  '//     stDatabase: Name of the SQL Server database that you are linking to
  '//     stUsername: Name of the SQL Server user who can connect to SQL Server, leave blank to use a Trusted Connection
  '//     stPassword: SQL Server user password
  Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
      On Error GoTo AttachDSNLessTable_Err
      Dim td As TableDef
      Dim stConnect As String
  
  For Each td In CurrentDb.TableDefs
      If td.Name = stLocalTableName Then
          CurrentDb.TableDefs.Delete stLocalTableName
      End If
  Next
  
  If Len(stUsername) = 0 Then
      '//Use trusted authentication if stUsername is not supplied.
      stConnect = "ODBC;DRIVER=SQL Server;SERVER=" &amp; stServer &amp; ";DATABASE=" &amp; stDatabase &amp; ";Trusted_Connection=Yes"
  Else
      '//WARNING: This will save the username and the password with the linked table information.
      stConnect = "ODBC;DRIVER=SQL Server;SERVER=" &amp; stServer &amp; ";DATABASE=" &amp; stDatabase &amp; ";UID=" &amp; stUsername &amp; ";PWD=" &amp; stPassword
  End If
  Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
  CurrentDb.TableDefs.Append td
  AttachDSNLessTable = True
  Exit Function
  </code></pre>
  
  <p>AttachDSNLessTable_Err:</p>
  
  <pre><code>AttachDSNLessTable = False
  MsgBox "AttachDSNLessTable encountered an unexpected error: " &amp; Err.Description
  </code></pre>
  
  <p>End Function</p>
  
  <p>Here is the link to the source <a href="http://support.microsoft.com/kb/892490" rel="nofollow">http://support.microsoft.com/kb/892490</a> I used method 1.</p>
  
  <p>Please help me.</p>