Source code for Issue Number 62

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste create a new module in Access and then paste this source code into it.

'----------------------------------------------------------------------
'
'   Module Name:    basAccess
'   Written By:     C&D Programming Corp.
'   Create Date:    11/98
'   Copyright:      Copyright 1998 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit

Public Function GetNextValue(sTable As String, sField as String) As Long
    Dim RS As Recordset
    Dim db As Database

    ' get currently opened database
    Set db = CurrentDb
    ' open counter record set
    '   The Counter table has a single field called CounterField defined as a Long Integer
    Set RS = db.OpenRecordset("select * from " & sTable, dbOpenDynaset)
    ' If there are no records, create one and initialize the counter
    If RS.RecordCount = 0 Then
        RS.AddNew
        RS.Fields(sField) = 1
    Else
        RS.Edit
        RS.Fields(sField) = RS.Fields(sField) + 1
    End If
    GetNextValue = RS.Fields(sField)
    RS.Update
    Set RS = Nothing
End Function

Public Function PadRight(sData As String, lLength As Long) As String
    PadRight = Mid$(sData & String$(lLength, " "), 1, lLength)
End Function

Public Function PadLeft(sData As String, lLength As Long) As String
    If Len(sData) > lLength Then
        PadLeft = Left(sData, lLength)
    Else
        PadLeft = String$(lLength - Len(sData), " ") & sData
    End If
End Function