Is there a way to evaluate a variable in vba? - ms-access

I have a variable newItem. I want to place the value stored in newItem into a string. I thought I would be able to accomplish this with...
myString = eval(newItem)
...but it doesn't work.
Is there any way to evaluate a variable in Access vba?

Eval is a function that will execute the text in the given string as if it were code.
I think what you are looking for is CInt:
Dim s as String : s = "15"
Dim i as Integer : i = CInt(s)
'at this point, i = 15, and s = "15"
Similarly, you should look into CStr, CLng, CDate, etc.

Sure is. you need to typecast the variable to a string.
In VBA you use myString = CStr(var)
I believe these are all the typecasts in vba:
CBool(expression)
CByte(expression)
CCur(expression)
CDate(expression)
CDbl(expression)
CDec(expression)
CInt(expression)
CLng(expression)
CSng(expression)
CVar(expression)
CStr(expression)

You can use the Script Control for that too :)

Related

VBA Access - Array via textbox

At the moment all my arrays are implemented in the vba code. This works fine
Dim COST As String
Dim GAT As String
Dim OND As String
COST = "C:\Users\update\COST.xlsb"
GAT = "C:\Users\BACKUP\GAT.xlsb"
OND = "C:\Users\BACKUP\OND.xlsb"
MyArray = Array(COST, GAT, OND)
However I would like to select the above arrays from a textbox via a form
Below code works but I have to implement the file path
MyArray = Array(Forms![LAYOUT_F]![Update_F])
with file path
MyArray = Array("C:\Users\BACKUP\" & Forms![LAYOUT_F]![Update_F])
Using the above will require me to be file name specific and doesn't take into account arrays plus the file paths are different
Is there a way of selecting declared arrays from a textbox
I would use a multi-line textbox, where the user e.g. enters
C:\Users\update\COST.xlsb
C:\Users\BACKUP\GAT.xlsb
and then use the Split() function on the line breaks:
MyArray = Split(Me!myTextbox, vbCrLf)
But I my have misunderstood your question - not sure what you mean by "selecting declared arrays from a textbox".

I can't use the UBound() function in VBA Access. Seems like it's not recognised

I work on access 2013 and I have a problem using UBound(). The compiler doesn't recognise the function. Maybe there is a specific library this function is from, but it wasn't mentionned on microsoft website.
Here is the code I used :
Dim Key(2) As String
Key(0) = "0"
Key(1) = "1"
Key(2) = "2"
UBound(Key,1)
Does anyone has the same issue?
Thanks!
It is a function that you need to assign it's return to something.
Dim i As Integer
i = UBound(Key, 1)
You can also use it's return as a variable
For i = 1 to UBound(Key, 1)

NOT IN in SSRS TextBox

How can I write NOT IN in TextBox expression?
I must check if some field value not belong to some list of strings, and then do some work.
Example:
Iif(SomeField.Value NOT IN ('Text1', 'Text2'), DoSomething, nothing)
I wrote code like this and got error when previewing report, and error was :
Overload resolution failed because no accessible 'Iif' accepts this number of type arguments
How can I do this stuff?
Try this small piece of custom code that accepts a string array. Just paste it into the report code section of the report..
Public Shared Function ValueExists(ByVal arr() as string, checkVal as string)
Dim i As Long
For i = LBound(arr) To UBound(arr)
If arr(i) = checkVal Then
return true
Exit Function
End If
Next i
return false
End Function
Usage would involve splitting the string into an array using the Split function
like so:
=iif(Code.ValueExists(Split("Your,comma,separated,string,in,here",","),"StringYouWantToFind")
,"Your value exists"
,"your value does not exist")
You can simply write the code like this:
Iif(SomeField.Value <> 'Text1' AND Field.Value <> 'Text2' , DoSomething, nothing)
I got this one in one report:
=iif(join(Parameters!Parameter1.Value,",") like "*" & Fields!Field1.Value & "*","Color1","Color2")
This instruction helps me to determine the fill colour of a cell inside a tablix, where:
Parameter1 is a multivalue parameter.
"Join" lets me have a string with all selected values from a multivalue parameter, eg. "value1,value2,value3,value4"
Field1 is the field that contains the values filtered by Parameter1
Color1 is the color if the value of the cell is included in the selection of parameter
else Color2
works well

SSIS Convert Blank or other values to Zeros

After applying the unpivot procedure, I have an Amount column that has blanks and other characters ( like "-"). I would like to convert those non-numberic values to zero. I use replace procedure but it only converts one at the time.
Also, I tried to use the following script
/**
Public Overrides Sub Input()_ProcessInputRows(ByVal Row As Input()Buffer)
If Row.ColumnName_IsNull = False Or Row.ColumnName = "" Then
Dim pattern As String = String.Empty
Dim r As Regex = Nothing
pattern = "[^0-9]"
r = New Regex(pattern, RegexOptions.Compiled)
Row.ColumnName = Regex.Replace(Row.ColumnName, pattern, "")
End If
End Sub
**/
but i'm getting error.I don't much about script so maybe I placed in the wrong place. The bottom line is that I need to convert those non-numberic values.
Thank you in advance for your help.
I generally look at regular expressions as a great way to introduce another problem into an existing one.
What I did to simulate your problem was to write a select statement that added 5 rows. 2 with valid numbers, the rest were an empty string, string with spaces and one with a hyphen.
I then wired it up to a Script Component and set the column as read/write
The script I used is as follows. I verified there was a value there and if so, I attempted to convert the value to an integer. If that failed, then I assigned it zero. VB is not my strong suit so if this could have been done more elegantly, please edit my script.
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
' Ensure we have data to work with
If Not Row.ColumnName_IsNull Then
' Test whether it's a number or not
' TryCast doesn't work with value types so I'm going the lazy route
Try
' Cast to an integer and then back to string because
' my vb is weak
Row.ColumnName = CStr(CType(Row.ColumnName, Integer))
Catch ex As Exception
Row.ColumnName = 0
End Try
End If
End Sub

How do I set a variable to one of my form's listboxes in vba(access)?

I have a function that I want to return different Listboxes based on a string argument.
Here is the function:
Here is the function:
Private Function returnList(name As String) As AccessObject
If name = "app" Then
returnList = Me.Controls("List61")
'I have also tried the following:
'returnList = Me.List61, returnList = Forms![Daily Reports]![List61]
ElseIf name = "lpar" Then
'..several more cases
End If
End Function
Whenever I try to call it, I get a "Run-time error '91': Object variable or With block variable not set." And when I use the debugger, it tells me that the reference to list61(Me.list61, Me.Controls("List61")) is null.
Anyone have any idea how to fix this? Any help would me much appreciated.
The most important thing to note is; as you are now handling "Objects" instead of "variables" you have to put the word "Set" in front of the object variable. Also change the AccessObject type to ListBox.
Private Function returnList(name As String) As ListBox
If name = "app" Then
Set returnList = Me.Controls("List61")
'I have also tried the following:
'returnList = Me.List61, returnList = Forms![Daily Reports]![List61]
ElseIf name = "lpar" Then
'..several more cases
End If
End Function