Solved

Connecting to FoxPro database with ADO

Posted on 2006-12-01
5
386 Views
Last Modified: 2013-12-25
Vernerable Guru's,
I'm a little rusty using VB6/ADO and FoxPro Tables
I seem to be doing something wrong connecting to a FoxPro Database Table that I need to process.  The routine I have below appears to connect but as soon as I wish to do anything significant such as moving to end of the table or getting a record count it flakes out.  Can someone enlighten me whats going on to correct it?

Thank you!

Ted

Code ------------------------------------------------------------

Private Function ConnectDBF() As Boolean
    On Error GoTo Err_Handler
    Dim oCn As New ADODB.Connection
    Dim oRn As New ADODB.Recordset
    Dim oCd As New ADODB.Command
    Dim bColor As Long
    Dim sConn As String
    Const CONNECTION_STRING = "DSN=POS_DBF;UID=;PWD="
   
    Screen.MousePointer = 11
    oCn.Open "Driver={Microsoft Visual FoxPro Driver};" & _
           "SourceType=DBF;" & _
           "SourceDB=d:\pos;" & _
           "Exclusive=No"
   
    Set oCd.ActiveConnection = oCn
    oCd.CommandText = "Select  * from MyData.dbf"
    oRn.CursorLocation = adUseClient
    oRn.Open oCd, , adOpenDynamic, adLockReadOnly
    cmdConnect.Font.Bold = True
    bColor = cmdConnect.BackColor
    cmdConnect.BackColor = vbRed
    Screen.MousePointer = 0
    MsgBox "Connected to DBF." , vbInformation
    Msgbox oRn.RecordCount    '<<<<<<<<<<< "E_FAIL" ERROR MESSAGE OCCURS HERE!
   

QUit_Handler:
    Set oRn = Nothing
    Set oCd = Nothing
    Set oCn = Nothing
    cmdConnect.BackColor = bColor
    cmdConnect.Font.Bold = False
    Screen.MousePointer = 0
    Exit Function
   
Err_Handler:
    cmdConnect.BackColor = bColor
    Screen.MousePointer = 0
    cmdConnect.Font.Bold = False
    MsgBox Err.Description, vbInformation
    Resume QUit_Handler
    Resume
End Function

0
Comment
Question by:tcalbaz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
leonstryker earned 500 total points
ID: 18069265
Try this:

Private Function ConnectDBF() As Boolean
    Dim oCn As New ADODB.Connection
    Dim oRn As New ADODB.Recordset
    Dim strSQL As String

    oCn.Open "Driver={Microsoft Visual FoxPro Driver};" & _
           "SourceType=DBF;" & _
           "SourceDB=d:\pos;" & _
           "Exclusive=No"

    oRn.CursorLocation = adUseClient

    strSQL = "Select  * from MyData.dbf"
    oRn.Open strSQL, oCn, adReadOnly, adLockOptimistic

    Msgbox oRn.RecordCount

    oRn.Close
    Set oRn = Nothing
    oCn.Close
    Set oCn = Nothing
End Function
0
 
LVL 1

Author Comment

by:tcalbaz
ID: 18069381
leonstryker,

It appears that I got a similar result.  
When the code got to recordcount I got this Error:

"Data provider or other service returned an E_FAIL status"

I'm not sure why this is happening.  I'll try to find out a little more with google.

Thank you

Ted
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 18069415
I think the problem is with your connection string.  Take a look here:  

http://www.carlprothman.net/Default.aspx?tabid=87#OLEDBProviderForVisualFoxPro

Leon
0
 
LVL 1

Author Comment

by:tcalbaz
ID: 18070270
Leonstryker
I think your code is correct. I do think I have an issue with my drivers.
I'll work with this.

Thank you

Ted
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question