Get URL parameters in VBA - html

I need to get the ID parameter in a URL, for example I have
http://apps/inventory/others.aspx?ID=8678
How do I extract the 8678, I've looked at the method of the Object WinHttp.WinHttpRequest.5.1 but I haven't found anything. Could that be possible with a simple substring? The URL is always the same and there is always one GET parameter,
Thanks

Try like this:
Option Explicit
Public Sub TestMe()
Debug.Print ExtractAfter("http://apps/inventory/others.aspx?ID=8678", "ID=")
Debug.Print ExtractAfter("http://apps/inventory/others.aspx?ID=867843", "ID=")
End Sub
Public Function ExtractAfter(strInput As String, strAfter As String) As String
ExtractAfter = Mid(strInput, InStr(strInput, strAfter) + Len(strAfter))
End Function
This is what you get in the immediate window:
8678
867843

In VBA, assuming you have the url in a variable url:
Debug.Print Mid(url, InStr(url, "ID=") + 3)
However, works only correct if the ID parameter is always present and always the only paramter, else you need some more sophisticated string handling.

Related

OnlyDigits Function in Criteria

My database has a job number field that consists of year+month/serialnumber+type. There can be multiple jobs with the same job number:
201812/6Door
201812/6Stair
201812/6Wardrobe
When the user wants to change the date of any/all of these records I want all 201812/6 jobs to show in a form.
I have successfully used the OnlyDigits function below to pull only numbers from the text field: OnlyDigits(JobNumber) = 2018126. But I can't figure out how to filter the form to show all jobs containing 2018126.
I have tried using this query but get an error saying expression typed incorrectly or is too complex.
SELECT onlydigits(jobnumber) AS JobNumberDigits, tbldelivery.DelDateDoors, tbldelivery.Lag, tbldelivery.ProductionDate, tbldelivery.OrderNumber, tbldelivery.JobNumber
FROM tbldelivery
WHERE (((onlydigits(jobnumber))=OnlyDigits([Forms]![tblDelivery]![JobNumber])));
I also tried using a where expression in Docmd.OpenForm but that didn't work either. Can anyone suggest how I use this function to filter?
Public Function OnlyDigits(ByVal pInput As String) As String
Static objRegExp As Object
If objRegExp Is Nothing Then
Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
.Global = True
.Pattern = "[^\d]"
End With
End If
OnlyDigits = objRegExp.Replace(pInput, vbNullString)
End Function
Try specifying the parameter to free Access from guessing:
PARAMETERS
[Forms]![tblDelivery]![JobNumber] Text;
SELECT
OnlyDigits(JobNumber) AS JobNumberDigits,
tbldelivery.DelDateDoors,
tbldelivery.Lag,
tbldelivery.ProductionDate,
tbldelivery.OrderNumber,
tbldelivery.JobNumber
FROM
tbldelivery
WHERE
OnlyDigits(JobNumber)=OnlyDigits([Forms]![tblDelivery]![JobNumber]);
or:
WHERE
OnlyDigits(CStr(Nz(JobNumber, 0)))=OnlyDigits([Forms]![tblDelivery]![JobNumber]);

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

VBA function call

Is there a way to call a function, where the call is stored in a table
**Record 1 task Function call**
124567 Email customer Call function emailcus(a,b,c,d)
434535 AddCost Call function addcost(a,b,c,d)
Cheers
Graham
Yes, you can use the Eval() function for that.
Syntax:
Dim ReturnValue As String
ReturnValue = Eval("MyFunction(1, 2)")
Note that you have to provide the exact function call including parameters.
I'm pointing this out because I'm not sure if the parameters a, b, c, d in your example are only dummy values for your example, or if you expect VBA to fill in the values of some variables named a, b, c, d automatically.
The Eval function does not do this, so if you need variable values as parameters, you would have to do something like this:
Dim ReturnValue As String
Dim EvalString As String
EvalString = "MyFunction(" & Variable1 & ", " & Variable2 & ")"
ReturnValue = Eval(EvalString )
This is a variation on the answer already given by haarrrgh, so if you find it useful be sure to upvote that one as well.
There's another way to deal with placeholders in your DB-stored function calls. First, change your data thusly:
**Record 1 task Function call**
124567 Email customer Call function emailcus([TokenA],[TokenB])
434535 AddCost Call function addcost([TokenA],[TokenB])
Note that the [SquareBrackets] are not actually required syntax in this example, just something that I tend to use in this situation. The important part is to make the parameter tokens something that doesn't appear elsewhere in the string value (including other tokens). You can use as many parameters as you need, just make sure that the calling code knows about how many are expected by each function-call string (I cut it down to shorten my following code).
Then when it's time to call your function, do this:
Dim ReturnValue As String 'or as appropriate for individual the function's return
Dim EvalString As String
EvalString = 'code to fetch from table
EvalString = Replace(EvalString, "[TokenA]", strValueA) 'strValueA passed in?
EvalString = Replace(EvalString, "[TokenB]", strValueB) 'strValueB passed in?
ReturnValue = Eval(EvalString)
In VB6, at least (so I assume it's true in VBA), Replace is faster than concatenation. I also find this more readable, but that may be because I'm used to it from using a similar technique to build SQL commands in code (using Const declarations rather than DB storage, but that would work too).
EDIT
As I reread my "finished" post just after submitting it, I realized that there's a gotcha lurking in there. Because you're doing substitution before submitting the string to Eval, these are actual values that are being put into the string, not variables. The code I presented above works fine if your parameters are numeric, but if they're String type you have to include the quotes, either in your data or in your Replace call. I prefer the former, so change your data to this:
**Record 1 task Function call**
124567 Email customer Call function emailcus('[TokenA]','[TokenB]')
434535 AddCost Call function addcost('[TokenA]','[TokenB]')
This works as tested with a Const. Stored in a DB record, you might need this instead:
**Record 1 task Function call**
124567 Email customer Call function emailcus(""[TokenA]"",""[TokenB]"")
434535 AddCost Call function addcost(""[TokenA]"",""[TokenB]"")
(which also works with a Const...).
The alternative is to leave the data as it is in my first part, & change the Replace calls:
EvalString = Replace(EvalString, "[TokenA]", """" & strValueA & """") 'strValueA passed in?
'or maybe
EvalString = Replace(EvalString, "[TokenB]", "'" & strValueB & "'") 'strValueA passed in?
A couple of other potential gotchas: These must be Functions, not Subs, and they must be declared Public in a module, not in a Form's code.