VBA to SaveAs current project with added Date & Time in filename

E

EagleOne

2003 up-to-date

How to save current project with current project filename + Current Date + Current Time

? Date() in Immediate Window - Returns date with "/" which cannot be used in a filename.

Therefore, how can I SaveAs a CurrentProject.name + Date + Time in a "new" filename?

Or kinda-like -

DoCmd.Save CurrentProject.Path & "\" & CurrentProject.name & Date????? & Time ?????

TIA EagleOne
 
C

Clif McIrvin

2003 up-to-date

How to save current project with current project filename + Current
Date + Current Time

? Date() in Immediate Window - Returns date with "/" which cannot be
used in a filename.

Therefore, how can I SaveAs a CurrentProject.name + Date + Time in a
"new" filename?

Or kinda-like -

DoCmd.Save CurrentProject.Path & "\" & CurrentProject.name &
Date????? & Time ?????

TIA EagleOne



Here's some code I put together to do something similar saving email as
text from Outlook. I'm going to remove the Outlook specific code as it's
not particularly relevant.

There are a variety of ways to break apart date information: Have you
looked at all the various date/time functions in the VBA help? Also, is
it important for the date-time information to be recognizable? In the
sample below I'm using just enough of the system date/time value to
guarantee a unique filename. I've since thought of other ways to do it,
but at the time I wrote a FRA function to extract just the fractional
value from a number.

As to dealing with the slash, I wrote a function to remove poison
characters from a string.

Hope this is useful!

::::::::::::: Begin VBA code :::::::::::::::::::::::


Public Const HomeEmail = "C:\long path name\to\folder I save to\"


rem function to return fractional portion of a number

Public Function Fra(ByVal num As Double) As Double

Fra = num - (Fix(num))

End Function



rem portion of code to save email message as text

Public Sub SaveAsTXT()

' Save selected mail item as text to home email folder.

' Add now() stamp to prevent duplicate filenames

rem I removed code to get email message.

"myItem.Subject" is the string containing the email subject,

which I use as the filename. There are some debug.print lines

commented out that I used during testing:



With myItem

' Debug.Print "Subject: ", .Subject

' Debug.Print "New name: ", .Subject & _

Format(Now Mod 1000 + Fra(Now), " 000.00000") & ".txt"

strName = HomeEmail & ParseFileName(.Subject) & _

Format(Now Mod 1000 + Fra(Now), " 000.00000") & ".txt"

' Debug.Print strName

rem strName is the fully qualified pathname of the new file. At this
point I inserted the Outlook SaveAS command.

End With

'Debug.Print "End Save as Text "; time

Set myItem = Nothing

Set SavedAsText = Nothing

End Sub



rem And this is the function to remove poison characters from a string.

I replace the poison character with a space, then finish with a test for
any double spaces and remove any found.

Public Function ParseFileName(ByVal strName As String) As String

' remove poison characters from a filename string

' \ / : * ? " < > |

' then replace any double spaces with single spaces

Dim Poison As Variant 'Array of Poison characters

Dim c1 As Integer 'instr start parameter

Poison = Array("\", "/", ":", "*", "?", """", "<", ">", "|", " ")

If Len(strName) > 0 Then

For Each c In Poison

c1 = 1 'Search from 1st character

Do

c1 = InStr(c1, strName, c, vbBinaryCompare)

If c1 Then

Mid(strName, c1, 1) = " "

If Len(c) > 1 Then

strName = Left(strName, c1) & Mid(strName, c1 + Len(c))

End If

End If

Loop Until c1 = 0

Next c

ParseFileName = strName

End If

End Function



::::::::::::: End VBA code :::::::::::::::::::::::
 
E

EagleOne

Thank you so much for your knowledge and time!!

Also, I am learning about Access VBA which has different capabilities when compared to Excel VBA.

Just experimenting, I came up with" myFilename &" "& Replace(Date, "/", "-") & " " & Time()

EagleOne
 
D

Douglas J. Steele

Far better to control the format of the date yourself. Your users could have
an unusual Date format defined....

myFilename &" "& Format((Date, "yyyy\-mm\-dd") & " " & Format(Time,
"hh\-nn\-ss")
 
D

Douglas J. Steele

Just realized there's no reason to use Date and Time: the Now function gives
you both:

myFilename &" "& Format(Now, "yyyy\-mm\-dd\-hh\-nn\-ss")

(Sorry about the extra parenthesis in the previous answer, too.)
 
E

EagleOne

Thanks Doug.

What code can I use to set-up for saving my current file (database) as another file?

MY attempts included (to no avail):

Dim TempFile As AccessObject
Set TempFile = CurrentProject.name
TempFile.SaveAs CurrentProject.Path & "MyFile " & Replace(Date, "/", "-") & " " & Time()

Help! (Can I record a macro in Access like in Excel?)
 
D

Douglas J. Steele

You can't record macros in Access, and you can't save the currently open
file.

Why do you think it's necessary to save the current 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