Question

Connect to an ADP using ADO...

Asked by: WATYF

Hi there... I'm having a horrible time getting data out of my SQL Server. I need a completely flexible, 100% code-based method of extracting data into a text file or Access table, but none seem to exist (that don't have significant setbacks). DTS's require front-end setup in Enterprise Manager, BCP requires command line access on the server, etc. etc.

So... after giving up on being able to get data directly from the SQL Server,  I decided to set up an ADP and connect it to my SQL Server (kind of as a "middle man" approach)... So now, I would like to connect to that ADP using ADO (from with Excel, or Access or whatever). So what provider do I use? (the Jet provider didn't work) What's the connection string needed to do this?



WATYF


P.S. I'm working in Access 2000

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-10-08 at 13:40:03ID21161603
Tags

adp

,

connection

Topic

Microsoft Access Database

Participating Experts
4
Points
0
Comments
15

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. To ADP or not to ADP
    I am in the process of moving several systems (stock control, accounting, and others) to an SQL server backend. Currently these systems use either a C-ISAM based file access method or an Access database. My two options are Delphi or staying with Access. I am more comfortable ...
  2. Long winded ADP ADO OLE DB ODBC question
    Im wondering if someone can give me some explanation regarding these layers of communication. If find them quite confusing. In particular, the hype regarding how great Access ADP files are because of their native connectivity with SQL Server. In my limited experience with ...
  3. How to Import a .CSV File into an existing .ADP table on …
    I have an .ADP project that is the front-end for my database which sits on an SQL 2000 Server. I need to import a .CSV file into an existing table using ADO 2.8 and Provider "Microsoft.Access.OLEDB.10.0" I'm sure there is a way to read the .CSV file in ADO and sim...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: shanesuebsahakarnPosted on 2004-10-08 at 13:44:40ID: 12262943

 

by: WATYFPosted on 2004-10-08 at 14:06:19ID: 12263139

Well I'm not connecting to SQL Server... I'm connecting to the ADP file... are you saying I need to connect to it as if it's SQL Server?


I tried using a SQL Server con string to connect to the ADP, but I kept getting this error:

[DBNETLIB][ConnectionOpen (ParseConnectParams()).]Invalid connection.


Here was the connection string I used:

con.Open "Provider=sqloledb;Data Source=C:\Temp\Test3.adp;Initial Catalog=MYDB;User Id=myuser;Password=mypass"


I can connect straight to the SQL Server just fine using that same exact string (replacing the adp path with the server name).


WATYF

 

by: shanesuebsahakarnPosted on 2004-10-08 at 14:19:06ID: 12263247

Hold on, you can't connect from something else to the ADP file, since the ADP file itself has no local tables for you to connect to.

If you can connect to the SQL Server, why not use that connection to transfer data from a recordset into your table or text file?

 

by: LSMConsultingPosted on 2004-10-08 at 14:23:30ID: 12263273

I don't understand ... if you can connect directly to the SQL Server, why use an ADP? You should be able to connect from any ADO-aware host host (Excel, Access, VB, VB.NET, etc) and extract your data from there. Basically, the commands will be the same and you can cut out the overhead of an ADP. If you write class/standard modules with fairly generic language, you should have no trouble.

Or mayby I'm missing something here ...

 

by: Jokra_the_BarbarianPosted on 2004-10-08 at 14:31:03ID: 12263321

Connecting to SQL Server should be easy. Do the following:
1. Make sure you have data in the SQL Server database tables.
2. Make sure you have a user in SQL Server that can access the SQL Server database.
3. Create a new Access Project (Existing Database) .
4. The project should initally display a Data Link Properties window.
5. Enter the required information and test the connection.
6. If the test connection succeeded, you are ready to go.
7. Create a new Access module under the Modules tab.
8. Paste the following into the vba window:
  Public Sub cnntest()
    Dim cmd As ADODB.Command
    Dim rst As ADODB.Recordset

    Set cmd = New ADODB.Command
    Set prm = New ADODB.Parameter
   
    With cmd
        .ActiveConnection = CurrentProject.Connection
        .CommandText = "select * from table1"
        .CommandType = adCmdText
    End With

    Set rst = cmd.Execute
   
    Debug.Print rst.RecordCount

    rst.Close
    Set rst = Nothing
    Set cmd = Nothing

End Sub

9. Note: Modify the .commandText string to return records from your table.
10. In the immediate window, type cnntest, then press the Enter key.

You should get a record count returned from SQL Server. Let me know if this works.

 

by: WATYFPosted on 2004-10-08 at 14:48:23ID: 12263413

OK... I left out some key details as to why I'm doing this...


I am quite familiar with using ADO to connect to SQL Server... and returning data to a recordset is not a problem. But I don't want a recordset... I want a table (or a text file). My problem, up to this point, has been finding an efficient way to get the data from SQL Server straight into an Access table or a text file... and there doesn't seem to be one.

I have tried BCP, but it requires granting way too much server-side permissions to a standard user... I've tried DTSs, but they have to be setup manually in the Enterprise Manager... and I've tried using an ADO connection, but all that gets me is a recordset...


This:


    Dim con As New ADODB.Connection
    Dim rs As ADODB.Recordset
   
    con.Open "Provider=sqloledb;Data Source=MYSERVER;Initial Catalog=MYDB;User Id=myuser;Password=mypass"
    Set rs = con.Execute("SELECT * FROM MyTable")


...only gets me a recordset (rs) populated with data. That does me no good. I want that data in a table or a text file, not a recordset.


If the database was in Access (instead of SQL) then I could use an SQL statement like this:

"SELECT * INTO MyTable IN "C:\Temp\MyDB.mdb" FROM SourceTable"

...but the "IN" operator doesn't work on SQL Server... So I wanted to try using an ADP, so I could run the SQL statements on an Access ADP, which would return data from SQL Server. It is only a theory, since I'm not sure it will work, but I can't even test the theory because I can't connect to the ADP to see if a "SELECT INTO IN" statement will work.


I hope that's clear. If anyone has other ways of getting data from SQL straight into Access, or getting data from a recordset into a table or text file, then that would be exactly what I'm looking for.


(and no... I do not want to loop through the recordset and do "INSERT INTO" statements for each record... that would be extremely inefficient considering the number of records I'm dealing with)



WATYF

 

by: shanesuebsahakarnPosted on 2004-10-08 at 14:57:05ID: 12263455

In that case, dynamically create a table link to the SQL Server, run an append query and then run an insert query to select all records from the table link to your Access table, or else use TransferText to get the data out into a file. You can then delete the table link in code.

 

by: WATYFPosted on 2004-10-08 at 20:10:23ID: 12264856

I had previously tried setting up linked tables, and then altering their "Connect" and "SourceTableName" name properties as needed. The problem I ran into was, when trying to open that linked table as other users on other machines, it would always prompt for a password for SQL Server, even though the user and pass were contained in the Connect string... (obviously I don't want that prompt.. they shouldn't even know that they're being connected to a server).

But I didn't create those tables from scratch using code... in my tests I just created them manually, and altered them with code. Perhaps if I create them with code, I can set up the connection dynamically at run time and avoid the prompt...


...so can you provide an example of how to create a linked table from scratch? Is it simply a matter of creating a new tabledef and then setting its Connect and SourceTableName properties?


WATYF

 

by: alanwarrenPosted on 2004-10-08 at 21:09:09ID: 12265001

Hi WATYF,

If you want to stop the users from accessing the tables in the ADP create a View and give the user/role you use in your connect string permissions on the View, but remove all permissions for the tables.

If you want to connect to sql and return a table as a disconected object that can later be sourced by other apps, then rerurn a recordset and persist it as XML, you can then use the the msPersist diriver to open the XML as a recordset from any other app.

Public Sub Connect2Sql()
  Dim strDataConnect
  Dim objCn As ADODB.Connection
  Dim objRs As ADODB.Recordset
  Dim strSql As String
 
  Set objCn = New ADODB.Connection
  Set objRs = New ADODB.Recordset
 
  strDataConnect = adoConnectSQL("pwd", "sa", "Northwind", "(local)")
  strSql = "Select * from Employees"
 
  objCn.Open strDataConnect
 
  objRs.Open strSql, objCn, adOpenKeyset, adLockOptimistic
 
  With objRs
    Kill CurrentProject.Path & "\Employees.xml"
    objRs.Save CurrentProject.Path & "\Employees.xml", adPersistXML
    .Close
 
  End With
  Set objCn = Nothing
 
 
  ' Then in some other app open the xml as a recordset
  objRs.Open CurrentProject.Path & "\Employees.xml", "Provider=MSPersist;", , , adCmdFile
 
  With objRs
    ' Do stuff
    .Close
  End With
 
  Set objRs = Nothing
 
End Sub


Public Function adoConnectSQL(psPassword, psUser, psCatalog, psDataSource)

  ' Returns SQLOLEDB ADO connect string
  ' Uses SQL Server security

  Dim sProvider, sPassword, sPersist, sUser, sCatalog, sDataSource

  sProvider = "Provider=SQLOLEDB.1;"
  sPassword = "Password=" & psPassword & ";"
  sPersist = "Persist Security Info=True;"
  sUser = "User ID=" & psUser & ";"
  sCatalog = "Initial Catalog=" & psCatalog & ";"
  sDataSource = "Data Source=" & psDataSource
  adoConnectSQL = sProvider & sPassword & sPersist & sUser & sCatalog & sDataSource

End Function


Hope this helps

Alan

 

by: WATYFPosted on 2004-10-08 at 21:38:44ID: 12265059

That's a very interesting feature... it's good to know that I can save off a recordset to a compact file (using adPersistADTG) and restore it later to a recordset.


But unfortunately, this doesn't address my issue. I'm am still left with a recordset as my end result, when the end result I'm looking for is a table.


WATYF

 

by: LSMConsultingPosted on 2004-10-09 at 06:13:00ID: 12266023

In your original post, you mentioned you wanted to write to a text file ... this will open a recordset and write it to a text file named "MyTextFile.txt" in the application's current directory. YOu could certainly alter this to accept a recordset, filepath, etc as arguments and therefore make this more "portable". Note that you need a reference to the MS ActiveX Datat Objects in your project to run this.

Function WriteToFile() As Boolean

  '/Purpose:
  '/Created: 10/9/2004 08:37 AM
  '/Created By: Scott

  Dim rst          As ADODB.Recordset
  Dim i            As Integer
  Dim strLine      As String
  Dim lngFile      As Long
 
  On Error GoTo Err_WriteToTable
  Set rst = New ADODB.Recordset
 
  lngFile = FreeFile
 
  rst.Open "SELECT * FROM tblClass", CurrentProject.Connection
 
  '/if the file already exists, kill it
  On Error Resume Next
  Kill CurrentProject.path & "\YourTextFile.txt"
  '/now open the new file
  On Error GoTo Err_WriteToTable
  Open CurrentProject.path & "\YourTextFile.txt" For Append As lngFile
    '/print the field names; if you don't want to print the field names, comment out the For - Next loop below
    For i = 0 To rst.Fields.Count - 1

        If Len(strLine) = 0 Then
          strLine = rst.Fields(i).Name
        Else
          strLine = strLine & "," & rst.Fields(i).Name
        End If 'If Len(strLine) = 0

    Next i
 
  Print #lngFile, strLine
 
  strLine = vbNullString

    Do Until rst.EOF

        For i = 0 To rst.Fields.Count - 1

            If Len(strLine) = 0 Then
              strLine = rst.Fields(i)
            Else
              strLine = strLine & "," & rst.Fields(i)
            End If 'If Len(strLine) = 0

        Next i

      Print #lngFile, strLine
      strLine = vbNullString

      rst.MoveNext
    Loop

Exit_WriteToTable:

  On Error Resume Next
  Close #lngFile
  Set rst = Nothing
  Exit Function

Err_WriteToTable:

    Select Case Err
      'case
      'case
      Case Else
        MsgBox Err & ":" & Error$, vbCritical, "basDevelopmentRoutines" & ": " & "WriteToTable"
    End Select

  Resume Exit_WriteToTable

End Function


 

by: WATYFPosted on 2004-10-13 at 10:18:08ID: 12300298


I finally found what I'm looking for... it was a single SQL Statement.


SELECT * INTO [AccTable] FROM [odbc;driver={SQL Server};server=MYSERVER;database=MYDB;uid=myuser;pwd=mypass].[SQLTable]


Pulls right from SQL Server into Access DB when run from the Access DB.



WATYF

 

by: shanesuebsahakarnPosted on 2004-10-14 at 08:30:37ID: 12309175

No objections.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...