VBS Add numbers for each loop - variables

For my VBS script, for each loop, I want it to add the number of which loop it's on. Like if it's a site and has an ID, then if it's on loop 1 adds 1 to the ID. I already declared "site" for my script.
What do I do? Do you need to see some of the script?

Not sure what kind of collection you're working with, but you could do something simple like this
Set objFilesystem = CreateObject("Scripting.FileSystemObject")
Set objInputFolder = objFileSystem.GetFolder("c:\temp")
i = 1
For Each objInputFile In objInputFolder.Files
WScript.Echo SITE + i
i = i + 1
Next

Related

Dynamically name apple script variable

Let me preface this by saying that I know that I can easily do the following using a calculated Apple Script from Filemaker. I'd really love to know how to accomplish this inside an Apple Script. I'm trying to set variables into a macro program called Keyboard Maestro that I will refer to as KM.
My script first steps through found records in Filemaker and copies data from each found record. I can't figure out how to set a KM Variable from a loop in Apple Script because, as far as I know, you can't dynamically set the names of Apple Script variables.
I can create the names (that i would like to use) of the variable by creating a list with:
set variableListFunderName to {}
set i to 1
repeat until i = winCount + 1
copy "Business_ExistingAdvance" & i & "FunderName" to the end of variableListFunderName
set i to i + 1
end repeat
set variableListFundingCurrentBalance to {}
set i to 1
repeat until i = winCount + 1
copy "Business_ExistingAdvance" & i & "FundingCurrentBalance" to the end of variableListFundingCurrentBalance
set i to i + 1
end repeat
set variableListFundingAmount to {}
set i to 1
repeat until i = winCount + 1
copy "Business_ExistingAdvance" & i & "FundingAmount" to the end of variableListFundingAmount
set i to i + 1
end repeat
--
But when I set the contents of my fields to the list items that I created, the variables don't show up in KM:
--
set i to 1
repeat until i = winCount + 1
tell record i
set item i of variableListFunderName to cell "Funders_ExistingAdvances::FunderCompanyName"
set item i of variableListFundingCurrentBalance to cell "FundingCurrentBalance"
set item i of variableListFundingAmount to cell "FundingAmount"
end tell
ignoring application responses
tell application "Keyboard Maestro Engine"
setvariable item i of variableListFunderName & "_AS" to item i of variableListFunderName
setvariable item i of variableListFundingCurrentBalance & "_AS" to item i of variableListFundingCurrentBalance
setvariable item i of variableListFundingAmount & "_AS" to (item i of variableListFundingAmount)
end tell
end ignoring
set i to i + 1
end repeat
How can I setup these KM variables?
You cannot create dynamic variables with applescript.
What you currently do is to generate strings and add them to a list. Then you REPLACE these strings with other values so the strings are lost.
But you could achieve this with the help of applescript objective-c:
use framework foundation
set variableListFunderName to {}
set i to 1
repeat until i = winCount + 1
copy "Business_ExistingAdvance" & i & "FunderName" to the end of variableListFunderName
set i to i + 1
end repeat
set funderNameDict to current application's NSMutableDictionary's alloc()'s init()
repeat with var in variableListFunderName
funderNameDict's setObject:"yourValue" forKey:var
end repeat
Then you can access the dynamic values with
set myValue to funderNameDict's objectForKey:"yourDynamicVarName"
to loop through the dictionary use
repeat with theKey in funderNameDict's allKeys()
set myValue to funderNameDict's objectForKey:theKey
end repeat

vba store recordset as integer variable

First time poster, I finally had a question that I couldn't find an answer to here.
I have an MS Access query that returns 1 result (which is a number) that I want to store as an integer variable (x) so I can use it later for a loop. The issue is that because I'm using it as a recordset and the variable is an integer, I'm receiving a "Type Mismatch" error.
Right now I'm just storing the result to a cell and setting the variable equal to the cell:
Ws.Range("A1") = Db.OpenRecordset("SELECT COUNT(Asset_Name) FROM Assets WHERE Active = True").GetRows(1)
x = Ws.Range("A1")
Ws.Range("A1").Delete
And then later I just have a loop that runs x times:
For i = 0 To x
Basically, I just want to have some code that looks like this:
x = Db.OpenRecordset("SELECT COUNT(Asset_Name) FROM Assets WHERE Active = True").GetRows(1)
Any help here would be huge. Thank you!
The following should give you the correct result:
Dim x As Integer
Dim db As DAO.Recordset
db.MoveFirst
If IsNumeric(db.OpenRecordset("SELECT COUNT(Asset_Name) FROM Assets WHERE Active = True").Fields(0).Value) Then
x = CInt(db.OpenRecordset("SELECT COUNT(Asset_Name) FROM Assets WHERE Active = True").Fields(0).Value)
Else
MsgBox "The query did not return a number." & Chr(10) & "Aborting..."
End If
Note, that you are using DAO and not ADO as your original tags on the post indicated. Still, they both behave rather similar and the cursor is normally on the first row (when the data is returned). So, MoveFirst should not be necessary. Still, Microsoft themselves keep using it in its own sample code all the time. The first column if for DAO and ADO alike .Fields(0).

VBA - Looping through filtered table in access

Access 2013 32 Bit / Windows 7 64 Bit
I have a table of keys and based on the key value my vba will run some queries and populate other fields in the table - I call this "Checking" I have a "Checked" Column in the table and have to break the processing of this list into several batches (and more records are constantly added) so I'd like to filter out the "Checked" ones. I'd like to do this WITHOUT using a separate query
I'm using the traditional loop w/ a condition:
(My code works this is just a mock up ignore syntax here please)
Move First
Do Until EOF
if Table("Checked") = "True" Then goto NextRow
<Code>
NextRow:
Move Next
Loop
I'd like to continue with this method, but filter out all of the "Checked" = True on the onset
Simply open your recordset with a query that has a WHERE clause.
Dim rs As Recordset
Dim S As String
S = "SELECT foo, morefoo FROM bar WHERE [Checked] = 0"
' Or if Checked is a Text column:
S = "SELECT foo, morefoo FROM bar WHERE [Checked] <> 'True'"
Set rs = CurrentDb.OpenRecordset(S)
Do While Not rs.EOF
' code
rs.MoveNext
Loop
(BTW, your Move First inside the loop would create an endless loop...)
I ended up sorting the field then exiting the sub when I reached a row marked as checked.

Trying to fill in data if Null to take the previous that has data

Newbie here and probably to this process. I'm trying to fill in data that is Null on the ticker field to take the previous known data. See my example below. I don't know if I should write in a query or something else on MS Access 2007.
So for example under 0897250D CH Equity is null or blank. I want to take the data before it and fill it in.
Sorry. Got my answer. I pretty much used the following....
Sub nullparts()
Dim i As String
Set myDb = CurrentDb()
Set MyRs = myDb.OpenRecordset("BB - HistoricalDividends")
MyRs.MoveFirst
Do While Not MyRs.EOF
MyRs.Edit
For Each myfld In MyRs.Fields
If myfld.Name = "Field1" Then
If MyRs(myfld.Name).Value <> "" Then
i = MyRs(myfld.Name).Value
Else
MyRs(myfld.Name).Value = i
MyRs.Update
End If
End If
Next myfld
MyRs.MoveNext
Loop
End Sub
Now I'm trying to accomplish 2 more things.
1) Create an Import From Excel button on the database
2) Once imported, remove all lines that have no data on F2 to be deleted.

VBA deleting name throws object required

I just want to delete a few names that get created every time a querytable gets created. They are all in 3 sheets starting with 0048,0114,0715, so I would just delete all names that start with any of them. However, I get the rejection "object required" in the if clause when I use rName.Delete. Without this, the code runs fine and prints all the names. Also, if I do range(rName).delete it would delete the ranges in the workbook (not what I want, though).
Sub delNames()
Dim strStartString(0 To 2) As String
strStartString(0) = "'0048'!mta"
strStartString(1) = "'0114'!mta"
strStartString(2) = "'0715!'mta"
For Each rName In ActiveWorkbook.Names
For Each ss In strStartString
If rName.Name Like ss & "*" Then
Debug.Print rName.Name
rName.Delete
End If
Next ss
Next rName
End Sub
Any idea what I am doing wrong here?
Posting Tim's comment as solution
Modifying a collection while looping through it can cause problems. Try using a for next loop counting back through the names in reverse - For x = ActiveWorkbook.Names.Count to 1 Step -1 : ActiveWorkbook.Names(x).Delete – Tim Williams 49 mins ago