UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
2 Pages V  1 2 >  (Go to first unread post)
   Reply to this topicStart new topic
> Saving A Sequential Backup, visual Studio 2017    
 
   
mdelke@chartermi...
post Mar 27 2020, 04:07 PM
Post#1



Posts: 11
Joined: 26-March 20



Hello everyone,
I am having a problem saving my file that I have added a sequential number to as a backup. I got the code for adding a sequential number from this web site
this is the code:

Public Class BackupDataBase
Public Function GenerateSequentialFileName(ByVal strPath As String,
ByVal strBaseName As String,
ByVal strExtension As String,
Optional ByVal intMinSequence As Object = 1,
Optional ByVal strSeparator As String = "-BACKUP-",
Optional ByVal byteFormatDigits As Object = 4) As String

'THIS FUNCTION RETURNS THE NEXT FILE NAME IN SEQUENCE, USING A SCHEME WHERE A SEQUENTIAL NUMBER IS ADDED TO THE FILE NAME BEFORE THE EXTENSION
'IF FILE NUMBERS ARE MISSING IN SEQUENCE THE FUNCTION RETURNS THE NEXT ONE AFTER THE CURRENT HIGHEST NUMBER
'USAGE EXAMPLE: GENERATEsEQUENTIALfILEnAME("C:\RRLOGGER DATA\","COUNTY HUNTER - K8EMS","MDB",1,"-BACKUP-",4) THIS WOULD
'GENERATE A FILE NAME FORMATTED LIKE "C:\RRLOGGER DATA\COUNTY HUNTER - K8EMS-BACKUP-0001.MDB"
'PARAMETERS:
' strPath: FULL PATH NAME INCLUDING SLASH
' strBaseName: FILENAME
' strExtension: FILE EXTENSION WITH NO DOT(.)
' intMinSequence: STARTING SEQUENCE NUMBER, USED IF NO FILES ALREADY EXSIST (OPTIONAL, DEFAULT 1)
' strSeparator: STRING USED BETWEEN THE BASE FILE NAME AND THE NUMBER (OPTIONAL DEFAULT IS '-BACKUP-')
' byteFormatDigits: NUMBER OF DIGITS TO FORMAT THE NUMBER TO (OPTIONAL DEFAULT IS 4)
' THIS CODE WAS FOUND ON UtterAccess.COM WRITTEN BY CLIPPIT

On Error GoTo ErrorHandler
Dim strFilePattern As String
Dim strThisFile As String
Dim intMaxSequenceNumberFound As Integer
Dim intSequenceNumber As Integer
Dim strNumberPlusPart As String
Dim strNumberPart As String

intMaxSequenceNumberFound = -1

strFilePattern = strPath & strBaseName & strSeparator & "*" & "." & strExtension
strThisFile = Dir(strFilePattern) ' get the first file matching the pattern
Do Until strThisFile = "" ' loop thru all files matching the pattern
' parse the file name to find the sequence number if there is one
strNumberPlusPart = Mid(strThisFile, Len(strBaseName & strSeparator) + 1, 99)
If Len(strNumberPlusPart) = Len("." & strExtension) Then
strNumberPart = ""
Else
strNumberPart = Left(strNumberPlusPart, Len(strNumberPlusPart) - Len("." & strExtension))
End If
If IsNumeric(strNumberPart) Then
If Val(strNumberPart) > intMaxSequenceNumberFound Then
intMaxSequenceNumberFound = CInt(Val(strNumberPart))
End If
End If
strThisFile = Dir() ' get the next file matching the pattern
Loop

If intMaxSequenceNumberFound = -1 Then ' -1 indicates no files were found
intSequenceNumber = CInt(intMinSequence)
Else
intSequenceNumber = intMaxSequenceNumberFound + 1

End If

GenerateSequentialFileName = strPath & strBaseName & strSeparator &
Format(intSequenceNumber, Left("000000", CInt(byteFormatDigits))) & "." & strExtension


ErrorHandler:
Debug.Print("Function GenerateSequencialFileName encountered error" & Err.Description & "(#" & Err.Number & ")")
On Error Resume Next
GenerateSequentialFileName = ""



End Function



End Class

I pass it the variables....... _BackupDataBase.GenerateSequentialFileName("C:\RRLogger Data\", "County Hunter - K8EMS", "mdb", 1, "-Logger Two BACKUP-", 4)

I get no errors but the file isn't written. I know it is probably a stupid question but my brain just isn't wrapping around the problem. Any help would be appreciated I am just a retired person trying to learn Visual basic to use my spare time.

Mike ( Retired Paramedic)
Go to the top of the page
 
theDBguy
post Mar 27 2020, 04:52 PM
Post#2


UA Moderator
Posts: 78,120
Joined: 19-June 07
From: SunnySandyEggo


Hi Mike. Welcome to UtterAccess! welcome2UA.gif

Have you tried stepping through the code to see what is actually happening.

There might be an easier approach. I don't use sequential numbering but use the current date/time instead (can go up to the seconds). Just a thought...

You can check it out here.

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
DanielPineault
post Mar 27 2020, 06:24 PM
Post#3


UtterAccess VIP
Posts: 7,343
Joined: 30-June 11



Should that be a standard module rather than a class?

You may also like to take a peak at https://www.devhut.net/2018/02/22/ms-access...ing-a-vbscript/

--------------------
Daniel Pineault (2010-2019 Microsoft MVP, UA VIP, EE Distinguished Expert 2018)
Professional Help: https://www.cardaconsultants.com
Free MS Access Code, Tips, Tricks and Samples: https://www.devhut.net

* Design should never say "Look at me". It should always say "Look at this". -- David Craib
* A user interface is like a joke, if you have to explain it, it's not that good! -- Martin LeBlanc


All code samples, demonstration databases, links,... are provided 'AS IS' and are to be used at your own risk! Take the necessary steps to check, validate ...(you are responsible for your choices and actions)
Go to the top of the page
 
mdelke@chartermi...
post Mar 27 2020, 06:41 PM
Post#4



Posts: 11
Joined: 26-March 20



Thank you for your suggestion of using the date I have it doing that already but the users of the program say it is to confusing as to which one is the latest backup. The program they are using already has the backups numbered sequentially and that is what they are asking of me. So I am trying to get it working but having problems.
The users of this program, at least a majority are older and not very computer literate. Trying to make it so easy even a caveman could do it. ( Sorry Progressive)
This post has been edited by mdelke@chartermi.net: Mar 27 2020, 06:45 PM
Go to the top of the page
 
theDBguy
post Mar 27 2020, 07:18 PM
Post#5


UA Moderator
Posts: 78,120
Joined: 19-June 07
From: SunnySandyEggo


I see. Again, though, stepping through the code should help you trace the cause of the problem, so you can fix it.

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
DanielPineault
post Mar 27 2020, 07:25 PM
Post#6


UtterAccess VIP
Posts: 7,343
Joined: 30-June 11



The original code can be found at http://www.UtterAccess.com/forum/index.php?showtopic=455115

Also note, I could always be wrong, but this function simply generate the new file name, it doesn't actually perform the backup. So you still need to use FSO, FileCopy, ... or some other technique to perform the actual backup.

--------------------
Daniel Pineault (2010-2019 Microsoft MVP, UA VIP, EE Distinguished Expert 2018)
Professional Help: https://www.cardaconsultants.com
Free MS Access Code, Tips, Tricks and Samples: https://www.devhut.net

* Design should never say "Look at me". It should always say "Look at this". -- David Craib
* A user interface is like a joke, if you have to explain it, it's not that good! -- Martin LeBlanc


All code samples, demonstration databases, links,... are provided 'AS IS' and are to be used at your own risk! Take the necessary steps to check, validate ...(you are responsible for your choices and actions)
Go to the top of the page
 
cheekybuddha
post Mar 27 2020, 07:34 PM
Post#7


UtterAccess Moderator
Posts: 12,814
Joined: 6-December 03
From: Telegraph Hill


What language are you coding in?

--------------------


Regards,

David Marten
Go to the top of the page
 
mdelke@chartermi...
post Mar 28 2020, 09:31 AM
Post#8



Posts: 11
Joined: 26-March 20



I am using Visual Basic in Visual Studio 2017.
Go to the top of the page
 
mdelke@chartermi...
post Mar 28 2020, 09:36 AM
Post#9



Posts: 11
Joined: 26-March 20



Dan
I have come to that conclusion. But not sure how to take the created sequential file and put it to use. I use at this time copyfile and append the date to it, but using this generated file name has me baffled.
Thanks for the help.
Go to the top of the page
 
cheekybuddha
post Mar 28 2020, 09:39 AM
Post#10


UtterAccess Moderator
Posts: 12,814
Joined: 6-December 03
From: Telegraph Hill


>> I use at this time copyfile and append the date to it, but using this generated file name has me baffled. <<

Can you show your code that does this.

d

--------------------


Regards,

David Marten
Go to the top of the page
 
mdelke@chartermi...
post Mar 28 2020, 01:44 PM
Post#11



Posts: 11
Joined: 26-March 20



David
The code that creates the sequential number is in the original post, it is error free as far as Visual Studio is concerned. The problem is taking the file that is created in this function and saving it to the folder.

The original code can be found at http://www.UtterAccess.com/forum/index.php?showtopic=455115
This post has been edited by mdelke@chartermi.net: Mar 28 2020, 01:47 PM
Go to the top of the page
 
cheekybuddha
post Mar 28 2020, 01:48 PM
Post#12


UtterAccess Moderator
Posts: 12,814
Joined: 6-December 03
From: Telegraph Hill


Hi Mike,

The function just returns a string containing the filename you wish to use.

You need more code to copy the existing file to a new one and give it the generated string as its name.

--------------------


Regards,

David Marten
Go to the top of the page
 
DanielPineault
post Mar 28 2020, 02:14 PM
Post#13


UtterAccess VIP
Posts: 7,343
Joined: 30-June 11



Using
CODE
Public Function GenerateSequentialFileName( _
       ByVal strPath As String, _
       ByVal strBaseName As String, _
       ByVal strExtension As String, _
       Optional ByVal intMinSequence = 1, _
       Optional ByVal strSeparator As String = "v", _
       Optional ByVal byteFormatDigits = 2 _
       ) As String
' this function returns the next file name in sequence, using a scheme where a sequential number
'   is added to the file name before the extension
'   if file numbers are missing in sequence the function returns the next one after the current
'   highest number
' usage example: GenerateSequentialFileName("C:\temp\","outdata","txt",1,"_",4)
'   this example generates a file name formatted like "C:\temp\outdata_0001.txt"
' parameters:
'  strPath: full path name, including slash
'  strBaseName: file name
'  strExtension: file extension, (no .)
'  intMinSequence: starting sequence number, used if no files already exist (optional, default 1)
'  strSeparator: string used between the base file name and the number (optional, default 'v',
'    for no separator use ""
'  byteFormatDigits: number of digits to format the number to (optional, default 2)
'
' Created by "Clippit" for www.UtterAccess.com
    On Error GoTo ErrorHandler
    Dim strFilePattern        As String
    Dim strThisFile           As String
    Dim intMaxSequenceFound   As Integer
    Dim intSequenceNumber     As Integer
    Dim strNumberPlusPart     As String
    Dim strNumberPart         As String

    intMaxSequenceFound = -1

    strFilePattern = strPath & strBaseName & strSeparator & "*" & "." & strExtension
    strThisFile = Dir(strFilePattern)    ' get the first file matching the pattern
    Do Until strThisFile = ""    ' loop through all files matching pattern
        ' parse the file name to find the sequence number if there is one
        strNumberPlusPart = Mid(strThisFile, Len(strBaseName & strSeparator) + 1, 99)
        If Len(strNumberPlusPart) = Len("." & strExtension) Then
            strNumberPart = ""
        Else
            strNumberPart = Left(strNumberPlusPart, Len(strNumberPlusPart) - Len("." & strExtension))
        End If
        If IsNumeric(strNumberPart) Then
            If Val(strNumberPart) > intMaxSequenceFound Then
                intMaxSequenceFound = Val(strNumberPart)
            End If
        End If
        strThisFile = Dir    ' get the next file matching the pattern
    Loop

    If intMaxSequenceFound = -1 Then    ' -1 indicates no files were found
        intSequenceNumber = intMinSequence
    Else
        intSequenceNumber = intMaxSequenceFound + 1
    End If

    GenerateSequentialFileName = strPath & strBaseName & strSeparator & _
                                 Format(intSequenceNumber, Left("000000", byteFormatDigits)) & "." & strExtension
EndFunction:
    Exit Function
ErrorHandler:
    Debug.Print "Function GenerateSequentialFileName encoutered error " & Err.Description & "(#" & Err.Number & ")"
    On Error Resume Next
    GenerateSequentialFileName = ""
    GoTo EndFunction
End Function

You could do something along the lines of
CODE
Public Sub BackupDB()
    Dim sSourceFile As String
    Const sPath = "C:\RRLogger Data\"
    Const sFilename = "County Hunter - K8EMS"
    Const sExtension = "mdb"
    Const sSeparator = "-Logger Two BACKUP-"

    sSourceFile = sPath & sFilename & "." & sExtension
    FileCopy sSourceFile, GenerateSequentialFileName(sPath, sFilename, sExtension, 1, sSeparator, 4)
End Sub

Obviously, it needs to have error handling, and test for the existance of the file first before trying to use FileCopy, ... but the concept is there.

--------------------
Daniel Pineault (2010-2019 Microsoft MVP, UA VIP, EE Distinguished Expert 2018)
Professional Help: https://www.cardaconsultants.com
Free MS Access Code, Tips, Tricks and Samples: https://www.devhut.net

* Design should never say "Look at me". It should always say "Look at this". -- David Craib
* A user interface is like a joke, if you have to explain it, it's not that good! -- Martin LeBlanc


All code samples, demonstration databases, links,... are provided 'AS IS' and are to be used at your own risk! Take the necessary steps to check, validate ...(you are responsible for your choices and actions)
Go to the top of the page
 
DanielPineault
post Mar 28 2020, 04:03 PM
Post#14


UtterAccess VIP
Posts: 7,343
Joined: 30-June 11



I was thinking, I'm not sure if you have FileCopy natively in VB, so perhaps you would need to use a custom function like
CODE
'REF: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/copyfile-method
Public Function FSO_CopyFile(ByVal sSource As String, _
                             ByVal sDestination As String, _
                             Optional bOverwrite As Boolean = False) As Boolean
    Dim fso                   As Object
    Dim oFSO                  As Object

    On Error GoTo Error_Handler

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    oFSO.CopyFile sSource, sDestination, bOverwrite
    FSO_CopyFile = True

Error_Handler_Exit:
    On Error Resume Next
    If Not oFSO Is Nothing Then Set oFSO = Nothing
    Exit Function

Error_Handler:
'Some of the errors that can occurs and perhaps you wish to code for
'*******************************************************************
'53 - file not found
'58 - destination file already exists
'70 - permission denied
'76 - folder not found (either source or destination)
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: FSO_CopyFile" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function


and then do
CODE
Public Sub BackupDB()
    Dim sSourceFile As String
    Const sPath = "C:\RRLogger Data\"
    Const sFilename = "County Hunter - K8EMS"
    Const sExtension = "mdb"
    Const sSeparator = "-Logger Two BACKUP-"

    sSourceFile = sPath & sFilename & "." & sExtension
    FSO_CopyFile sSourceFile, GenerateSequentialFileName(sPath, sFilename, sExtension, 1, sSeparator, 4), True '*** You decide about the True/False here though ***
End Sub

--------------------
Daniel Pineault (2010-2019 Microsoft MVP, UA VIP, EE Distinguished Expert 2018)
Professional Help: https://www.cardaconsultants.com
Free MS Access Code, Tips, Tricks and Samples: https://www.devhut.net

* Design should never say "Look at me". It should always say "Look at this". -- David Craib
* A user interface is like a joke, if you have to explain it, it's not that good! -- Martin LeBlanc


All code samples, demonstration databases, links,... are provided 'AS IS' and are to be used at your own risk! Take the necessary steps to check, validate ...(you are responsible for your choices and actions)
Go to the top of the page
 
FrankRuperto
post Mar 28 2020, 04:56 PM
Post#15



Posts: 980
Joined: 21-September 14
From: Tampa, Florida USA


Every Windows box has dotnet framework, so you could compile the following vb.net FolderCopy program into an executable and call it from vba, passing the source and sequential filename backup destination paths. This will also provide the default windows progress bar dialog box when it executes:

CODE
Imports Microsoft.VisualBasic.FileIO
Imports System.Environment
Module Module1

    Public Sub Main()

        If GetCommandLineArgs().Count <> 3 Then
            Console.WriteLine("Missing from or to folder or too many values")
            Exit Sub
        End If

        Dim strFolder As String = GetCommandLineArgs(1)
        Dim strTolder As String = GetCommandLineArgs(2)

        My.Computer.FileSystem.CopyDirectory(strFolder, strTolder, UIOption.AllDialogs)

    End Sub

End Module



Attached File(s)
Attached File  CopySpeed.PNG ( 34.94K )Number of downloads: 0
 

--------------------
Currently supporting pawnbrokers that use my store management system developed with Access 2010 on Windows7. Experienced with Informix, Oracle & PostgreSQL db's.
Go to the top of the page
 
isladogs
post Mar 28 2020, 05:04 PM
Post#16


UtterAccess VIP
Posts: 2,311
Joined: 4-June 18
From: Somerset, UK


You can also use API code to do fast copying from Access with the same file copy dialog box

--------------------
Colin (Mendip Data Systems)
Website, email
Go to the top of the page
 
FrankRuperto
post Mar 28 2020, 08:56 PM
Post#17



Posts: 980
Joined: 21-September 14
From: Tampa, Florida USA


WinAPI call will also work, but having an executable also allows you to re-use it externally for different tasks. We use it to do automatic backups of an Access backend that lives in a Linux server VM, and other maintenance as well. You can create multiple shortcuts to it, batch files, plus throw it in windows task manager. Before the automatic backend backup runs, we disable access to the Linux VM where the Access backend lives. How do you manage checking if there's any open Access FE/BE lockfiles, gracefully closing all of them, and preventing any while backup is running?

BTW, is anyone familiar with SMB (Server Message Block) network protocol? We use an open source interop suite named SAMBA that allows us to seamlessly integrate Linux with Windows.
This post has been edited by FrankRuperto: Mar 28 2020, 09:31 PM

--------------------
Currently supporting pawnbrokers that use my store management system developed with Access 2010 on Windows7. Experienced with Informix, Oracle & PostgreSQL db's.
Go to the top of the page
 
AlbertKallal
post Mar 29 2020, 11:46 AM
Post#18


UtterAccess VIP
Posts: 3,065
Joined: 12-April 07
From: Edmonton, Alberta Canada


I had some code in VBA - used it for years.

I simply cut + pasted it into vb.net, it works the same - only one line of code was changed.

To use:

CODE
Dim strFile as string

strFile = "full path name and file Name "

strFile = GetServerFileNext(strFile)

'So, if the file name does NOT exist then the file name of course does not change. If the file does exist then a _1, then _2 etc. is added.

So you get this sequence:

C:\dataExport\test1.csv
C:\dataExport\test1-1.csv
C:\dataExport\test1-2.csv
C:\dataExport\test1-3.csv


The code is this:

CODE
    Public Function GetServerFileNext(strFileFullPath As String) As String

        Dim strF As String
        Dim i As Integer = 0

        ' check if file already exist - start increamnt -N to file name
        Do
            If i = 0 Then
                strF = strFileFullPath
            Else
                strF = Path.GetFileNameWithoutExtension(strFileFullPath) & "-" & i & Path.GetExtension(strFileFullPath)
            End If
            If File.Exists(strF) = False Then
                Exit Do
            End If
            i = i + 1
        Loop

        Return strF        ' return

    End Function


So above is just easy way to get/generate the next file name in the sequence. It has zero to do with the actual back up code, but it is a handy dandy file sequence routine that I used for years in Access. In fact the above comes from a asp.net project I have in use.

Albert D. Kallal (Access MVP 2003-2017)
Edmonton, Alberta Canada
Go to the top of the page
 
cheekybuddha
post Mar 29 2020, 12:19 PM
Post#19


UtterAccess Moderator
Posts: 12,814
Joined: 6-December 03
From: Telegraph Hill


Hi Albert,

CODE
' ...
            If File.Exists(strF) = False Then
' ...


Is 'File' a built-in class in vb.net, or does it have to be imported? If so, what's the import look like?

[ edit: also same question re. 'Path' class ? ]

tia,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
FrankRuperto
post Mar 29 2020, 12:22 PM
Post#20



Posts: 980
Joined: 21-September 14
From: Tampa, Florida USA


Albert,

Niether of those two Get methods verify that the path or file name exists and your code is just exiting the loop without writing a console message if not exists. Is a default dialog displayed when running this code or do you have to explicitly tell it to display AllDialogs?
This post has been edited by FrankRuperto: Mar 29 2020, 12:23 PM

--------------------
Currently supporting pawnbrokers that use my store management system developed with Access 2010 on Windows7. Experienced with Informix, Oracle & PostgreSQL db's.
Go to the top of the page
 
2 Pages V  1 2 >


Custom Search


RSSSearch   Top   Lo-Fi    29th May 2020 - 09:46 PM