Solved

ODBC, DSNLess and a dBase database

Posted on 2000-05-02
9
876 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 69

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 69

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
 

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 69

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 69

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 69

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

863 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

22 Experts available now in Live!

Get 1:1 Help Now