How do I access a .dbf file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wouterdeprez
    New Member
    • Mar 2008
    • 1

    How do I access a .dbf file?

    Hi,

    I want to acces a .dbf file with visual studio 2008 express edition, but it doesn't work.When I try to open the connection, i get the error message 'D:\temp\Mijn Documenten\SDT\ SDT Klantentool\SDT Klantentool\akt txt.dbf' is no valid path', but I'm sure it's the right directory. The .dbf file is a dBase file, but i don't know which version (I tried III, IV, and 5.0) but always the same error message.

    Here's the code:
    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
    con.ConnectionS tring = "PROVIDER=Micro soft.Jet.OLEDB. 4.0;Data Source=D:\temp\ Mijn Documenten\SDT\ SDT Klantentool\SDT Klantentool\akt txt.dbf;Extende d Properties=""dB ASE IV;"";"
    'Open the database
    con.Open() 'Here the error comes up!

    sql = "SELECT * FROM akttxt"
    da = New OleDb.OleDbData Adapter(sql, con)
    da.Fill(ds, "AktTxt")
    con.Close()
    MaxRows = ds.Tables("AktT xt").Rows.Cou nt
    inc = -1
    End Sub

    Please can someone help me with this, it's been giving me headaches for days now! If you want I can send the 'akttxt.dbf' file to you.

    best regards
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #2
    try this function:

    NOTE: IMPORTS SYSTEM.DATA.OLE DB

    [code=vbnet]
    Try
    '---- Imports System.Data.Ole Db - At top! ----
    '---Usage---
    'Dim oDS As New System.Data.Dat aSet
    'DBConn("c:\use rs\users.mdb", "select * from TBLUsers", oDS, DGV1, "table2")
    '---------
    Dim StrConn As String = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" & strSource
    Dim oConn As New System.Data.Ole Db.OleDbConnect ion(StrConn)
    Dim da As System.Data.Ole Db.OleDbDataAda pter
    da = New OleDbDataAdapte r(strQuery, oConn)
    da.Fill(dataset , tablename)
    'can change DGV - to the name of your DataGrid
    DGV.DataSource = dataset.Tables( tablename).Defa ultView
    DGV.Refresh()
    da = Nothing
    dataset = Nothing
    oConn = Nothing
    Catch
    MsgBox("Did not execute correctly")
    End Try
    [/code]

    usage:

    [code=vbnet]
    Dim oDS As New System.Data.Dat aSet
    DBConn("c:\user s\users.mdb", "select * from TBLUsers", oDS, DGV1, "table1")
    [/code]

    Comment

    • pureenhanoi
      New Member
      • Mar 2007
      • 175

      #3
      Originally posted by wouterdeprez
      Hi,

      I want to acces a .dbf file with visual studio 2008 express edition, but it doesn't work.When I try to open the connection, i get the error message 'D:\temp\Mijn Documenten\SDT\ SDT Klantentool\SDT Klantentool\akt txt.dbf' is no valid path', but I'm sure it's the right directory. The .dbf file is a dBase file, but i don't know which version (I tried III, IV, and 5.0) but always the same error message.

      Here's the code:
      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
      con.ConnectionS tring = "PROVIDER=Micro soft.Jet.OLEDB. 4.0;Data Source=D:\temp\ Mijn Documenten\SDT\ SDT Klantentool\SDT Klantentool\akt txt.dbf;Extende d Properties=""dB ASE IV;"";"
      'Open the database
      con.Open() 'Here the error comes up!

      sql = "SELECT * FROM akttxt"
      da = New OleDb.OleDbData Adapter(sql, con)
      da.Fill(ds, "AktTxt")
      con.Close()
      MaxRows = ds.Tables("AktT xt").Rows.Cou nt
      inc = -1
      End Sub

      Please can someone help me with this, it's been giving me headaches for days now! If you want I can send the 'akttxt.dbf' file to you.

      best regards
      I think you better create an ODBC DataSource, an point it to your DBF file
      To Create ODBC DataSource:
      Open Control Panel > Administrative Tools > ODBC DataSource
      On the System DSN tab, click Add
      Select "Driver do Mircosoft dBase" driver, and then Finish
      Now, name the datasource is AKTTXT, select the right version for dBase file (you can change version latter if it does not correct)
      Uncheck the "Use current Directory", and click "Select Directory" to select the folder that containing your dBase file. Finally, click Ok to complete

      In VB project, simply use
      con.ConnectionS tring ="AKTTXT"
      con.Open

      That "AKTTXT" is Datasource name you've made above
      If it does not work correctly, try Change dBase version by edit AKTTXT DataSource
      With ODBC Data Source, you can access more than one DBF file. Each file is now a Table (like table in MS-Access). File name is the table name (ofcoz, table name have no extension). If you want access another DBF file, simply move this DBF file to the Directory you've selected in ODBC DataSource

      Comment

      Working...