Solved

Connecting to FoxPro database with ADO

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now