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.
sd854282Asked:
Who is Participating?
 
Jim P.Connect With a Mentor Commented:
Great Plains, in its current design, does not use Windows Authentication.

You can never save the password in a SQL Server ODBC call itself. That was done deliberately by Microsoft for security.

When you link tables you have two options. Using Integrated Security/Windows Authentication or SQL Authentication with a userid and password. Linked tables are using the DAO/JET engine, not the ADO connections.

If you want, you can create a separate Windows Domain userid in a GP DB, but that is done with the SQL Server Management Studio (or similar tools) and GP will totally disregard that userid.

If you use SQL Authentication, when you link the tables in, on the lower right-hand corner is a checkbox to Save Password. Turn it on.

As far as the ADO "rs = cnn.Execute" commands: That is telling the SQL Server to run on the Server side an insert into localtable which probably doesn't exist and that is why you are getting an error and no results. ADO does not know anything about your local tables in Access and can't see them by default.

You need to run the DAO command, something like below to get the data into your local tables
Dim SQL as String
 
SQL = "INSERT INTO localtable " & _
     "SELECT * " & _
     "FROM IVC30101;"
 
DoCmd.SetWarnings False
DoCmd.RunSQL SQL
DoCmd.SetWarnings True

Open in new window

Save-Pwd-Link-Tables.jpg
0
 
milduraitCommented:

<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;")



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

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
milduraitCommented:
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.
0
 
sd854282Author Commented:
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
0
 
milduraitCommented:
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.
0
 
sd854282Author Commented:
The SQL table is linked.  The SQL table is linked to the access db that this code is running in.  Localtable is in access.
0
 
milduraitCommented:
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.
0
 
sd854282Author Commented:
The SQL is executed locally in access through VGA
0
 
milduraitCommented:
<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.
0
 
sd854282Author Commented:
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?

0
 
milduraitCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.