Stuart McCall's Microsoft Access Pages - DAO

Home
Store a single value in a Table

Note that this code requires a reference to "Microsoft DAO 3.x", where x is the highest value on your system.
Public Function rsReplace(pNewData As Variant, pField As String, _
                          pTable As String, Optional pCriteria As String) As Variant
' Use Dlookup syntax to navigate to a record, return current
' value of specified field, and set it to pNewData
    On Error GoTo rsReplace_Err

    With CurrentDb.OpenRecordset("Select " & pField & " From " & pTable & _
                                 IIf(Len(pCriteria), " Where " & pCriteria, ""), _
                                 dbOpenDynaset)
        If Not .BOF Then
            rsReplace = .Fields(0)
            .Edit
            .Fields(0) = pNewData
            .Update
        End If
        .Close
    End With

rsReplace_Exit:
    Exit Function

rsReplace_Err:
    MsgBox Err.Description & " (" & Err.Number & ")"
    Resume rsReplace_Exit
End Function
Paste this Code into a standard module.

Usage:

OldValue = rsReplace(21, "[Order Qty]", _
                     "tblOrders", "[Order Number]=" & Me![txtNumber])

or ignore the return value:

rsReplace "Stuart McCall", "[Customer Name]", _
          "tblCustomers", "[Customer id]=" & Me![txtId])
Home Contents

Retrieve part of a linked table's connect property

Note that this code requires a reference to "Microsoft DAO 3.x", where x is the highest value on your system.
Public Function ConnectPart(tblName As String, part As String) As String
' Retrieves part of a table's connect property
' Zero-length string returned if part is missing/invalid
    Dim i As Integer, j As Integer
    Dim strConnect As String
    Const DELIMITER As String = ";"
    
    strConnect = CurrentDb().TableDefs(tblName).Connect
    i = InStr(1, strConnect, DELIMITER)
    If i = 0 Then Exit Function
    If Right$(strConnect, 1) <> DELIMITER Then strConnect = strConnect & DELIMITER
    
    If UCase$(part) = "TYPE" Then
        If i = 1 Then
            ConnectPart = "ACCESS"
        Else
            ConnectPart = Left$(strConnect, i - 1)
        End If
    Else
        i = InStr(1, strConnect, part)
        If i Then
            j = InStr(i + 1, strConnect, DELIMITER)
            If j Then
                i = i + Len(part) + 1
                ConnectPart = Mid$(strConnect, i, j - i)
            End If
        End If
    End If
End Function
Paste this Code into a standard module.

Usage:

ConnectPart("tblCustomers", "DATABASE")
 would return the path to the remote file
 
ConnectPart("tblCustomers", "DSN")
 would return the DSN name (ODBC link)
Home Contents

Determine the Access version or file format of an Access file

Note that this code requires a reference to "Microsoft DAO 3.x", where x is the highest value on your system.
Public Function AccessFileVersion(ByVal FileName$) As Long
'Returns Access file version number
'or zero on error.
    Dim ff$
    
    ff = AccessFileFormat(FileName)
    If ff Like "97*" Then
        AccessFileVersion = 8
    ElseIf ff Like "2000*" Then
        AccessFileVersion = 9
    ElseIf ff Like "2002*" Then
        AccessFileVersion = 10
    ElseIf ff Like "2007*" Then
        AccessFileVersion = 12
    End If
End Function

Public Function AccessFileFormat(ByVal FileName$) As String
'Returns "97 MDE", "2000 MDB", "2007 ACCDB" etc.
'or zero length string on error.
    Dim rtn$
    
    On Error GoTo Handler
    
    If FileName Like "*.accd?" Then
        rtn = "2007"
    Else
        With DAO.Workspaces(0).OpenDatabase(FileName)
            Select Case Int(Val(.Version))
                Case 3  '97 file format is 3.0
                    rtn = "97"
                Case 4  '2000 or 2002/3 file format is 4.0
                    Select Case .Properties("AccessVersion")
                        Case "08.50"    '2000 format
                            rtn = "2000"
                        Case "09.50"    '2002/3 format
                            rtn = "2002/3"
                    End Select
            End Select
            .Close
        End With
    End If
    
    rtn = rtn & " " & Mid$(FileName, InStrRev(FileName, ".") + 1)
    AccessFileFormat = rtn
ExitPoint:
    Exit Function
Handler:
    Select Case Err.Number
        Case 2482, 3270       'Object not found, Property not found
            Resume Next
        Case Else
            MsgBox Err.Description, vbCritical
            Resume ExitPoint
    End Select
End Function
Paste this Code into a standard module.

Usage:

    Debug.Print AccessFileVersion("c:\temp\test.mdb")
    Result: 9
    
    or:
    
    Debug.Print AccessFileFormat("c:\temp\test.mdb")
    Result: 2000 mdb
Home Contents