Loops in Access VBA - loops

I'm kind of new to Access VBA and I’m having issues with my loop.
I have reviewed various books, websites, asked various friends. Currently I’m trying to have a user input two characters, and then if the two characters equal a certain combination then it is supposed to equal a numeric value, and then use the value in a distance calculation.
The user inputed values are strings and everything else is declared as double.
I have 200+ combinations that I am testing and have tried case statements, DLookup, do while, do until, if, and elseif loops with no success.
I'm also limited by IT and I’m unable to use DAO code.
Any assistance would be greatly appreciated.

I would first suggest creating a table to formally define the various character combinations and the corresponding value for each combination. This has the advantage that the combinations may be easily maintained going forward, rather than defining such combinations as part of queries or in VBA code.
Such a table could be as simple as containing three fields:
Character 1 (Text)
Character 2 (Text)
Value (Number)
You could then populate such a table with your valid combinations:
With the combinations rigorously defined, you have many options regarding how to prompt the user to enter the two characters and obtain the correponding value.
A very simplistic approach might be to use a form with three textboxes:
Here, the first two textboxes would be unbound, and might be called char1 and char2, and the third textbox might be called result and have a Control Source property equal to:
=DLookup("Value", "LookupTable", "char1 = '" & [char1] & "' and char2 = '" & [char2] & "'")
This would yield the following behaviour:

Consider using Access as a database and GUI application by storing your 200+ combinations in a table with corresponding numeric value. Then have an SQL query filter by user input and use resulting value for needed calculations. Below requires creating and using four Access objects: table, query, form, and module.
Table (tblCombinations)
id combn1 combn2 numeric_value
1 A B 150
2 C D 225
3 E F 100
4 G H 75
5 I J 200
...
SQL (no loops needed; using Access form control values)
SELECT c.combn1, c.combn2, c.numeric_value
FROM tblCombinations c
WHERE c.combn1 = Forms!myForm!myUserInputField1
AND c.combn2 = Forms!myForm!myUserInputField2
And even pass needed numeric value in distance calculation formula which can be a VBA function in a standard module:
SELECT c.combn1, c.combn2, Distance_Calculation(c.numeric_value) As distance_result
FROM tblCombinations c
WHERE c.combn1 = Forms!myForm!myUserInputField1
AND c.combn2 = Forms!myForm!myUserInputField2
VBA (place in standard module)
Public Function Distance_Calculation(num_value As Long) As Double
...
Distance_Calculation = some_value
End Function

You can use following type function in your result form button or after event on both textboxes-
Dim resultValue as Integer
If DCount("numeric_value", "tblCombinations", "[combn1] = '" & Forms!myForm!myUserInputField1 & "' and [combn2] = '" & Forms!myForm!myUserInputField2 & "'") > 0 then
resultValue = Dlookup("numeric_value", "tblCombinations", "[combn1] = '" & Forms!myForm!myUserInputField1 & "' and [combn2] = '" & Forms!myForm!myUserInputField2 & "'")
txtResult = Distance_Calculation(resultValue)
Else
Msgbox "No such combination exist. Kindly check combimation", vbinformation, "Information"
txtResult = 0
End if

Related

Can you populate Classic ASP Constants (CONST) dynamically?

I am wishing to replace an include file with ~ 25 declared Constants by dynamically creating those constants from a Recordset (or Array). I have tried looping through a record set and have also tried populating a 2D array and then trying to set these Constants from the array values but it seems to take what is after the "Const" literal and not the value.
If (not rsVars.Eof) Then
cCount = rsVars.RecordCount
ReDim cArray(cCount - 1, 1)
For i = 0 To cCount - 1
cArray(i, 0) = rsVars("VariableNm")
cArray(i, 1) = rsVars("Value")
rsVars.MoveNext
Next
End If
For i = 0 To cCount - 1
' Display the Array Elements - WORKS and Displays All Variables and Desired Values
Response.write "("&(i+1)&") " & cArray(i, 0) & " = " & cArray(i, 1) & "<br>"
' Attempt to set Constant Variables - this next line errors - "Name_redefined"
Const cArray(i, 0) = cArray(i, 1)
Next
You could use the server.execute method to achieve this.
But it needs a FILE path to do so.
You will need to create a file dynamically using the FileSystemObject and pass the file's full path to the server.execute method to make those variables available as constants.
ex:
constants.asp
Note
Values will be CONSTANT Across user sessions and requests.
If you need a different set of constants by user, company, date etc, you will need to change the filename to include the unique constants-.asp and execute that file.
References
https://www.w3schools.com/asp/met_execute.asp
https://www.w3schools.com/ASP/asp_ref_filesystem.asp

Latex - labeling tables while using foreach loop

I have a loop in latex, where I access 5 different tables to include in my document. The look has two elements - one variable indicating short name of the category (\n which can be A, O, I, R or H) and the variable that has the long name (\m, which can be "Apartment", "Office", etc).
This loop works as intended for caption and for input. But it does not work for "\label". In other words, the loop produces 5 tables, pulling the right files each time. It puts correct caption on these tables (Apartment, Office, etc), but \label does not get populated correctly. It produces only one label as "output_reg\n" instead of 5 labels as "output_reg_A", "output_reg_O", etc.
I would appreciate all the help I can get!
\documentclass{article}
\usepackage{tikz}
\begin{document}
\foreach \n\m in {A/Apartments,O/Office,R/Retail,I/Industrial,H/Hotel}
{ \begin{table}
\small
\centering
\caption{Regression results \n - \m } \label{output_reg_\n}
\begin{tabular}{ccccc}
a & a & \\
a & a &
\end{tabular}
\end{table}
}
content
% I want to be able to reference the tables as \ref{output_reg_A} and \ref{output_reg_O and so on.
\end{document}

3 part formula for linking string data from different / same cell

A1
Michael Lewis
B1
'at'xyz
I would like an individual formula to do each of the following
Michael.lewis#xyz.com (First Name ‘dot’ Surname)
M.lewis#xyz.com (First initial ‘dot’ Surname)
mlewis#xyz.com (First initialSurname)
Id like a drop down menu to change which formula I am using.
Is this possible?
I'm not sure if I get exactly what you need, but here goes.
I had to translate all the formulas back to english because my work computer is in a different locale but I think it's right (in case there's a typo I missed)
The formulas you asked for in respective order:
(B11 = name, B12 = email domain)
=LEFT(B11;SEARCH(" ";B11)-1) & "." & RIGHT(B11; LEN(B11)-SEARCH(" ";B11))& "#" &B12
=LEFT(B11;1) & "." & RIGHT(B11; LEN(B11)-SEARCH(" ";B11))& "#" &B12
=LEFT(B11;1) & RIGHT(B11; LEN(B11)-SEARCH(" ";B11))& "#" &B12
Then you can just add a data validation list pointing to the cells you write the formulas on and it should work as intended, I hope.
Accessing first name in cell A1:
= LEFT(A1,FIND(" ",A1)-1)
Accessing last name in cell A1:
= RIGHT(A1,FIND(" ",A1)-1)
Accessing first initial in cell A1:
= LEFT(A1,1)
Getting passed the 'at' in cell B1 and only returning xyz:
= RIGHT(B1,5)
Therefore, for example, <first name>.<last name> is:
= LEFT(A1,FIND(" ",A1)-1)&"."&RIGHT(A1,FIND(" ",A1)-1)&"#"&RIGHT(B1,5)&".com"
If you remove the 'at' from cell B1 and just have xyz, then the formula above could be slightly simplified; You can substitute RIGHT(B1,5) simply with B1.
Also, the LOWER function converts all text to lowercase.
For example to get the lowercase first letter of first name, formula would be:
= LOWER(LEFT(A1,1))

VBA assign Formula to Variable then use the Variable to evaluate the Formula

I require some help please.
Using Excel 2007.
I'm not even sure this can be done.
I need to maintain efficiency and speed else the application will not be used.
I have a 'IF' statement to test for a criteria.
depending on the results want to assign one of several possible Formulas to a Variable for later use.
I then need to have the Variable to Evaluate the Formula so that the result can be stored in a second variable to build an array.
Sample Code:
If (varEmployeeName = "(All)" Or varEmployeeName = "(Multiple Items)") Then
**varArrayFormula = "Application.WorksheetFunction.SumIfs(wsSumRange, wsLookupRange, wsLookupValue)"**
Else
**varArrayFormula = "Application.WorksheetFunction.SumIfs(wsSumRange, wsLookupRange, wsLookupValue, wsLookupRange2, varEmployeeName)"**
Then Later on this code will execute:
ReDim varKPIArray(0 To varLastRow) ' creates the array size
For varCol = 10 To 13
For varRow = 8 To varLastRow
varDailyKPIHeading = ws.Cells(7, varCol).Value
Select Case varDailyKPIHeading
Case "PRODUCTIVITY"
Set wsSumRange = ThisWorkbook.Worksheets("DailyProductivity").Range("$G:$G")
Set wsLookupRange = ThisWorkbook.Worksheets("DailyProductivity").Range("$F:$F")
wsLookupValue = ws.Range("B" & varRow).Value
**varSumifValue = Application.Evaluate(varArrayFormula)**
varKPIArray(varRow - 8) = varSumifValue
Case "HOURS"
The program will then loop through several columns and rows hence why the variables are needed. I have 2 options at this point:
1) is to build an array and then I can paste the array results in the appropriate cells with a second loop.
or
2) As I am loop through each cell I Populate the formula results to the cell
Thank you everyone for you help, suggestions and ideas
Use this as your formula strings:
"SumIfs(" & wsSumRange.Address(0,0) & "," & wsLookupRange.Address(0,0) & "," & wsLookupValue & ")"
And:
"SumIfs(" & wsSumRange.Address(0,0) & "," & wsLookupRange.Address(0,0) & "," & wsLookupValue & "," & wsLookupRange2.Address(0,0) & "," & varEmployeeName & ")"
Thanks to all who gave their suggestions and Help.
Ultimately i was not able to get anything to work and ended up designing and rewriting the code.
It was hoped that there was a simply solution of applying a Formula with variables to a variable so that one did not have to keep retyping the formula and could just reference the variable that contained the formula.
I will post my solution shortly once i have the code finalized and debugged.

Lcase operation on array taking too long

I'm working with a script designed to compare values returned from a form against values from a database dumped to an array, via GetRows. The purpose of the check is to compare form values against database values and only update the matching ids' rows in the database.
I've seen this done with hidden variables in forms, but as we have quite a few users online at any given time, the values on the db end could change while a user was completing the form.
Currently, the code uses an inner and outer loop to run this comparison, with a temporary variable being assigned the current col/row from the aforementioned array. An lcase and trim operation are performed on the value to obtain the temporary variable.
This is causing a considerable performance drain, and I was wondering if the lcase/trim functionality could perhaps be performed during the creation of that array, rather than in a looping situation?
Here's my code:
**note: this utilizes the FastString Class for concatenation, thus the "FastString" and ".Append"
dim iRowLoop, iColLoop, zRowLoop, strChange, tempDbValsCase
Set strChange = New FastString
for iRowLoop = 0 to ubound(arrDbVals, 2)
for zRowLoop = 0 to ubound(arrFormComplete)
''#****below line is what is causing the bottleneck, according
''#****to a timer test
tempDbValsCase = lcase(trim(arrDbVals(1, iRowLoop)))
''#****
if (mid(trim(arrFormComplete(zRowLoop)),1,8) = trim(arrDbVals(0, iRowLoop))) AND (mid(trim(arrFormComplete(zRowLoop)),9) <> tempDbValsCase) then
dim strFormAllVals
strFormAllVals = arrFormComplete(zRowLoop)
strChange.Append strFormAllVals & ","
end if
next
next
On the database side (MS SQL Server 2008), the table from which the array is derived through GetRows contains the bit datatype column "Complete". The lcase and trim operations are performed upon this column of the array. Does the bit datatype add any hidden characters in the output? Visually, I don't detect any, but when I compare a value of "True" from the form input against a value from the array that looks like "True," it doesn't match, until I run the lcase and trim on the "Complete" column.
Try
dim iRowLoop, iColLoop, zRowLoop, strChange, tempDbValsCase
dim iCount1, iCount2, match
Set strChange = New FastString
iCount1 = ubound(arrDbVals, 2)
iCount2 = ubound(arrFormComplete)
for iRowLoop = 0 to iCount1
for zRowLoop = 0 to iCount2
' Assign array lookup to a variable '
tempDbValsCase = arrDbVals(1, iRowLoop)
' ...and then perform operations on it one at a time '
tempDbValsCase = trim(tempDbValsCase)
tempDbValsCase = lcase(tempDbValsCase)
' Assign this array lookup to a variable and perform trim on it '
match = trim(arrFormComplete(zRowLoop))
if (mid(match,1,8) = trim(arrDbVals(0, iRowLoop))) AND (mid(match,9) <> tempDbValsCase) then
strChange.Append match & ","
end if
next
next

Resources