Link to home
Start Free TrialLog in
Avatar of sd854282
sd854282

asked on

Pulling Great Plains odbc connected tables into Access

I am trying to automate the download of tables out of Great  Plains into Access.  I have an entire process of queries setup in a macro; however, to initiate the login to sql, I was told I must use code as follows:

'LOGON CODE:
   Dim cnn As ADODB.Connection
   Dim rs As ADODB.Recordset
   Set cnn = New ADODB.Connection
   cnn.ConnectionString = "driver={SQL Server};" & "server=MySqlServer;uid=sa;Pwd=password;database=pubs"

'EXECUTION CODE:
  cnn.Open
  cnn.Execute ("INSERT INTO localtable SELECT IVC30101.* FROM IVC30101;")
    DoEvents

The problem is that the code is is trying to, I believe, write the table in the sql database - not the local Access mdb.  Even though the connection is established, I can then not run a macro to do the query and subsequent steps because it brings up the sql login box.

So, the way I see it, the problem can be solved 2 ways -
1) Allow the login from the first part of the code above to be used in the Access database queries to pull from the Great Plains tables without reentering the password.
2) Modify the sql above so that the INSERT area will actually insert into a local table.

Any suggestions would be GREATLY appreciated.
Avatar of mildurait
mildurait
Flag of Australia image


<cnn.Execute ("INSERT INTO localtable SELECT IVC30101.* FROM IVC30101;")>
Aside from the sql connection issue, the code would insert data into a table called localtable within the gp sql database.
Suggest linking table IVC30101 Using File / Get External Data / Link tables and renaming the linked table to IVC30101.

Then you can run the code
Call docmd.runsql("INSERT INTO INSERT INTO localtable SELECT IVC30101.* FROM IVC30101;")



Avatar of sd854282
sd854282

ASKER

Thanks.  The above does get it into the local Access mdb instead of sql.  However, the login issue is still present.

The data is still in sql, it just a appears as a linked table.
There should be an option that you can check to remember username and password when you link the table.
My mistake....the  code above did NOT get it into a local table in the Access database. My error message states, "[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name localtable.

The table called localtable does exist in the Access db.  I think the code is trying to put the data in to sql - NOT Access.

 Here is what I have currently:

Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = New ADODB.Connection
cnn.ConnectionString = "driver={SQL Server};" & "server=SERVER1;uid=sa;pwd=password;database=ABCD"
cnn.Open
Set rs = cnn.Execute("INSERT INTO localtable SELECT IVC30101.* FROM IVC30101;"
 rs.Close
Yep, that's exactly what the code is trying to do (see my original comment).
You need to use the docmd.runsql(sql) after linking the sql table to access to write to a local table in access.
The SQL table is linked.  The SQL table is linked to the access db that this code is running in.  Localtable is in access.
Try
You need to execute the sql locally rather than in the sql database.

call docmd.runsql("INSERT INTO localtable SELECT IVC30101.* FROM IVC30101;")

<The above does get it into the local Access mdb instead of sql.  However, the login issue is still present.>
You need to unlink and link the sql table again ensuring that the remember password option is checked.
The SQL is executed locally in access through VGA
<cnn.ConnectionString = "driver={SQL Server};" & "server=SERVER1;uid=sa;pwd=password;database=ABCD">
Yes VBA is executing the code locally, but the data updates are executed in the ABCD database on the SERVER1 sql server directly, not in access.

You need VBA to maniplulate the local table and the locally linked sql table, which is why you would run docmd.runsql.
Below are the two options I am currently working with:

1)   Set rs = cnn.Execute("INSERT INTO localtable SELECT IVC30101.* FROM IVC30101;")
2)   Call DoCmd.RunSQL("INSERT INTO localtable SELECT IVC30101.* FROM IVC30101;")

The problem is that option 2 still requires a login.  Even if you tell the odbc connection to remember, the password, it doesn't.  

Any ideas?

2) is the correct answer to this question.

<Even if you tell the odbc connection to remember, the password, it doesn't.>
This is a separate issue.  
Suggest asking another question.
ASKER CERTIFIED SOLUTION
Avatar of Jim P.
Jim P.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial