automatically naming

D

dc

Does anyone know of a way to automatically name an exported table using a
field name? I would like to export tables nameing them like "meeting" and
the table name.
 
D

dc

I would like to set up either a macro or VBA to do it automatically when the
database closes. Is there a way to do this?
 
K

Ken Snell \(MVP\)

Please provide more details about what you want to do here. Are you
exporting to a text file? to an EXCEL spreadsheet? Which tables are to be
exported? How will the database know which ones to export? How will the
database know which "prefix word" to attach to a table name?
 
D

dc

Preferably into an access table but I could do an excel sheet if necessary.
The tables I want to copy are main data related like a speakers name and
their session. Its a form of backing up specific information in case of user
error. The macro or VBA would be built per table unless there is a way to
combine them.
 
K

Ken Snell \(MVP\)

You cannot export into a table per se. You can copy a table from one
database file to another, if that is what you mean?

You haven't told us how you want to trigger running the macros or code to do
this, once we identify exactly what you're wanting to export to where.

Also, perhaps if you explain more about *the purpose* for doing this export,
we might provide an alternative means for what you seek to do. Are you
wanting to make a backup copy of a file when it's closed? Or do you want to
send copies of data to someone else?
 
D

dc

That would be fine as well. I work with several different databases at one
time, along with several different novice users and have had problems with
stuff "disappearing". I am looking for ways to do a backup of these
databases (just specific tables are critical). My thought was to create a
database probably named "backup" on a secure drive other than our main drive
and when a user closes the database a macro or VBA would run copying the
newest data into specifc tables, i.e. "dbnamespeakers" or dbnamesessions".
We are running XP Pro here with office 2000 but I dont believe we have MSDE
or an SQL server.
 
K

Ken Snell \(MVP\)

It's probably not going to be as useful as you'd like if you try to make
copies of tables whenever someone closes the databasea -- I assume that
you're using a split database (front end and back end) so that many users
might be using the database at one time. Making copies of data while others
may be amending/editing the data is prone to a corrupt result.

Perhaps you would be better off if you just schedule a job to run on Windows
during "off" hours to make backup copies of the databases? That will give
you a regular backup of the data and avoid corruption problems?
 
D

dc

Yes its a split db and that would be great, when I first started looking into
doing this that was my intent. Is there a way to do that? Does it still run
through access? I only have rights to our databases and not the network and
any backups I do would have to be through the db.
 
K

Ken Snell \(MVP\)

Let's clarify the issues for this item, then.

1. You want to make automatic backups of the backend database.
2. You want to do this via Windows Scheduler.

Some additional questions:

1. Where are the backend database files located? In a shared folder on the
network?
2. Where do you want to put the backup copies?
3. Will you run the Windows Scheduler process from your PC at night? Or do
you want the ability to manually run the backup process by opening a file
that generates the backup?
 
D

dc

Thank you for all of your assistance on this, below are my answers to your
questions:

1. Where are the backend database files located? In a shared folder on the
network? and 2. Where do you want to put the backup copies?

Yes the copies would go into one database called backup data in which the
tables would be copied into on the "common" drive.

3. Will you run the Windows Scheduler process from your PC at night? Or do
you want the ability to manually run the backup process by opening a file
that generates the backup?

Both would be great actually.
 
K

Ken Snell \(MVP\)

Here is some code that I've pulled from one of my applications. It is
designed to run from a button on a form in the front end database, and makes
a copy of the backend database if all users other than the front end running
the code have disconnected from the backend. This would be useful for the
"manual" method.

You could run similar code via an AutoExec macro in a database file, where
the scheduler opens the file with the AutoExec macro in it. You'd need to
change UserCreateBackendBackup to a function; and then the AutoExec macro
would call the function via the RunCode action. But try this with the manual
setup first.

I took out a few things that are specific to that application, so it's
possible that you may get a compiler error the first time. Let me know if
you need assistance with this.
---------------------------

' ****************************************
' ** Subroutine UserCreateBackendBackup **
' ****************************************

Public Sub UserCreateBackendBackup()
' *** THIS SUBROUTINE IS USED TO ALLOW A USER TO CREATE A BACKUP COPY OF
' *** THE BACKEND DATABASE FILE IN ANOTHER LOCATION (e.g., ZIP DISC DRIVE,
' *** CD DRIVE, NETWORK LOCATION, etc.)
' Ken Snell 19 May 2005

Dim dbs_DB As DAO.Database
Dim datNowValue As Date
Dim xstrToLocation As String, strTempVar As String, strPathOfBE As String
Dim strPathFilenameOfBE As String, strFilenameOfBE As String
Dim tdf_DB As DAO.TableDef

On Error GoTo Err_CopyBackup

DoCmd.Hourglass True


Set dbs_DB = CurrentDb

' Get the path and filename of the "backend" file
For Each tdf_DB In dbs_DB.TableDefs
If Len(tdf_DB.Connect & "") > 0 Then
strPathFilenameOfBE = Replace(tdf_DB.Connect, ";DATABASE=", _
"", 1, -1, vbTextCompare)
Exit For
End If
Next tdf_DB
Set tdf_DB = Nothing
DoEvents
strFilenameOfBE = ExtractFileName(strPathFilenameOfBE)
strPathOfBE = ExtractPath(strPathFilenameOfBE)

' Check to see if an .ldb file exists for the current backend file. If yes,
tell user
' that someone is in the backend, and the copy cannot be created; then
reopen form
' "_frm_KeepRecordsetOpen" in *hidden* mode and exit the subroutine.
strTempVar = Dir(Left(strPathFilenameOfBE, _
Len(strPathFilenameOfBE) - 3) & strLockFileExtension)

If strTempVar <> "" Then

MsgBox "Someone else is still working in the database! The program " & _
"cannot make a copy of the ""backend"" file at this time." & _
vbCrLf & vbCrLf & "Try again later when no one other than you is
working " & _
"in the database.", vbCritical, "Cannot Copy Backend File!"


Else

' Provide the directory to the folder where the copy is to be put
xstrToLocation = "PathToWhereToPutTheBackendFile"

On Error Resume Next

If xstrToLocation <> "" Then

datNowValue = Now
' Copy the backend file to the selected location
FileCopy strPathFilenameOfBE, xstrToLocation & strFilenameOfBE

If Err.Number = 75 Then
MsgBox "You cannot create a file in the folder that you
selected:" & _
vbCrLf & Space(5) & """" & xstrToLocation & """" & vbCrLf &
vbCrLf & _
"The device may be a ""read-only"" device, or you may not
have " & _
"permission to write to the folder." & vbCrLf & vbCrLf & _
"Select a different location.", vbCritical, "Cannot Create
File"
Err.Clear
GoTo LoopLabel

Else

Open strPathOfBE & "Backend_Manually_Copied_On_" & _
Format(datNowValue, "ddmmmyyyy_hh.nn.ssAMPM") & ".txt" For
Output As #1
Print #1, "A copy of the backend database file ( """ &
strPathFilenameOfBE & _
""" ) was manually created by the front end's backup
feature:"
Print #1, " -- made on " & Format(datNowValue, "mmmm
dd, yyyy") & _
" at " & Format(datNowValue, "hh:nn:ss AMPM")
Print #1, " -- copied to """ & xstrToLocation &
strFilenameOfBE & """"
Print #1, " -- copied by """ & fOSUserName & """ from
computer """ & _
fOSMachineName & """"
Close #1
DoEvents
' Tell user that the copying was successful
MsgBox "The file has been created at" & vbCrLf & Space(5) &
xstrToLocation & _
strFilenameOfBE, vbInformation, "File Created"

End If

Else

MsgBox "No location was selected. No copy of the backend file will
be made.", _
vbExclamation, "No Location Selected"

End If

End If

Exit_CopyBackup:
On Error Resume Next
Set tdf_DB = Nothing
dbs_DB.Close
Set dbs_DB = Nothing
DoCmd.Hourglass False
Err.Clear
Exit Sub

Err_CopyBackup:
If Err.Number = 71 Then
MsgBox "The device that you selected ( """ & xstrToLocation & _
""" ) does not contain a disc or diskette. " & _
"The file cannot be copied to this device.", vbCritical, _
"No Disc or Diskette"
Else
MsgBox "An error has occurred while making a copy of the backend
database file:" & _
vbCrLf & " Error #" & Err.Number & ": " & Err.Description & vbCrLf
& vbCrLf & _
"Try again in a few minutes. If the problem persists, contact the
programmer for assistance.", _
vbCritical, "Error While Copying File"
End If
Resume Exit_CopyBackup

End Sub

' ********************************
' ** Function ExtractFileName **
' ********************************

Public Function ExtractFileName(ByVal strPathFile As String) As String
' *** THIS FUNCTION EXTRACTS THE "FILE NAME" PORTION OF A STRING THAT HOLDS
' *** THE FULL PATH AND FILENAME FOR A FILE. IT DOES THIS BY DROPPING
' *** THE PATH PORTION FROM THE STRING (ALL TEXT BEFORE THE LAST
' *** "\" CHARACTER IN THE STRING, AND THAT LAST "\" CHARACTER, TOO).

' *** IF THERE IS NO "\" CHARACTER IN THE TEXT STRING, THE FUNCTION RETURNS
' *** AN EMPTY STRING AS ITS VALUE. OTHERWISE, IT RETURNS THE "FILE NAME"
PORTION
' *** OF THE TEXT STRING.
' Ken Snell 19 May 2005

' strPathFile is string variable that contains the full path and filename
text string.

On Error Resume Next

If InStr(strPathFile, "\") = 0 Then
ExtractFileName = ""
Else
ExtractFileName = Mid(strPathFile, InStrRev(strPathFile, "\") + 1)
End If
Err.Clear
End Function




' ****************************
' ** Function ExtractPath **
' ****************************

Public Function ExtractPath(ByVal strPathFile As String) As String
' *** THIS FUNCTION EXTRACTS THE "PATH" PORTION OF A STRING THAT HOLDS
' *** THE FULL PATH AND FILENAME FOR A FILE. IT DOES THIS BY DROPPING
' *** THE FILENAME PORTION FROM THE STRING (ALL TEXT AFTER THE LAST
' *** "\" CHARACTER IN THE STRING).

' *** IF THERE IS NO "\" CHARACTER IN THE TEXT STRING, THE FUNCTION RETURNS
' *** AN EMPTY STRING AS ITS VALUE. OTHERWISE, IT RETURNS THE "PATH" PORTION
' *** (INCLUDING THE ENDING "\" CHARACTER) OF THE TEXT STRING.
' Ken Snell 19 May 2005

' strPathFile is string variable that contains the full path and filename
text string.

On Error Resume Next

If InStr(strPathFile, "\") = 0 Then
ExtractPath = ""
Else
ExtractPath = Left(strPathFile, InStrRev(strPathFile, "\"))
End If
Err.Clear
End Function
 
D

dc

Thank you, I am trying to get it to work and getting hung up on the
Err.Clear
GoTo LoopLabel

I am not that great at VBA so please excuse the novice questions.
 
K

Ken Snell \(MVP\)

Yep, my error. Told you it might have a few "buggies" in it. Try this:

Here is some code that I've pulled from one of my applications. It is
designed to run from a button on a form in the front end database, and makes
a copy of the backend database if all users other than the front end running
the code have disconnected from the backend. This would be useful for the
"manual" method.

You could run similar code via an AutoExec macro in a database file, where
the scheduler opens the file with the AutoExec macro in it. You'd need to
change UserCreateBackendBackup to a function; and then the AutoExec macro
would call the function via the RunCode action. But try this with the manual
setup first.

I took out a few things that are specific to that application, so it's
possible that you may get a compiler error the first time. Let me know if
you need assistance with this.
---------------------------

' ****************************************
' ** Subroutine UserCreateBackendBackup **
' ****************************************

Public Sub UserCreateBackendBackup()
' *** THIS SUBROUTINE IS USED TO ALLOW A USER TO CREATE A BACKUP COPY OF
' *** THE BACKEND DATABASE FILE IN ANOTHER LOCATION (e.g., ZIP DISC DRIVE,
' *** CD DRIVE, NETWORK LOCATION, etc.)
' Ken Snell 19 May 2005

Dim dbs_DB As DAO.Database
Dim datNowValue As Date
Dim xstrToLocation As String, strTempVar As String, strPathOfBE As String
Dim strPathFilenameOfBE As String, strFilenameOfBE As String
Dim tdf_DB As DAO.TableDef

On Error GoTo Err_CopyBackup

DoCmd.Hourglass True


Set dbs_DB = CurrentDb

' Get the path and filename of the "backend" file
For Each tdf_DB In dbs_DB.TableDefs
If Len(tdf_DB.Connect & "") > 0 Then
strPathFilenameOfBE = Replace(tdf_DB.Connect, ";DATABASE=", _
"", 1, -1, vbTextCompare)
Exit For
End If
Next tdf_DB
Set tdf_DB = Nothing
DoEvents
strFilenameOfBE = ExtractFileName(strPathFilenameOfBE)
strPathOfBE = ExtractPath(strPathFilenameOfBE)

' Check to see if an .ldb file exists for the current backend file. If yes,
tell user
' that someone is in the backend, and the copy cannot be created; then
reopen form
' "_frm_KeepRecordsetOpen" in *hidden* mode and exit the subroutine.
strTempVar = Dir(Left(strPathFilenameOfBE, _
Len(strPathFilenameOfBE) - 3) & strLockFileExtension)

If strTempVar <> "" Then

MsgBox "Someone else is still working in the database! The program " & _
"cannot make a copy of the ""backend"" file at this time." & _
vbCrLf & vbCrLf & "Try again later when no one other than you is
working " & _
"in the database.", vbCritical, "Cannot Copy Backend File!"


Else

' Provide the directory to the folder where the copy is to be put
xstrToLocation = "PathToWhereToPutTheBackendFile"

On Error Resume Next

If xstrToLocation <> "" Then

datNowValue = Now
' Copy the backend file to the selected location
FileCopy strPathFilenameOfBE, xstrToLocation & strFilenameOfBE

If Err.Number = 75 Then
MsgBox "You cannot create a file in the folder that you
selected:" & _
vbCrLf & Space(5) & """" & xstrToLocation & """" & vbCrLf &
vbCrLf & _
"The device may be a ""read-only"" device, or you may not
have " & _
"permission to write to the folder.", vbCritical, "Cannot
Create
File"
Err.Clear

Else

Open strPathOfBE & "Backend_Manually_Copied_On_" & _
Format(datNowValue, "ddmmmyyyy_hh.nn.ssAMPM") & ".txt" For
Output As #1
Print #1, "A copy of the backend database file ( """ &
strPathFilenameOfBE & _
""" ) was manually created by the front end's backup
feature:"
Print #1, " -- made on " & Format(datNowValue, "mmmm
dd, yyyy") & _
" at " & Format(datNowValue, "hh:nn:ss AMPM")
Print #1, " -- copied to """ & xstrToLocation &
strFilenameOfBE & """"
Print #1, " -- copied by """ & fOSUserName & """ from
computer """ & _
fOSMachineName & """"
Close #1
DoEvents
' Tell user that the copying was successful
MsgBox "The file has been created at" & vbCrLf & Space(5) &
xstrToLocation & _
strFilenameOfBE, vbInformation, "File Created"

End If

Else

MsgBox "No location was selected. No copy of the backend file will
be made.", _
vbExclamation, "No Location Selected"

End If

End If

Exit_CopyBackup:
On Error Resume Next
Set tdf_DB = Nothing
dbs_DB.Close
Set dbs_DB = Nothing
DoCmd.Hourglass False
Err.Clear
Exit Sub

Err_CopyBackup:
If Err.Number = 71 Then
MsgBox "The device that you selected ( """ & xstrToLocation & _
""" ) does not contain a disc or diskette. " & _
"The file cannot be copied to this device.", vbCritical, _
"No Disc or Diskette"
Else
MsgBox "An error has occurred while making a copy of the backend
database file:" & _
vbCrLf & " Error #" & Err.Number & ": " & Err.Description & vbCrLf
& vbCrLf & _
"Try again in a few minutes. If the problem persists, contact the
programmer for assistance.", _
vbCritical, "Error While Copying File"
End If
Resume Exit_CopyBackup

End Sub

' ********************************
' ** Function ExtractFileName **
' ********************************

Public Function ExtractFileName(ByVal strPathFile As String) As String
' *** THIS FUNCTION EXTRACTS THE "FILE NAME" PORTION OF A STRING THAT HOLDS
' *** THE FULL PATH AND FILENAME FOR A FILE. IT DOES THIS BY DROPPING
' *** THE PATH PORTION FROM THE STRING (ALL TEXT BEFORE THE LAST
' *** "\" CHARACTER IN THE STRING, AND THAT LAST "\" CHARACTER, TOO).

' *** IF THERE IS NO "\" CHARACTER IN THE TEXT STRING, THE FUNCTION RETURNS
' *** AN EMPTY STRING AS ITS VALUE. OTHERWISE, IT RETURNS THE "FILE NAME"
PORTION
' *** OF THE TEXT STRING.
' Ken Snell 19 May 2005

' strPathFile is string variable that contains the full path and filename
text string.

On Error Resume Next

If InStr(strPathFile, "\") = 0 Then
ExtractFileName = ""
Else
ExtractFileName = Mid(strPathFile, InStrRev(strPathFile, "\") + 1)
End If
Err.Clear
End Function




' ****************************
' ** Function ExtractPath **
' ****************************

Public Function ExtractPath(ByVal strPathFile As String) As String
' *** THIS FUNCTION EXTRACTS THE "PATH" PORTION OF A STRING THAT HOLDS
' *** THE FULL PATH AND FILENAME FOR A FILE. IT DOES THIS BY DROPPING
' *** THE FILENAME PORTION FROM THE STRING (ALL TEXT AFTER THE LAST
' *** "\" CHARACTER IN THE STRING).

' *** IF THERE IS NO "\" CHARACTER IN THE TEXT STRING, THE FUNCTION RETURNS
' *** AN EMPTY STRING AS ITS VALUE. OTHERWISE, IT RETURNS THE "PATH" PORTION
' *** (INCLUDING THE ENDING "\" CHARACTER) OF THE TEXT STRING.
' Ken Snell 19 May 2005

' strPathFile is string variable that contains the full path and filename
text string.

On Error Resume Next

If InStr(strPathFile, "\") = 0 Then
ExtractPath = ""
Else
ExtractPath = Left(strPathFile, InStrRev(strPathFile, "\"))
End If
Err.Clear
End Function
 
D

dc

Thank you very much, The button works great with two exceptions

I dont get an opportunity to input a location and what if I only want to
back up just a few tables of the database?
 
K

Ken Snell \(MVP\)

To choose a location, you'll need to use API calls. See
http://www.mvps.org/access/api/api0001.htm for the code that will need to be
put into a regular module in your database (see the code in the blue-shaded
area). Then you'll need to change the code I gave you so that the location
is not hard-coded, but instead you call the API function to pop up the
browse window for naviagating to the location.

If you just want to copy some tables, you could write some reasonably
complex code to let you choose which tables you want to backup...or you
could just use the File | Export menu to export a table to another database.
This allows you to export a copy of one table at a time.

Any particular reason why backing up all the data is not desired? I usually
find that backing up all data is a good thing to do.

--

Ken Snell
<MS ACCESS MVP>
 
D

dc

Thank you Ken for all of your help. I finally got most of it figured out
with two exceptions, the first being the "back-end" database, when I am in
one db its copying another db where I want this one to be so my thinking is I
am not working in a "back-end" environment. We dont have securities right
now on the db's. The other issue is, the xstrToLocation currently I have it
going to the ahtCommonFileOpenSave which in turn does the pop up. How do I
know what the xstrToLocation should be refered to?
 
K

Ken Snell \(MVP\)

Second question first. If your code step looks like this:

xstrToLocation = ahtCommonFileOpenSave

then the xstrToLocation variable will contain the full path to the folder
where you want to put the copy of the database.

First question next: If I'm understanding correctly, the copy action is
copying a different database than the one you want to copy? If so, then that
means that you're navigating to the wrong database file when you're asked
for the database file that you want to copy.

Backend database is a reference to the "data-containing" portion of a split
database setup. When you split a database the backend contains all the
tables with data; the frontend contains the forms, queries, reports, code,
etc. The frontend contains links to the tables in the backend database.

I cannot state with certainty if you are or are not using a split database;
I have been assuming that you are because you said earlier that you are.
--

Ken Snell
<MS ACCESS MVP>
 
D

dc

I get two open pop ups but never a save as, you have to open a db to save
your backup and it then copies another db at this location. On the splitting
of the db, sorry my confusion, no we have not split the db, should we?
 
K

Ken Snell \(MVP\)

If the database is not split, then you cannot try to make a copy of the
database from within itself. You can make a copy only by running another
ACCESS file that has the code to make the copies in it, and the database
file that you want to copy cannot have anyone in it.

You don't get a "SaveAs" window; you get a Browse window. The code that I'd
provided uses the folder that you select in that window in order to store a
copy of the database in that folder, using the name that the code is
assigning to the copy of the file. The code that I provided is based on the
database being split, with the backend file being in a location either that
is hard-coded into the programming or that is "obtained" via a Browse window
(sounds like you're using the latter option).

If you have multiple people using the database, it should be split.
Corruption problems occur very easily when multiple users are using the same
database file.
 

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