Solved

Connecting to FoxPro database with ADO

Posted on 2006-12-01
5
382 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
  • 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

773 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