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
Related
This seems crazy basic to ask, but I can't figure out what I've done wrong. Working in Access VBA to loop through an array, but the FOR loop doesn't stop and I get a "Subscript out of range" error.
compStr = "[" & uniqueIDs(0) & "]=" & rs(uniqueIDs(0))
bnd = UBound(uniqueIDs)
For i = 1 To bnd
compStr = compStr & " and [" & uniqueIDs(i) & "]=" & rs(uniqueIDs(i))
Next i
In this example I'm using the loop to build compStr and bnd stores the array size. uniqueIDs holds 2 records (0-1) and I've confirmed that bnd and UBound(uniqueIDs) value=1. However the FOR statement continues until i=2 and of course I get an error when it executes uniqueIDs(2) on the 4th line. What am I missing? (I've pasted debug window images below)
I did a test but that successfully ran, see below implementation:
Regarding above comments on the data types: I understand why people use Variants in Access: this is because often table values are assigned to variables, and only Variant can pick up NULL. However, in your case something forced the string values to be Strings and not Variants - if I used the line commented out, it would yield Variant/Variant(0 to 1), so I used a typed dummy array - I don't know how otherwise get Variant/String(0 to 1).
Nevertheless, there was no error. Did you compile your code with Debug -> Compile? If not, could you do it and re-try? Sometimes this is necessary, otherwise some code segments newly added/removed are just not executing and the previous compiled version of the code (which is stored by Access) is making a total mess.
If it's still not working, I think you should add the rest of the code as others asked in comments to see how the variables are assigned values, and the version of Access you are using.
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'm getting a persistent error:
The element cannot be found in a collection.
This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.
I've checked, double and triple-checked my variable listings in the Read-Only and Read-Write variables in my Script task.
I've debugged it to death and gotten input from another programmer here who couldn't spot the issue either.
I've also researched to no end.
Does anyone see anything wrong with my code?
Script Task code:
Public Sub Main()
Dts.Variables("User::strMailBody").Value = "Thank you for submission. For your convenience, we are including the last four of the HICN# and the Name on the application(s) we have received* from you." _
& vbNewLine & vbNewLine & "Here are the following: " & vbNewLine & vbNewLine
Dts.Variables("User::strMailBody").Value = Dts.Variables("User::strMailbody").Value.ToString() & vbNewLine & Dts.Variables("User::strListing").Value.ToString()
Dts.Variables("User::strMailBody").Value = Dts.Variables("User::strMailBody").Value.ToString() & vbNewLine & vbNewLine & Dts.Variables("User::strFooter").Value.ToString()
If Left(Dts.Variables("User::strAgentID").Value, 2) = "TX" Then
Dts.Variables("User::strSubject").Value = "ACME Health Plans Confirmation: Total "
Else
Dts.Variables("User::strSubject").Value = "ACME2 Baptist Health Plans Confirmation: Total "
End If
Dts.Variables("User::strSubject").Value = Dts.Variables("User::strSubject").Value.ToString() & Dts.Variables("User::lngCountAgent").Value.ToString() & " " & "[RESTRICTED: CONFIDENTIAL]"
Dts.Variables("User::DateSent").Value = Now()
Dts.Variables("User::UserSent").Value = "SSIS"
Dts.TaskResult = ScriptResults.Success
End Sub
For anybody else struggling with this issue the resolution for me was as follows: (note I am NOT using User:: when getting variable values within my script task)
On the package Properties I hadn't included the variables as ReadOnlyVariables
You'll need to set your newly added variables as follows:
Right click on the package and select Edit
In the Script section click on ReadOnlyVariables or ReadWriteVariables (depending on your how you want your variables behave)
Check the check-box beside the variables you wish to use in your script task
Click Ok to save your changes
Hope this helps
I just had the same issue and unable to find the problem for ages. I found that the reason for the error was that I had missed one of the colons between "User" and the variable name.
I had this (which caused the error):
string FileName = UserVariables["User:CurrentFileName"].Value.ToString();
when I should have had this:
string FileName = UserVariables["User::CurrentFileName"].Value.ToString();
just in case anyone else has the same problem :)
Ohhh.........man. It's amazing how you can stare at this stuff and miss something stupid, for hours.
strFooter was missing in the listing.
ALL SET NOW. Sincere thanks to those who looked and commented. Eric, thanks, I'll remember that as sometimes I will probably need to use C insatead of VB (haven't yet but will).
Had a similar issue, after a lot of debugging, realized that the variable naming convention should be User::varname and NOT USER::varname
I guess c# is very case sensitive.
Hope this helps and saves you lot of your valuable time :-)
a) Right click on the script task and choose edit
b) Locate the Read or Read/Write variables properties in the list.
c) Click on the property and the variable you wish to access in the script task.
Another variation on "have been staring at the screen for too long to see the typo". In my case, I got the same error by mixing up the syntax between Project Params and User variables, and adding a $ sign in front of User.
error :
string varA = (string)Dts.Variables["$Project::ParamA"].Value
string varB = (string)Dts.Variables["$User::ParamB"].Value
corrected :
string varA = (string)Dts.Variables["$Project::ParamA"].Value
string varB = (string)Dts.Variables["User::ParamB"].Value
I'm experiencing a 1004 runtime error when saving a workbook (wbk_New) on which I copy-pasted a group of shapes from another workbook (wbk_Old). I should tell that a macro from wbk_Old is assigned to this group.
wbk_Old.Worksheets("DashBoard").Activate
ChartTop = ActiveSheet.Shapes("Group_VesselGraphics").Top
ChartLeft = ActiveSheet.Shapes("Group_VesselGraphics").Left
ActiveSheet.Shapes("Group_VesselGraphics").OnAction = ""
ActiveSheet.Shapes("Group_VesselGraphics").Copy
wbk_New.Worksheets("DashBoard").Activate
ActiveSheet.Shapes("Group_VesselGraphics").Delete
ActiveSheet.Paste
ActiveSheet.Shapes("Group_VesselGraphics").Top = ChartTop
ActiveSheet.Shapes("Group_VesselGraphics").Left = ChartLeft
ActiveSheet.Shapes("Group_VesselGraphics").OnAction = "'" & ActiveWorkbook.Name & "'!UpdateShipGraph"
wbk_Old.Close
wbk_New.SaveAs As path_Old
I can't figure out what is causing this error...Does anyone have already faced this issue ?
Thanks a ton for your help !
(I forgot to mention that a chart also belong to this group of shapes!)
Ok, I don't understand why but it seems that some links were still existing between "wbk_New" and "wbk_Old" although I broke all the links and updated the chart to refer to intrinsic data.
Hence, closing of "wbk_Old" couldn't be properly performed and wbk_New became corrupted, so that it was impossible to save it.
The only way I found to work around this issue is to save (on itself) wbk_New before closing wbk_Old and then to call an external process that replace wbk_Old by wbk_New, using a delay of 4s, and to close wbk_New and Old before the delay completed.
I have to admit that this is an ugly solution, if someone knows a better way it would be nice to share it!
Here below is the code of my solution, where BatchCmd creates a batch file of a command and shell it:
{code above}
wbk_New.Save
cmd = "ping -n 4 127.0.0.1 >nul" + vbCrLf
cmd = cmd + "move /Y " & path_New & " " & path_Old
Call BatchCmd(cmd, status:=vbHide)
wbk_Old.Close
wbk_New.Close
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.