Checking the passed variable - vba

Is it possible to check if the passed variable in a function is a specific text? For example, I have a function:
Function dateCheck2(dateValue As String) As Boolean
If IIf(IsDate(frmResponse.txtEndDate), Format(CDate(frmResponse.txtEndDate), _
"mm\/dd\/yyyy"), "") = dateValue Then
dateCheck2 = True
Exit Function
End If
End Function
Then I have to call this in a UForm, lets say :
dateCheck2(sample)
What I want is to check if the passed variable is sample. Is there any way to do that?

Or you can create your own class (in VBA its called data type) which will hold variable name and values. Something like this
Public Type myFluffyType
fluffyName as string
fluffyValue as string
end Type
Then you can dim your variable like this
Dim fluffyVariable as myFluffyType
and initialize
fluffyVariable.fluffyName = "fluffyVariable"
fluffyVariable.fluffyValue = "notSoMuchFluffyValue"
and then when you passed it into your function you have all informations you need
Edit: on your problem you can do something like this. But im not sure what you exactly wana do next
Public type myType
varName as string
varValue as string
end type
Sub callDateCheck2
Dim sample as myType
sample.varName = "sample"
sample.varValue = "whateverValues"
Call dateCheck2(sample)
end sub
Function dateCheck2(dateValue As myType) As Boolean
if dateValue.varName = "sample" then
msgbox "everything ok"
else
msgbox "bad variable name"
end function
end if
If IIf(IsDate(frmResponse.txtEndDate), Format(CDate(frmResponse.txtEndDate), _
"mm\/dd\/yyyy"), "") = dateValue Then
dateCheck2 = True
Exit Function
End If
End Function

Related

How do I access a returned variable from a different function within a function?

I have a function "A" that retrieves an integer value from a textbox in SSRS:
Dim currasm As Integer = Nothing
Function GetAsmCurrent(ByVal currasm As Integer) As Integer
Return currasm
End Function
...and a second function "B" where I would like to use the value returned from function "A".
Dim lastsn As String = Nothing
Function GetSN(ByVal currentsn As String) As String
Dim currasm1 As Integer = Nothing
' <------ Here is where I thought I could call the function ------>
currasm1 = GetAsmCurrent(currasm)
' <--------------------------------------------------------------->
If Not currasm1 = Nothing And currentsn = Nothing Then "NO SN"
ElseIf Not currentsn = Nothing Then
lastsn = currentsn
End If
Return lastsn
End Function
It always pulls a 0 in, instead of 1, 2, etc. depending on the assembly number in the textbox on the report page.
Private currasm As Integer
Function GetAsmCurrent(ByVal currasm As Integer) As Integer
Return currasm
End Function
This function will return the value passed to it, not the class level variable. The code will always seek the variable in the closest scope. Sort of silly to return what is passed. Again, as I stated in comments, a value type has a default value, in this case 0 and cannot be set to Nothing unless nullable. It is customary to use Private instead of Dim for class level variables.
Private lastsn As String = Nothing
Function GetSN(ByVal currentsn As String) As String
Dim currasm1 As Integer
currasm1 = GetAsmCurrent(currasm)
If Not currasm1 = 0 And currentsn = Nothing Then
currentsn = "NO SN"
ElseIf Not currentsn = Nothing Then
lastsn = currentsn
End If
Return lastsn
End Function
In this function GetAsmCurrent(currasm) will pass the value of the class level variable and return the same value. Might be easier to just say currasm1 = currasm assuming the 2 functions are in the same class. As discussed above currasm1 cannot be Nothing.
This will not compile "NO SN". What could that possibly mean? Did you intend to assign that string to some variable? I will assume you intended to assign it to currentsn.
If you are calling this function within the same class, it can be a Sub. Any method in the class can access class level variables.

Compile error with writing return function

I want to create a function where I use one parameter so the function returns another value. In this case I want to insert "links" in a function and then the function should give me back "Left" (Im need a of 'translations' so therefore I use case of if statements.
I have the following function
Public Function newParameter(aParameter) As Integer
Select Case aParameter
Case Is = "Links"
aParameter = "ppAlignLeft"
End Select
End Function
Sub finalFunction()
Dim newParameter As String
newParameter = newParameter(Links)
MsgBox (newParameter)
End Sub
This however gives me a compile error that a matrix is required. Any thoughts on how I can get this working?
You're passing the argument ByRef and change it's value. You dont need a function for this. Functions retun values.
Since you are using a Function though, why dont you have the Function return the value you want?
Public Function newParameter(ByVal aParameter As String) As String
Select Case aParameter
Case "Links":
newParameter = "ppAlignLeft"
Case "A":
newParameter = "You passed A"
Case Else:
newParameter = "No match"
End Select
End Function
Sub finalFunction()
Dim newParameter1 As String
newParameter1 = newParameter("Links") 'This reads 'ppAlignLeft'
MsgBox newParameter1
End Sub

Having an optional parameter in a function

Just a very quick question: I want to create a function with an optional parameter because I can't find a need for a parameter in the function. As a result I have coded the following function in visual basic:
Sub characterListLength(ByVal Optional)
Dim rowCount As Integer
Dim endOfArray As Boolean
While endOfArray = False
If dataArray(0, rowCount) And dataArray(1, rowCount) = "" Then
arrayLength = rowCount
endOfArray = True
Else
rowCount += 1
End If
End While
End Sub
However on the first line:
Sub characterListLength(ByVal Optional)
There is an error where an identifier is expected where the code says (ByVal Optional). I am not sure how to fix this error and have the optional parameter. If anyone could explain what else I need to do to fix it, that would be very useful.
You need an actual variable, something like:
Sub characterListLength(Optional ByVal optionalNumber As Integer = 0)
If you said:
because I can't find a need for a parameter in the function
Then use method without parameters:
Sub characterListLength()
'Here your code
End Sub
You need to give the parameter a name and switch the order of the keywords
Sub characterListLength(Optional ByVal p = Nothing)
A better "dot-nettier" alternative to optional parameters is to use overloaded methods. Consider following:
Overloads Sub ShowMessage()
ShowMessage("This is the default alter message")
End Sub
Overloads Sub ShowMessage(ByVal Message As String)
Console.WriteLine(Message)
End Sub
Written like this you can call the above method both ways:
ShowMessage() 'will display default message
ShowMessage("This is custom message") 'will display method from the parameter
Demo: http://dotnetfiddle.net/OOi26i

Access variables of sub in VBA

I have the following sub
Public Static Sub Varib()
Device_ = Sheet1.DeviceType_.Text
Model_ = Sheet1.Model_.Text
Security_ = Sheet1.SecurityGroup_.Text
Catagory_ = Application.Index(Worksheets("Temp_for_varible_lists").Range("b:b"), Application.Match(x, Worksheets("Temp_for_varible_lists").Range("A:A"), 0))
End Sub
It in fact carries on and in total produces a whole bunch of vaules of various datatypes based on the users input.
So the user choses from a few check boxes, list boxes, fills in some text boxes and hits a submit button and this sub populates a number of varibles from that, that are then uterlised by other funcation and sub in the application.
Now I could make all the varibles Global and access them in that fassion. But I was hoping for something more like what I have seen with c# and VB.net
where you can get the value by using
sub.varible name
example for the code above.
Sub Main()
x = Varib.Device_
msgbox(x)
end sub
is there a simmular way to do this in VBA?
Cheers
aaron
What you're asking cannot be done. The solution is not to make your variables global either (generally a bad idea, with some exceptions, this case not being one of them).
One possibility is to create a user-defined type:
Type Varib
Device_ As String
Model_ As String
Security_ As String
Category_ As String
End Type
and a sub to populate it from your sheet:
Sub LoadVaribFromSheet(v As Varib)
With v
.Device_ = Sheet1.DeviceType_.Text
.Model_ = Sheet1.Model_.Text
.Security_ = Sheet1.SecurityGroup_.Text
.Category_ = _
Application.Index(Worksheets("Temp_for_varible_lists").Range("b:b"), _
Application.Match(x, _
Worksheets("Temp_for_varible_lists").Range("A:A"), 0))
End With
End Sub
You can then use this as follows:
Sub Main()
Dim myVarib As Varib
LoadVaribFromSheet myVarib
' Now do stuff with myVarib ...
MsgBox myVarib.Device_
End Sub
you can use encapsulation for this
Private value As String
Private value1 As String
Public Function setValue(val As String)
value = val
End Function
Public Function setValue1(val As String)
value1 = val
End Function
Public Function getValue() As String
getValue = value
End Function
Public Function getValue1() As String
getValue1 = value1
End Function
-------------------------------------------------------------------------
Sub test()
MsgBox getValue & vbCrLf & getValue1
setValue "myValue"
setValue1 "myValue1"
MsgBox getValue & vbCrLf & getValue1
End Sub

No return keyword for this Function in Visual Basic .NET

I've been doing some code review and this code seemed weird to me since it doesn't have any return statement:
Protected Function AddZero(ByVal vsInput As String) As String
If Len(vsInput) = 1 Then
AddZero = "0" & vsInput
Else
AddZero = vsInput
End If
End Function
Visual Basic treats the function name as the return value and does not return until the end of the function. In the code, you can see that AddZero (the function name) is set to one of two values depending on the if condition. That is how you can determine what is returned.
In VB, you have an implicit return at the end of the function.
The function name gets assigned the return value, like this:
Protected Function AddZero(ByVal vsInput As String) As String
AddZero = "0" ' The return value is "0"
End Function
You can exit a function (return) like this:
Protected Function AddZero(ByVal vsInput As String) As String
If vsInput = "0" Then
AddZero = vsInput;
Exit Function
End If
AddZero = "0" ' The return value is "0"
End Function