Imploding an array into a string using VB - vb6

I'm extremely unfamiliar with ancient VB, and I'm trying to figure out the proper commands to concatenate an array (I am assuming it is in array form) into a string with comma separated values.
The values are being provided by a multiselect box, which is being assigned to the areas variable, which is grabbed from the areas select box.
dim name
dim from
dim company
dim phone
dim zip
dim message
dim areas
name = Request.Form("name")
from = Request.Form("from")
company = Request.Form("company")
phone = Request.Form("phone")
zip = Request.Form("zip")
areas = Request.Form("areas")
message = Request.Form("message")
I want to take areas, and implode it into a string.
What's the command in very old VB to do this?

The Join function does also exist in VB6, the syntax is like this:
myString = Join(myArray, ",")
EDIT: Note that the array goes before the delimiter. The delimiter is optional, it'll be a space if left empty.

you want to use the Join function String.Join(",",array)

If you are using VB6, try this:
Join(New String() {name, from, company}, ", ")
Join Function (Visual Basic) # MSDN
EDIT: I know it's a link to VB.NET, but it's the old VB6 function call, it should be compatible with VB6, syntax wise.

Related

Convert string to variable name in VBScript

How can I convert a string to a variable name in VBscript? I found similar Q/A for JavaScript, C#, Pyton, Perl, Ruby, Java, but not for VBscript.
This is why I can not work directly with variables instead of strings:
There is a list of comma separated permission names which I get from a web service. The project manager may update this list any time and expects that I check if a user of software is granted to these permissions. User's permissions are set in run time by complex methods as well as profile data, history of activities in the software etc. e.g. if a user has bought 1000 products, the variable SuperCustomerPermission will set for him in header of page using SuperCustomerPermission="yes" (not as cookies or session nor stored in a databse).
To check the list of permission I want to pass the list of permission names to a function and loop through the names:
Permission names which I get from a web service:
permissionNames = "adminPermission,deletePermission,configPermission,SuperCustomerPermission"
I try to pass these strings as variables to the subroutine:
Sub checkPermission(PermissionNames)
permissionNamesArray = split(permissionNames,",",-1,1)
For Each x In permissionNamesArray
'How to convert x to its variable equivalent and check if it equals "yes"
Next
End Sub
Call checkPermission(permissionNames)
If it is possible to put it into application or session you could do something like session(variableName) = 5.
If not, the next easiest way is using a dictionary:
Dim myDict
Set myDict= CreateObject("Scripting.Dictionary")
myDict.Add (variableName, value)
Using a dictionary should work fine with your situation and it's much cleaner.
That being said, since you asked for an Execute solution, you can do something like this:
Sub checkPermission(PermissionNames)
Dim permissionNamesArray, x
permissionNamesArray = split(permissionNames,",",-1,1)
For Each x In permissionNamesArray
Dim valueOfX
Execute "valueOfX = " & x
WScript.Echo valueOfX
Next
End Sub
Example of usage:
' Adding some values to the variables
Dim adminPermission, deletePermission, configPermission, SuperCustomerPermission
adminPermission = "yes"
deletePermission = "no"
configPermission = "yes"
SuperCustomerPermission = "no"
' Storing names of variables
Dim permissionNames
permissionNames = "adminPermission,deletePermission,configPermission,SuperCustomerPermission"
' Passing variables by names
checkPermission(permissionNames)
Output:
yes
no
yes
no

hidden space in excel

I tried almost all the methods (CLEAN,TRIM,SUBSTITUTE) trying to remove the character hiding in the beginning and the end of a text. In my case, I downloaded the bill of material report from oracle ERP and found that the item codes are a victim of hidden characters.
After so many findings, I was able to trace which character is hidden and found out that it's a question mark'?' (via VBA code in another thread) both at the front and the end. You can take this item code‭: ‭11301-21‬
If you paste the above into your excel and see its length =LEN(), you can understand my problem much better.
I need a good solution for this problem. Therefore please help!
Thank you very much in advance.
Thanks to Gary's Student, because his answer inspired me.
Also, I used this answer for this code.
This function will clean every single char of your data, so it should work for you. You need 2 functions: 1 to clean the Unicode chars, and other one to clean your item codes_
Public Function CLEAN_ITEM_CODE(ByRef ThisCell As Range) As String
If ThisCell.Count > 1 Or ThisCell.Count < 1 Then
CLEAN_ITEM_CODE = "Only single cells allowed"
Exit Function
End If
Dim ZZ As Byte
For ZZ = 1 To Len(ThisCell.Value) Step 1
CLEAN_ITEM_CODE = CLEAN_ITEM_CODE & GetStrippedText(Mid(ThisCell.Value, ZZ, 1))
Next ZZ
End Function
Private Function GetStrippedText(txt As String) As String
If txt = "–" Then
GetStrippedText = "–"
Else
Dim regEx As Object
Set regEx = CreateObject("vbscript.regexp")
regEx.Pattern = "[^\u0000-\u007F]"
GetStrippedText = regEx.Replace(txt, "")
End If
End Function
And this is what i get using it as formula in Excel. Note the difference in the Len of strings:
Hope this helps
You have characters that look like a space character, but are not. They are UniCode 8236 & 8237.
Just replace them with a space character (ASCII 32).
EDIT#1:
Based on the string in your post, the following VBA macro will replace UniCode characters 8236 amd 8237 with simple space characters:
Sub Kleanup()
Dim N1 As Long, N2 As Long
Dim Bad1 As String, Bad2 As String
N1 = 8237
Bad1 = ChrW(N1)
N2 = 8236
Bad2 = ChrW(N2)
Cells.Replace what:=Bad1, replacement:=" ", lookat:=xlPart
Cells.Replace what:=Bad2, replacement:=" ", lookat:=xlPart
End Sub

Get value of variable named in a string

Is there any way to get the value of a variable that has been named in a string.
For instance:
Dim number As Integer = 12
Dim numberCopy As Integer = Convert("number")
' numberCopy will now equal 12
' Convert is the function to convert the string to the variable
Something like that can be done only with Introspection, and only if the variable holding the value is a property of a class. Your example, where the variable is only temporarily known inside a function, cannot be made work as you like.
To learn more about Introspection, see the documentation and examples of the same name.
You can also use a Dictionary instead of straight properties and variables.
For instance
dim d as new dictionary
d.value("number") = 12
Then when you need the value, you can do
Dim numberCopy As Integer = d.value("number")
Nice thing about Dictionary is since keys as well as values are variant, you can throw at it pretty much anything, all variable types, as well as objects.
See Language Reference

Extracting information from a field using Visual Studio 2005

I need to extract specific information from a field using Visual Studio 2005. I need only the 'partNumber' and the 'serialNumber' from this field;
BeginGroup:databasecurrentFifos=0maximumFifos=34369maximumSpace=6291456percentUsed=7totalFixedSpace=123156totalIndexSpace=77182totalRowCount=2533totalRowSpace=139024totalTableSpace=339362uncollectedFifos=0usedSpace=478386EndGroup:databaseBeginGroup:networkDNS_ServerIP=NoneDNS_DomainName=NoneFTP_Enabled=falseEndGroup:networkBeginGroup:hardwareBeginGroup:biometricsEndGroup:biometricsBeginGroup:mSystemformat=7101processorType=859chipId=CFEndGroup:mSystemBeginGroup:barcodesBarcode_attached=noSymbologies=code3of9
i2of5
code128Remote_Barcode_attached=noEndGroup:barcodesBeginGroup:boardrevision=REV
Aversion=415partNumber=8602800-503serialNumber=JC117590EndGroup:boardBeginGroup:magneticstrackNum=noEndGroup:magneticsBeginGroup:memorySDRAM=134217728BeginGroup:flashDisksflashDisk0=1018773504flashDisk1=0numOfFlashDisks=1EndGroup:flashDisksEndGroup:memoryBeginGroup:peripheralsmodem=noneWand_attached=noEndGroup:peripheralsBeginGroup:keypadKeypad_Type=NumericEndGroup:keypadBeginGroup:proximityReadersProximityReader_attached=noProxReaderFormat1=Default
26 Bit
FormatProximityReader_NumRecords=2RemoteProx_attached=noRemoteProxFormat1=Default
26 Bit
FormatRemoteProx_NumRecords=2EndGroup:proximityReadersEndGroup:hardwareBeginGroup:softwarerelease=03.00.07.006
HTMLClientVersion=5708model=Kronos-4500OSversion=5708bootVersion=5708appVersion=5708dbSchema=5708Font_Information=Default
Latin fonts. EndGroup:software
This answer assumes VB.NET is used and also assumes that structure of the source string doesn't change.
'Variable to hold source data
Dim sSource As String
'*** perform code here to load source (from DB etc.) ***
'separator array (change this as needed if sructure of the source sting changes)
Dim sSep As String() = {"partNumber=", "serialNumber=", "EndGroup:boardBeginGroup:magnetic"}
'split the source into Array
Dim asSplit As String() = sSource.Split(sSep, StringSplitOptions.None)
'get Part Number
Dim sPartNumber As String = asSplit(1)
'get Serial Number
Dim sSerialNumber As String = asSplit(2)

Full Search testing in LINQ (custom filter query)

I have a table that has a list of restaurant names and links to another table that holds the cuisine type.
I want to provide a search box on my web page that when typing, each word is searched for in the database and results returned. I was looking for a solution that doesn't involve setting up sql full text search as I want to be able to test this using Linq to SQL.
From my old code I have a function which creates a query filter given the input text and returns all the results based on that.
Private Function SetupQuery(ByVal searchText As String) As String
Dim searchFields As New List(Of String)
searchFields.Add("Name")
searchFields.Add("Postcode")
searchFields.Add("Cuisine")
Dim firstCol As Boolean = True
Dim a() As String
Dim j As Integer
a = searchText.Trim.Split(" ")
Dim filter As String = ""
Dim compareString As String
For Each col As String In searchFields
For j = 0 To a.GetUpperBound(0)
compareString = a(j).ToUpper()
compareString = compareString.Trim()
If firstCol Then
filter = filter & col & " LIKE '" & compareString & "%' "
firstCol = False
Else
filter = filter & " or " & col & " LIKE '" & compareString & "%' "
End If
Next
Next
Return filter
End Function
This has the search fields hard coded and then loops through each one and each word in the search text to construct an OR LIKE filter.
I'm sure I could then use this in my LINQ code but it doesn't seem like an elegant solution especially since the columns are hard coded and not used how LINQ uses them.
Could anyone recomend a better way to do this or some tips in what direction to go with this?
Thanks
I don't how know this will affect your testing, but LinqtoSQL can be made to work with full-text indexing with a few wiggles:
LinqToSql and full text search - can it be done?
http://sqlblogcasts.com/blogs/simons/archive/2008/12/18/LINQ-to-SQL---Enabling-Fulltext-searching.aspx
You could use full text search and still use LINQ to SQL. You can create a stored procedure and have LINQ call it. In addition to finding exact matches you could return:
Simple searches for specific words or phrases
Thesaurus searches for synonymous forms of word – a search on IE might return hits to Internet Explorer and IE (thesaurus based expansion search); a search on Bombay might also return hits to Mumbai (thesaurus based replacement search)
Searches that will return all different linguistic forms of a word (called generations) search on bank would return hits to banking, banked, banks, banks' and bank's, etc. (all declensions and/or conjugations of the search term bank)
Accent insensitive searches – a search on café would return hits to cafe and café
http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx
http://www.simple-talk.com/sql/learn-sql-server/sql-server-full-text-search-language-features/
This probably isn't the best way to do this, but if you're always getting the search text as string split into an array of three then you could try using this:
from t in temp
where t.Name == a(0)
|| t.Postcode == a(1)
|| t.Cuisine == a(2)
select t
I normally do C#, so naturally anything in VB scares, but I think the LINQ syntax should be similar

Resources