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.
Related
I have created a query that works great with no errors in Access, and while trying to translate this to my vba setup I can't figure out why I am not getting any values, even though the query clearly works in access, and causes no errors on the VBA side.
Pulling my hair out here, I have created a side table to see if I could "pseudo-insert" the calculated value, but I get the same issue as above - insert works with no errors in access, goes through in vba with no issues, but doesn't actually insert any data.
I have copied the string queries while pausing code to make sure EVERYTHING matches up between the Access query that works and the VBA query, and haven't found any differences.
I read somewhere since I am trying to pull a "first line" data piece that there may be some HDR setting that I could change, but when I tried to apply any fixes I found they opened another instance of excel and opened a dialogue box.
Please let me know what I am doing wrong here.
Public Function PullNextLineItemNumB(QuoteNum) As Integer
Dim strQuery As String
Dim ConnDB As New ADODB.Connection
Dim myRecordset As ADODB.Recordset
Dim QuoteModifiedNum As String
ConnDB.Open ConnectionString:="Provider = Microsoft.ACE.OLEDB.12.0; data
source=" & ThisWorkbook.Path & "\OEE Info.accdb"
'Query to try and make Access dump the value "18" into the table so I can
grab it after the query is finished, sidestepping excel not working
strQuery = "INSERT INTO TempTableColm (TempColm) SELECT
MAX(MID([Quote_Number_Line],InStr(1,[Quote_Number_Line]," & Chr(34) & "-"
& Chr(34) & ")+1)) AS MaxNum from UnifiedQuoteLog where Quote_Number_Line
like '" & QuoteNum & "*'"
ConnDB.Execute strQuery
'Original query, returns "18" as expected in Access, and null or empty in
the recordset
strQuery = "SELECT MAX(MID([Quote_Number_Line],InStr(1,
[Quote_Number_Line]," & Chr(34) & "-" & Chr(34) & ")+1)) from
UnifiedQuoteLog where Quote_Number_Line like '" & QuoteNum & "*'"
Set myRecordset = ConnDB.Execute(strQuery)
Do Until myRecordset.EOF
For Each fld In myRecordset.Fields
Debug.Print fld.Name & "=" & fld.Value
Next fld
myRecordset.MoveNext
Loop
myRecordset.Close
Set myRecordset = Nothing
ConnDB.Close
Set ConnDB = Nothing
End Function
Actual output from access is "18" which is expected, output from excel's vba recordset is always null or empty string.
It appears I solved the problem, while looking into this apparently the excel operator using ADODB with access is % for LIKE and NOT * (because reasons). As soon as I made that change everything started working.
Can someone explain why that is a thing? I really want to understand why this was a design choice.
I have a simple SQL query (below) that I run via a macro in Excel workbook. It works fine on MS Office 32-bit as well as 64-bit, however, for 64-bit Office users a pop-up window (below) appears in the middle (on the .Refresh line). It is enough to just press "OK" and the macro continues and fetches the data correctly, however, I would like to avoid this pop-up window if possible and/or understand why this is happening, please?
Query:
Sub Run_Query( _
ByRef SQL_Data_rng As Range, _
ByRef Conn_str As String, _
ByRef SQL_str As String)
With ws1.QueryTables.Add( _
Connection:="" & Conn_str & "", _
Destination:=SQL_Data_rng, _
Sql:=SQL_str)
.BackgroundQuery = False
.Refresh
End With
End Sub
As Conn_str is already a string, you don't need to quote it (actually: shouldn't quote it):
Connection:= Conn_str, ...
OK, I think I found the cause of the problem. There was a mistake deeper in my code that I didn’t mention. I'm going to post my findings here, hopefully it will help someone in the future.
I store my Connection and SQL query info in a worksheet and to get these into a string I'm using a small function.
Function BuildStr(Rng As Range) As String
Dim Sub_Str As Range
For Each Sub_Str In Rng
BuildStr = BuildStr & Sub_Str & vbNewLine
Next Sub_Str
End Function
The & vbNewLine is there to ensure SELECT, FROM, WHERE, etc. statements in my SQL string start from new lines (otherwise no dice) and it also makes it easier to read the query when occasionally printed out.
But looks like it should not be used in the Connection string.
So in short, the connection string should look like this:
OLEDB;DRIVER=SQL Server;SERVER=SVR_name;Database=DB_name;Trusted_Connection=Yes
And not like this:
OLEDB; & VbNewLine & DRIVER=SQL Server; & VbNewLine & SERVER=SVR_name; & VbNewLine & Database=DB_name; & VbNewLine & Trusted_Connection=Yes
I am yet to understand why this isn't an issue on 32-bit Office, but most importantly I got rid of the annoying pop-up window (and learned something I didn't know).
Thanks,
J.
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.
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()
I'm ridding my code of all compiler warnings, as per my bosses request, when I came across some unused local variables. Naturally, I removed the ones which were legitimate, however I stumbled upon a couple which aren't as straight forward. (Variable names changed for security)
Dim strAAA As String = "aaaa" & strBBB & Now.ToString("yyyyMMddHHmmss") & ".doc"
If FUNCTION_NAME(strCCCC, strAAA) Then Return True
strAAA is allegedly an 'unused local variable' when it is clearly used directly below.
Even when I write it as follows:
Dim strAAA As String
strAAA = "ViewLet" & strBBB & Now.ToString("yyyyMMddHHmmss") & ".doc"
If FUNCTION_NAME(strTmpFileName, strAAA) Then Return True
The warning is still present.
Can anybody solve this mystery?
Solved it.
There was a Return True about 50 lines above.
This was always being hit, thus the variables were never being set.
Sloppy code from my predecessor I'm afraid!
Try eliminating the variable instance...
If FUNCTION_NAME(strTmpFileName, "ViewLet" & strBBB & Now.ToString("yyyyMMddHHmmss") & ".doc") Then Return True