Dairy Farmer
Registered User.
- Local time
- Tomorrow, 01:42
- Joined
- Sep 23, 2010
- Messages
- 244
I have a startup form that calls 3 function
These check that the 3 BE files exist.
The Module is as below, but has 3 identical functions. Only the blue text is different:
How could I reuse the same code for all three functions only changing Farm?
Or should I be looking at having the code checking all three files in one shot?
Code:
Private Sub Form_Open(Cancel As Integer)
Farm1File
Farm2File
FarmsFile
End Sub
The Module is as below, but has 3 identical functions. Only the blue text is different:
Code:
Function [COLOR="blue"]Farm1File[/COLOR]()
Dim Msg, Style, Title, Response, MyString
Dim pubInputFolder As String
Dim txtfolder As String
Dim fs As Object
Dim backupsource As String
Dim destination As String
Dim Farm As String
Dim DataPath As String
DataPath = CurrentProject.Path & "\Data\"
[COLOR="Blue"]Farm = "Farm_1.accdb"[/COLOR]
If Dir(DataPath & Farm) = "" Then
Msg = ("You are missing the data file " & Farm & "." & vbNewLine & _
vbNewLine & _
"Would you like to restore data from a previous backup?")
Style = vbYesNo + vbCritical + vbDefaultButton1
Title = "Missing Data"
Response = MsgBox(Msg, Style, Title)
If Response = vbNo Then
MyString = "Cancel"
DoCmd.Quit
Else
If Response = vbYes Then
MyString = "Yes"
Const conPATH_FILE_ACCESS_ERROR = 75
On Error GoTo 0
With Application.FileDialog(4)
.InitialFileName = CurrentProject.Path & "\Backups\"
.AllowMultiSelect = False
.Filters.Clear
.Show
pubInputFolder = .SelectedItems(1)
txtfolder = pubInputFolder
End With
backupsource = txtfolder
destination = CurrentProject.Path & "\Data\"
On Error GoTo Restore
MkDir destination
Resume Restore
Restore:
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile backupsource & "\" & Farm, destination & "\"
Set fs = Nothing
MsgBox "File " & Farm & " from " & vbNewLine _
& backupsource & vbNewLine & _
"successfully restored!" & vbNewLine & _
vbNewLine & _
"Please re-open Dairy Manager.", vbInformation, "Restore Successful"
DoCmd.Quit
End
End If
End If
End If
End Function
How could I reuse the same code for all three functions only changing Farm?
Or should I be looking at having the code checking all three files in one shot?