简体   繁体   中英

Reading a value, from a row, in a table, in an mdb file , from excel/vba?

I want to make a simple function that will open and read from a database (and mdb file). As simply and cleanly as possible. Preferably using only ADODB.

For now I need this from excel/vba and I will later migrate to vb.net

First the structure of my database

A single mdb file (actually, accdb, it doesn't matter I hope)

It has a single table called "myParts"

This table has 3 columns: id, part number, part description

Here is how the function I want to make

function GetPartDescription (PartNumber as string) as string

The part number should exist only once in the entire table.

So this function should, open the database, find the row with the exact matching part number and then return whatever is in the "part description" column for that row

How should I do this ? I tried getting started by just choosing which api, I get lost ! DAO , ADO, ACEDAO, ADODB, ADO.NET, OLEDB ??? What a nightmare !

IMO this question should be closed as too broad but let's give it a try The following function will connect to a Access database via ADODbD

Function ConnectToDB(ByVal fileName As String)

Dim conn As New ADODB.Connection

    If Dir(fileName) = "" Then
        MsgBox "Could not find file " & fileName
        Exit Function
    End If

    Dim connectionString As String

    ' https://www.connectionstrings.com/access/
    connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" _
                       & fileName & ";Persist Security Info=False;"

    conn.Open connectionString

    Set ConnectToDB = conn

End Function

And this might give you what you want. You need a sheet with the codename shRepAllRecords to make it work.

Option Explicit

Sub ReadFromDB()

    ' Get datbase name
    Dim dbName As String
    dbName = <fule filename of the database>

    ' Connect to the databse
    Dim conn As ADODB.Connection
    Set conn = ConnectToDB(dbName)

    ' read the data
    Dim rs As New ADODB.Recordset
    Dim query As String

    ' First example to use an SQL statement
   query = "SELECT * From myParts WHERE PartNumber = '123'"

    ' Second example to use a query name defined in the database itself
    ' query = "qryCustomer"

    rs.Open query, conn

    ' shRepAllRecords is the codename of the sheet where the 
    ' data is written to

    ' Write header
    Dim i As Long
    For i = 0 To rs.Fields.Count - 1
        'shRepAllRecords.Cells(1, i + 1).Value = rs.Fields(i).Name
        shRepAllRecords.Range("A1").Offset(0, i) = rs.Fields(i).Name
    Next i

    ' Write Data
    shRepAllRecords.Range("A2").CopyFromRecordset rs
    shRepAllRecords.Activate


    ' clean up
    conn.Close

End Sub

You need to adjust the code in order to get excatly what you need but I leave that to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM