0

This Microsoft KB article details how to run a query on another database than the current one used by the Access project. However it only states how to connect to DBase, Foxpro, Paradox, BTrieve and ODBC.

I want to be able to do something like this:

UPDATE MSSQLDatabase.Table
    SET MSSQLDatabase.Table.Column = AccessDatabase.Table.Column
    WHERE MSSQLDatabase.Table.Column = AccessDatabase.Table.ID

INSERT INTO AccessDatabase.Table
    VALUES (AccessDatabase.Table.ID)

Can you give me any pointers of where to begin? The database I want to connect to is a SQL Server 2008 Provider Native connection. I'm using Access 2007.

To do this in VBA would be perfect.

2
  • 1
    Any reason why you cannot link the SQL Server table? Commented Sep 26, 2012 at 12:46
  • @Remou I guess not, hadn't heard of linked tables before - can you elaborate a little? (Maybe you could modify your answer) Thanks! Commented Sep 26, 2012 at 12:55

2 Answers 2

1

By far the easiest way to work with SQL Server in MS Access is to use linked tables. However, you can also run pass-through queries and refer to a connection in-line:

SELECT * FROM [ODBC;FILEDSN=Z:\Docs\Test.dsn;].table_1

Or

SELECT * FROM 
  [ODBC;DRIVER=SQL Server;SERVER=srvr;Trusted_Connection=Yes;DATABASE=Test;].table_1

Or

SELECT * FROM [ODBC;Driver={SQL Server Native Client 11.0};Server=svr;Database=test;Trusted_Connection=yes;].table_1

see also http://www.connectionstrings.com/sql-server-2008

Sign up to request clarification or add additional context in comments.

11 Comments

I do not have available, nor want to use an ODBC connection.
I should add that ODBC is the recommended way to work with SQL Server. OleDB will soon be unsupported ( blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/… )
I do not have the facility to use an ODBC connection. It is on a customer's server, which I do not have such access to. I don't want to use OleDB either - I want to use Provider Native.
Do you mean SQL Server Native Client? "All appropriate registry settings for the SQL Server Native Client OLE DB provider and the SQL Server Native Client ODBC driver are made as part of the installation process." -- msdn.microsoft.com/en-us/library/ms131321.aspx
I believe that is to be able to create an ODBC connection to SQL Native. I want to directly connect with SQL Native. I may be able to just use the connection string directly in there though. I'll try.
|
0

This solution allows to catch errors:

Private Sub Command10_Click()
    On Error GoTo Err1:
        Dim cn As ADODB.Connection
        Set cn = New ADODB.Connection
        With cn
            .Provider = "SQL Native Client"
            .ConnectionString = "Server=myserver\myinstance;Database=mydb;Uid=myuser;Pwd=mypass;]"
            .Open
        End With

        MsgBox "Connection successful!"
        cn.Close
        Exit Sub

    Err1:
        MsgBox Err.DESCRIPTION
End Sub

The only thing to note, is that within the Visual Basic Editor, you must first go to Tools > References, and check Microsoft ActiveX Data Objects 2.x Library.

Comments

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.