Results 1 to 6 of 6
  1. #1
    hnhpak is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jan 2015
    Location
    Karachi Pakistan
    Posts
    28

    Define Record source in vba module public function

    I have a following module for backup backend file on open front end:
    ------------------------------------------------

    Public Function BackupOnOpen()


    ' Ensure you have a \ on the end of the pathname.
    Const BACKUP_PATH = "D:\Database\Backup\"


    On Error GoTo BackupOnOpen_Err


    If DCount("BackupDate", "BackupDetails", "BackupDate = date()") <> 0 Then
    Exit Function
    End If


    Dim strSourcePath As String
    Dim strSourceFile As String
    Dim strBackupFile As String


    strSourcePath = "D:\Database\BackEnd Data\"
    strSourceFile = "BE_FileName.accdb"



    strBackupFile = "BackupDB-" & Format(Date, "yyyy-mm-dd") _
    & "_" & Format(Time, "hhmmss") & "-" & strSourceFile


    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile strSourcePath & strSourceFile, BACKUP_PATH & strBackupFile, True
    Set fso = Nothing


    DoCmd.SetWarnings False
    Dim SQL As String
    SQL = "INSERT INTO BackupDetails " _
    & "(BackupDate, ComputerName, BackupFolder, Filename) " _
    & "VALUES ('" & Date & "', '" & Environ("COMPUTERNAME") _
    & "', '" & BACKUP_PATH & "', '" & strBackupFile & "');"
    DoCmd.RunSQL SQL
    SQL = "DELETE * FROM BackupDetails WHERE BackupDate < date() - 30;"
    DoCmd.RunSQL SQL
    DoCmd.SetWarnings True
    BackupOnOpen_Exit:
    Exit Function
    BackupOnOpen_Err:
    MsgBox Err.Description, , "BackupOnOpen()"
    Resume BackupOnOpen_Exit
    End Function

    --------------------------------------------
    This code works perfect, it want to make a little changes:

    1. TheLine:
    Const BACKUP_PATH = "E:\Database\Backup\" want to replace the line as under:
    Const BACKUP_PATH = [Company Details].[BackupPath]

    Whereas [Company Detail] is a table and [BackupPath] is the field where the path E:\Database\Backup\ is stored

    2. strSourcePath = "D:\Database\BackEnd Data\"
    strSourceFile = "BE_FileName.accdb"


    Want to replace above two lines as under:
    strSourcePath = [Company Details].[BE_Path]
    strSourceFile = [Company Details].[BE_FileName]

    Whereas [Company Detail] is a table and [BE_Path] is the field where the path D:\Database\BackEnd Data\ is stored and
    [BE_FileName] is the field where the File Name BE_FileName.accdb is stored

    All this I need to control and make changes of the BE Table Path and file and Backup Path through table instead of changes in module.

    Module shows compile error on Table Name, As what I understand is due to the reason that the record source is not define here, this is public function module not a form or report, where the record source is define in properties. I want to know how to define record source in vba module public function.

    Any help in modification the above code is highly appreciated.

    Thanks in Advance

    Regards

    Haseeb

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    try - you don't need the string assignments

    Code:
    dim rst as dao.recordset
    
    set rst=currentdb.openrecordset(SELECT BackupPath, BE_Path, BE_FileName FROM [Company Details]) 'assumes this is a one record table, otherwise you will need to add a criteria
    if not rst.eof then
        fso.CopyFile rst!BackupPath & rst!BE_Path, rst!BackupPath & strBackupFile, True
    end if
    set rst=nothing

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can pass variables to your function by adding arguments to its parameters.

    Code:
    Public Function BackupOnOpen(strSourcePath As String, strSourceFile As String)
    Because you have these variables declared in the parameters, you do not need them declared within the function. So comment out the two you do not need declared.

    Code:
    'Dim strSourcePath As String
     'Dim strSourceFile As String
     Dim strBackupFile As String
    Now, when you call your function, you can define the parameters by adding the values to the call.
    Call BackupOnOpen("D:\Database\BackEnd Data\", "BE_FileName.accdb")


    Or you can pass a new variable into the call
    Call BackupOnOpen(strMyPath, strMyFile)

  4. #4
    hnhpak is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jan 2015
    Location
    Karachi Pakistan
    Posts
    28
    Code reflects some compile error on the line rst=currentdb.openrecordset(SELECT

    Trying to figure out the error, if you found error let me know.

  5. #5
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    sorry - missed the quotation marks - also for future reference 'some compile error' says nothing - report the error description as well

    set rst=currentdb.openrecordset("SELECT BackupPath, BE_Path, BE_FileName FROM [Company Details]")

  6. #6
    hnhpak is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jan 2015
    Location
    Karachi Pakistan
    Posts
    28
    Thanks a lot Ajaz, It works.

    I am now marking this as solved...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Public variable not updating for each function
    By Ruegen in forum Programming
    Replies: 3
    Last Post: 08-28-2014, 06:26 PM
  2. Pass DAO.Recordset into public function
    By Ruegen in forum Programming
    Replies: 1
    Last Post: 07-07-2014, 12:31 AM
  3. Turning Private Function Into Public
    By g4tv4life in forum Programming
    Replies: 1
    Last Post: 02-04-2014, 05:31 PM
  4. Making a Public Function more flexible
    By sjs94704 in forum Programming
    Replies: 2
    Last Post: 12-10-2012, 02:21 PM
  5. Replies: 9
    Last Post: 12-20-2010, 08:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums