Solved

ODBC, DSNLess and a dBase database

Posted on 2000-05-02
9
887 Views
Last Modified: 2013-12-25
I'm trying to open a DBF file via ODBC without using a DSN entry. Is this possible? Does anyone have any example code of how to achieve this? What should the connection string look like? I'm using DAO 3.51 and a dBase IV database. Thanks in advance...
0
Comment
Question by:lbowers
  • 5
  • 4
9 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 2770286
You can create your DSN dynamically with DBEngine.RegisterDatabase.

Add this code to a class:
Option Explicit

Public glngError As Long        'Contient le code de l'erreur (s'il y en a une!)
Public gstrAttributes As String 'Contient la string d'attributs
Public gstrDriver As String     'Contient le nom du driver
Public gstrDSN As String        'Contient le nom du DSN

' *********************************************************
' *** Enregistre les données de l'ODBC dans le Registry ***
' *********************************************************
Public Function RegisterDB() As Boolean
Dim errLoop As Error

    On Error GoTo ErrorHandler
   
    RegisterDB = False
    DBEngine.RegisterDatabase gstrDSN, gstrDriver, True, gstrAttributes
    RegisterDB = True
   
    Exit Function
   
ErrorHandler:
    glngError = Err.Number
End Function

You can use it this way:
    Set objDSN = New clsRegisterDB
    With objDSN
        .gstrDriver = "Microsoft dBase Driver (*.dbf)"
       
        .gstrDSN = "FO-" & CStr(bytI)
        .gstrAttributes = "DefaultDir=" & "C:\test" & vbCrLf & _
"Description=Fichier du Front Office" & vbCrLf & _
                              "FIL=dBase 5.0;"
            .RegisterDB
    End With
0
 

Author Comment

by:lbowers
ID: 2772782
The above code works (thanks), but what is the ODBC connection string for dbf files? I notice that you don't actually specify a database within the DSN, just a directory. How do you tell VB the name of the database to use? I've tried loads of variations, but I keep gettings errors!
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2774718
Since dBase is not a model of a table containing tables, the folder becomes like a database and the files (tables) are specified in your queries.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:lbowers
ID: 2775184
Excellent - Thanks for your help. The points are yours, but I was wondering if you had ever come across the following problem before:

I am able to READ records via the ODBC connection, but I get an error whenever I attempt to DELETE, ADD or UPDATE records. I have set all "Read Only" properties to false. Any ideas?

Thanks again.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2775771
Show me your code.
0
 

Author Comment

by:lbowers
ID: 2776553
   Set ws = dao.CreateWorkspace("Workspace", "", "", dbUseODBC)
    Set db = ws.OpenConnection("Disk5Copy", dbDriverNoPrompt, False, "ODBC;")

sSQL = "DELETE FROM house" ' For example
db.Execute sSQL

I get "ODBC--call failed" error!
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2776735
You need to add you DSN name somewhere:
Set db = ws.OpenConnection("Disk5Copy", dbDriverNoPrompt, False, "ODBC;DSN=MYDSN")
0
 

Author Comment

by:lbowers
ID: 2776909
Disk5Copy is my DSN. I find if I use "DSN=xxx" in the connections string, it doesn't work. Using the code I've show above, I can read records from the DBF file, I just can't ADD, DELETE or UPDATE! Hmmm...
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 2777006
Why don't you open the dBase file directly (without going through ODBC).

http://support.microsoft.com/support/kb/articles/Q240/7/48.ASP?LN=EN-US&SD=gn&FR=0
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

813 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

15 Experts available now in Live!

Get 1:1 Help Now