VB6 - ODBC connection problem

Posted on 2009-02-10
Last Modified: 2013-12-20
I have a vb6 project, with a command button and a listbox on a form. I have linked the refference DAO 3.6 to the project. I want co connect to an access database and receive some records from there via ODBC.
Now i have the following code attached to Command1:

Option Explicit
Private Sub Command1_Click()
    Dim ws As Workspace
    Dim db As Database
    Dim strConnection As String
    Dim rs As Recordset
    Dim DatabaseName As String
    Dim UserName As String
    Dim UserPassword As String
    DatabaseName = "c:\Firewall.mdb"
    UserName = ""
    UserPassword = ""
    Set ws = DBEngine.Workspaces(0)
    Let strConnection = "ODBC;DSN=" & DatabaseName & ";UID=" & UserName _
    & ";PWD=" & UserPassword
    Set db = ws.OpenDatabase("", False, False, strConnection)
    Set rs = db.OpenRecordset("tbl_Clienti")
    Do While Not rs.EOF
        'Put the code here for what to do with the information.
        'The field information can be access by the field name
        List1.AddItem rs!Nume_Client
        'Or by the order number it is in the list (starting at 0)
'        intString = rs.Field(1)
End Sub

On the line:      Set db = ws.OpenDatabase("", False, False, strConnection)
i get the following error message:  
Run-time error '3151'
ODBC -- connection to 'c:\firewall.mdb' failed

Can you help me?
Question by:Cristi_E
    LVL 4

    Author Comment

    Any ideeas?
    LVL 2

    Accepted Solution

    Try this line

    Set db = ws.OpenDatabase(DatabaseName)
    LVL 2

    Assisted Solution

    You might also want to double check to make sure the database exists and that the variables for the username and password contain what you think they should.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    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.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    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…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now