Visual Basic How To's

Our products support Microsoft Visual Basic .Net in both Function Datasources and in Scripts. Here are some online resources that might help you write visual basic, and below you will also find a few examples.

Click on any of the articles to get the full text and example code.

Functions

Get the left part of a text
Description: The function below returns the left part of a datasource, commonly used to get the first characters of a postal code. In this example the left 3 characters are returned.
Syntax: Dim result As object = ""
result = LEFT( [Field], 3 )
return result
Returns: 123 for [Field] = 12345
Fill a new field out of either one of two others
Description: The function below returns fills a new datasource with the contents of one of two other fields of which one is filled and the other empty.
Syntax: Dim result As object = ""
if Trim( [field1] ) <> "" then
result = Trim( [field1] )
else
result = Trim( [field2] )
end if
return result
Returns: 123 when [Field1] = 123 and [Field2] is ""
456 when [Field1] = "" and [Field2] is "456"
Remove duplicate lines from a document area datasource
Description: This function checks for duplicate lines anywhere in the address block and removes them.
Syntax: Dim result As object = ""
Dim pos As Integer
Dim clean as String
Dim found as boolean

'set to initial values
clean = ""
pos = 0

'Split the lines
'[Area1] is the Document area that needs to be cleaned of duplicate lines
Dim StringArray() As String = Split([Area1],vbLf)

For Each s As String In StringArray
'run through each line
found = false

'check if the current line is identical to one of the previous lines
for n As integer = 0 To pos - 1
if StringArray(pos) = StringArray(n) Then
'if so, then it was found, setting it to true
found = true
End If
Next

'if not found then add the line, including a line feed
if found = false Then
clean = clean + StringArray(pos) + vbLf
End If

'increase the line counter
pos = pos + 1
'check the next line.
Next s 'check the next line.

return clean
Returns: A clean array without duplicate lines.
Add spaces in a field on a regular interval
Description: With the powerful features of regular expressions this can be done in just one line. Just replace [Counter] for the datasource where you wish to add spaces to. And change the number of . to after how many characters you wish a space. So .... if you wish to have a space after every 4 characters.
Syntax: Dim result As object = ""
result = System.Text.RegularExpressions.Regex.Replace([Field], "....", "$& ")
return result
Returns: 1234 5678 90 for [Field] = 1234567890
Calculate the Dutch 'Acceptgiro' 'Elfproef'
Description: This function adds the check digit based on the eleven-code for the Dutch "Acceptgiro" to a number. For more information on how it is calculated, see this article.
Syntax: Dim result As object = ""
'Field is the numbert to calculate the check digit for
Dim data As String = [Field]

'Trim the string and remove spaces
data = data.Trim()
data = Replace(data, " ", "")

'Extent to 15 characters
data = StrDup( 15 - data.Length(), "0") & data

Dim weights() As Integer = New Integer() {2, 4, 8, 5, 10, 9, 7, 3, 6, 1}

Dim n As Integer = 1
Dim sum As Integer = 0
Dim check As Integer = 0
Dim length As Integer = data.Length()

for each nr as Char in data
sum = sum + ( Val( nr ) * weights( ( length - n ) mod 10 ) )
n = n + 1
Next

check = 11 - ( sum mod 11)

if check = 11 Then
check = 0
else if check = 10 Then
check = 1
End If

return check & data
Scripting

How to print multiple pages based on a database field with a script
Description: The Main() below will print each record x times and x is the value of the [Counter1] field in that database record.
Syntax: Public Sub Main()
'Move the database to the first record
Database.MoveFirst()

'Start the printer
Printer.Open()

Do
'repeat as many times as said in column [Counter1]
For i As Integer = 0 To [Counter1]
'Print command
Printer.Print()
Next
'repeat while there are still records left in the database
Loop While Database.MoveNext()

'stop the printer
Printer.Close()
End Sub
Copyright © 1993 - 2024 Flex Systems B.V. Best viewed in Chrome, IE or FF. HTML5 Powered