How to run a module?

I

Iram

I need to run the below API code at the click of a control button while I am
in the Access database. The code below is supposed to backup the currently
opened db.
How do I run it at the click of a control button?

On Click Event
Module name does not work...


Iram/mcp


'********** Code Start *************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Boolean
hNameMappings As Long
lpszProgressTitle As String
End Type

Private Const FO_MOVE As Long = &H1
Private Const FO_COPY As Long = &H2
Private Const FO_DELETE As Long = &H3
Private Const FO_RENAME As Long = &H4

Private Const FOF_MULTIDESTFILES As Long = &H1
Private Const FOF_CONFIRMMOUSE As Long = &H2
Private Const FOF_SILENT As Long = &H4
Private Const FOF_RENAMEONCOLLISION As Long = &H8
Private Const FOF_NOCONFIRMATION As Long = &H10
Private Const FOF_WANTMAPPINGHANDLE As Long = &H20
Private Const FOF_CREATEPROGRESSDLG As Long = &H0
Private Const FOF_ALLOWUNDO As Long = &H40
Private Const FOF_FILESONLY As Long = &H80
Private Const FOF_SIMPLEPROGRESS As Long = &H100
Private Const FOF_NOCONFIRMMKDIR As Long = &H200

Private Declare Function apiSHFileOperation Lib "Shell32.dll" _
Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) _
As Long

Function fMakeBackup() As Boolean
Dim strMsg As String
Dim tshFileOp As SHFILEOPSTRUCT
Dim lngRet As Long
Dim strSaveFile As String
Dim lngFlags As Long
Const cERR_USER_CANCEL = vbObjectError + 1
Const cERR_DB_EXCLUSIVE = vbObjectError + 2
On Local Error GoTo fMakeBackup_Err

If fDBExclusive = True Then Err.Raise cERR_DB_EXCLUSIVE

strMsg = "Are you sure that you want to make a copy of the database?"
If MsgBox(strMsg, vbQuestion + vbYesNo, "Please confirm") = vbNo Then _
Err.Raise cERR_USER_CANCEL

lngFlags = FOF_SIMPLEPROGRESS Or _
FOF_FILESONLY Or _
FOF_RENAMEONCOLLISION
strSaveFile = CurrentDb.Name
With tshFileOp
.wFunc = FO_COPY
.hwnd = hWndAccessApp
.pFrom = CurrentDb.Name & vbNullChar
.pTo = strSaveFile & vbNullChar
.fFlags = lngFlags
End With
lngRet = apiSHFileOperation(tshFileOp)
fMakeBackup = (lngRet = 0)

fMakeBackup_End:
Exit Function
fMakeBackup_Err:
fMakeBackup = False
Select Case Err.Number
Case cERR_USER_CANCEL:
'do nothing
Case cERR_DB_EXCLUSIVE:
MsgBox "The current database " & vbCrLf & CurrentDb.Name &
vbCrLf & _
vbCrLf & "is opened exclusively. Please reopen in
shared mode" & _
" and try again.", vbCritical + vbOKOnly, "Database copy
failed"
Case Else:
strMsg = "Error Information..." & vbCrLf & vbCrLf
strMsg = strMsg & "Function: fMakeBackup" & vbCrLf
strMsg = strMsg & "Description: " & Err.Description & vbCrLf
strMsg = strMsg & "Error #: " & Format$(Err.Number) & vbCrLf
MsgBox strMsg, vbInformation, "fMakeBackup"
End Select
Resume fMakeBackup_End
End Function

Private Function fCurrentDBDir() As String
'code courtesy of
'Terry Kreft
Dim strDBPath As String
Dim strDBFile As String
strDBPath = CurrentDb.Name
strDBFile = Dir(strDBPath)
fCurrentDBDir = left(strDBPath, InStr(strDBPath, strDBFile) - 1)
End Function

Function fDBExclusive() As Integer
Dim db As Database
Dim hFile As Integer
hFile = FreeFile
Set db = CurrentDb
On Error Resume Next
Open db.Name For Binary Access Read Write Shared As hFile
Select Case Err
Case 0
fDBExclusive = False
Case 70
fDBExclusive = True
Case Else
fDBExclusive = Err
End Select
Close hFile
On Error GoTo 0
End Function
'************* Code End ***************
 
D

Douglas J. Steele

Why do you feel you need to backup the currently opened database? While
Dev's code is capable of backing it up, that doesn't mean it's a good idea.
You cannot be sure that the database isn't in the midst of saving data, so
that the copy conceivably can be in an inconsistent state. As far as I'm
concerned, you're better off not having a backup than having a backup that's
incorrect!

Besides, all applications should be split into a front-end (containing the
queries, forms, reports, macros and modules), linked to a back-end
(containing the tables and relations). Since you should always have a
back-up copy of the front-end, only the back-end needs to be backed up on a
regular basis, and there's no problem using FileCopy to copy the back-end as
long as no active connections exist to it. The front-end can be used to
issue that FileCopy command.
 
I

Iram

Doug.
Thanks for your response. I just looked into the Filecopy command and I
can't get it to backup my Back End DB.
I got these instructions from the MSDN website
http://msdn.microsoft.com/en-us/library/bwfbss93(VS.80).aspx

' Copy the file to a new location without overwriting existing file.
My.Computer.FileSystem.CopyFile( _
"C:\UserFiles\TestFiles\testFile.txt", _
"C:\UserFiles\TestFiles2\testFile.txt")

However when I put the REAL paths into this code (see below), everything
from My.computer... to the last LINE turns RED. Can you tell me why? Btw, I
am putting this vba on a button on my form that is on my Front End.

Private Sub Command92_Click()
' Copy the file to a new location without overwriting existing file.
My.Computer.FileSystem.CopyFile( _
"I:\Shared\DCSS Task Tracking Program\DB Administration\Back End Task
Tracker\DCSS IT Task Tracker_BackEnd.mdb", _
"I:\Shared\DCSS Task Tracking Program\DB Administration\Back End Task
Tracker\BE Backups\DCSS IT Task Tracker_BackEnd.mdb")



Thanks.
Iram/mcp
 
D

Douglas J. Steele

The reference you've cited is VB.Net, not VBA, and therefore won't work in
Access.

You say you can't get the FileCopy command to back up your back-end
database. What happens when you try? (FWIW, I do it all the time...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top