Good morning,
When I want to show the project name & version I use:
System.Windows.Forms.Application.ProductName
System.Windows.Forms.Application.ProductVersion
Is there a similar thing for the last changed date of a project?
At the moment I have a valid workaround (see below), but I wonder if there is a build in solution like with the ones above.
Dim strFile = Application.StartupPath & "\" & Application.ProductName & ".exe"
Return System.IO.File.GetLastWriteTime(strFile.ToString()).ToShortDateString()
Related
I have a VBA code to create a folder every day I run it, based on the date. I use the MkDir for this;
MkDir "C:\Users\Stark\Desktop\Automation\Report" & " " & Format$(Date, "dd-mm-yyyy")
Once this folder is created, I have to copy a few files to this folder on which I will have to work. Since this path keeps changing every time I run, I am not sure how to copy files to the newly created folder in such a case. The FileCopy function throws an error if I use the same path as mentioned above.
FileCopy "C:\Users\Stark\Desktop\Templates\RawData.xlsx", "C:\Users\Stark\Desktop\Automation\Report" & " " & Format$(Date, "dd-mm-yyyy")\RawData.xlsx"
This part throws an error when I want to copy the file from Templates folder to the newly created folder.
Kindly help me.
Your code runs well here, if I correct the concatenation at the end of the FileCopy-line:
\RawData.xlsx" must be concatenated properly using a & (like you do in the other places too):
FileCopy "C:\Users\Stark\Desktop\Templates\RawData.xlsx", "C:\Users\Stark\Desktop\Automation\Report" & " " & Format$(Date, "dd-mm-yyyy") & "\RawData.xlsx"
I have a problem with Excel (ODBC / Access). I would like to refresh data from any folder in my computer. I have file which I use from Desktop, but I would like to refresh data from the same file when it will be move to documents, etc. Please could you provide what I should do?
I have a file connection as below:
DSN=Excel Files;DBQ=C:\Users\User\Desktop\Task1\SalesBudget2018.xlsx;DefaultDir=C:\Users\User\Desktop\Task1;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;
Thank you.
EDIT1: Thank you. I tried use your solution and I received error: "run time error 2147467259 Database or object is read-only", my code below. The bug is related to ".Open" line:
Sub RefreshData()
Dim CreateNew As Object
Dim RunSELECT As Object
Dim Data As String
Dim SQL As String
FolderPath = ActiveWorkbook.path
Path = Left(FolderPath, InStrRev(FolderPath, "\") - 1)
Set CreateNew = CreateObject("ADODB.Connection")
With CreateNew
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & Path & "\SalesBudget2018.xlsx" & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=YES;"";"
.Open
End With
'Run SQL
SQL = "SELECT * FROM [twRynki$]"
Set RunSELECT = cn.Execute(SQL)
Do
output = output & RunSELECT(0) & ";" & RunSELECT(1) & ";" & RunSELECT(2) & vbNewLine
Debug.Print RunSELECT(0); ";" & RunSELECT(1) & ";" & RunSELECT(2)
rs.Movenext
Loop Until rs.EOF
End Sub
Your path actually is not relative at all. But ACE/JET data engine does not support relative paths anyway.
A relative path would be
\Data\mydb.mdb
So, above would be one folder up called data from current location. And one folder down using relative would be:
..\Data\mydb.mdb
However, with ACE/Access relative paths are not supported. However, what we do when we want software to work say from the current folder? We simple get and use the full path name ON APPLICATION start up. So, you can get/grab the current folder. In Excel VBA you can use this:
ActiveWorkbook.Path
So above will give you the current path. And thus you use that in code to set the connection string. So, even in access, if we want the software to work in any folder? We simply get/grab the full path name on startup. As a result, the software works in any folder and you effective get relative address in that you "don't care" where the software is placed, since you always get/grab the full path name anyway. So, with above, you could append a folder name called data
ActiveWorkbook.Path & "\Data\Mydb.accdb"
So, from the current workbook location, you could always have a folder called data, and inside that folder you can have your database. So, in effect you do get relative addressing, but you always pulling the full path name of the current workbook as per above.
The end result is you don't miss not having some form of relative addressing since you don't need to with this approach.
The above is for Excel VBA. To get current path from Access VBA? You can use this:
currentproject.Path
So, your connection string to Excel could be this:
dim strExcelPath as string
strExcelPath = CurrentProject.Path & "\Task1\SalesBudget2018.xlsx"
It not clear if the access application is in the SAME folder as task1?
Assuming yes, then this would work:
strExcelPath = CurrentProject.Path & "\SalesBudget2018.xlsx"
So, now the folder can be on the desktop, my documents - it will not matter. You can thus use above as part of your connection string. It not clear if you linking to Excel (linked table), or you using VBA and say ADO code. However, it really don't matter. On application startup, you get the above connection string, check it against the linked table -- if same then do nothing. If different, then you re-link that one table. Thus you ONLY re-link one time if the folder been moved. And no matter where you move the folder? As long as you assume the Excel sheet is in the same folder as the access app, then you good to go. And as noted, you could add a sub folder say ExcelSheets to above. And once again, no matter where you move this folder with the Access part, as long as the sub folder is in the same dir/folder, then this will work - despite you not having relative addressing.
I have a VB program built in Studio 2017. I need to generate the time in a format which can go on to be used in a filename.
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
spits out 12:00:00 for example and the : isn't usable in a filename.
I could do with either removing the : so it would be 1200000 (not particularly readable but suitable for my purposes) or 12-00-00.
I have checked here and can't see any ToString format that will do the trick.
My code will put a label (say label1) to the current date and time. Another part will use the Label1.Text to grab the string. So any formatting can happen with Label1.
For example, it will be used as follows;
oDoc = oWord.ActiveDocument
oDoc.saveas2("C:\Test\" & "DocumentTitle" & "-" & label1.text & ".docx"
Is there a way to format the Date string to what I want?
Why add the colons in the first place?
DateTime.Now.ToString("yyyy-MM-dd HHmmss")
Simply use the String.Replace
Dim ActualTime as String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
ActualTime = ActualTime.Replace("/","-").Replace(":","-")
oDoc = oWord.ActiveDocument oDoc.saveas2("C:\Test\" & "DocumentTitle" & "-" & ActualTime & ".docx"
Not the most optimized but clear to understand. Added a one line replace as suggested by Visual Vincent.
Make sure to check his solution as well.
I am adjusting an existing file transfer windows service that renames the file being sent as a timestamp. For testing purposes, I need to make the files sent show up in the destination directory as being ten days ahead of when they were actually sent. Ex: if it's sent on 11/23/2015, it needs to look like it arrived 12/03/2015.
The line of code that generates the file name looks like this:
Dim strFileNameToTransfer As String = My.Settings.FileDirectory.ToString() & Format(Now(), "yyyy") & Format(Now(), "MM") & Format(Now(), "dd")
File name shows up in directory like this, if sent on 11/23/2015:
"20151123.xml"
But I would need it to show up like this:
"20151203.xml"
It would need to adjust the month as well, since the test will cross over into December and it is now November.
Like I said, this is for testing purposes, so it needs to go back to the way it was when testing is over. I really just need a quick fix here, but I know zero about Visual Basic, and I'm still new to programming in general as well. Help!
All you have to do is add 10 days to the existing date and use that date for your file name. You can also don't need to split the formatting into three different strings.
Dim fileDate = Now().AddDays(10)
Dim strFileNameToTransfer As String = My.Settings.FileDirectory.ToString() & Format(fileDate, "yyyyMMdd")
EDIT:
gmiley is right, it is better to use Path.Combine instead of string concatenation
Dim NameToTransfer As String = System.IO.Path.Combine(My.Settings.FileDirectory.ToString(), String.Format("{0}.{1}", fileDate.ToString("yyyyMMdd"), "xml"))
based off of what Microsoft tells me here: http://msdn.microsoft.com/en-us/library/xbfwysex(v=vs.84).aspx
this script should work
Sub Copy_Folder()
FileSystemObject.CopyFolder "C:\Testing\Test\", "C:\Testing\Test" & "_" & Format(Now, "yyyy-mm-dd")
End Sub
while playing around, I did receive some errors, which tells me the script is running. however, this runs w/o error, yet it just doesnt work. maybe its the date concatenation, so i comment out and just rename the folder to Tests (plural), it too runs w/o error yet doesnt do what it is supposed to. I've even moved the folder out of c:\Testing to the root of c, nope. sorry, this is noob but I dont get it.
As I mentioned in my comment, you can't use Format(). Also, if you don't need the time, use Date instead of Now. Here's a VBScript alternative.
' Global scope...
Dim FileSystemObject
' Somewhere along the way...
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
' Your function...
Sub Copy_Folder()
Dim strDate
strDate = Year(Date) & "-" & Right("0" & Month(Date), 2) & "-" & Right("0" & Day(Date), 2)
FileSystemObject.CopyFolder "C:\Testing\Test\", "C:\Testing\Test" & "_" & strDate
End Sub
Finally, your code above should have returned an error. Make sure you're not using On Error Resume Next anywhere in your code. It's almost never a good idea, especially for beginners or when debugging.