Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim x As Integer
Dim ary(6) As String
Dim aryNew(6) As Single
ary(0) = "0100-(26.867)-AAA"
ary(1) = "0101-(6.155)-BBB"
ary(2) = "0102-(40)-CCC"
ary(3) = "0103-(5.048)-DDD"
ary(4) = "0143-(12.949)-AAA"
ary(5) = "0146-(9.677)-DDD"
ary(6) = "0149-(12.039)-FFF"
'Pick just the numbers from (xxx)
For i = LBound(ary) To UBound(ary)
aryNew(i) = Mid(ary(i), 7, InStr(ary(i), ")") - 7)
Next i
'Show what we have
For i = LBound(aryNew) To UBound(aryNew)
Debug.Print aryNew(i)
Next i
Call Sort(aryNew)
'Show sorted numbers
For i = LBound(aryNew) To UBound(aryNew)
Debug.Print aryNew(i)
Next i
Combo1.Clear
For i = LBound(aryNew) To UBound(aryNew)
For x = LBound(ary) To UBound(ary)
If InStr(ary(x), aryNew(i)) Then
Combo1.AddItem ary(x)
End If
Next x
Next i
End Sub
Private Sub Swap(a As Variant, b As Variant)
Dim t As Variant
'Swap a and b
t = a
a = b
b = t
End Sub
Private Sub Sort(arr As Variant)
Dim x As Long, Y As Long, Size As Long
On Error Resume Next
'Get array size
Size = UBound(arr)
'Do the sorting.
For x = 0 To Size
For Y = x To Size
If arr(Y) < arr(x) Then
'Swap vals
Call Swap(arr(x), arr(Y))
End If
Next Y
Next x
End Sub
Option Explicit
'
'VConvert
'--------
'
'A VB6 class used to convert between Variant types using a specific
'locale instead of the current program locale.
'
'This is most useful with LOCALE_INVARIANT for non-String typed data
'that is perisisted or communicated as String data. This avoids
'cross-cultural factors related to language, the decimal point
'character, etc.
'
'It can also be used with conversions of data between a user
'interface and data where your code assumes a fixed locale and you
'want to insist users work in that locale even when the user locale
'is an entirely different one. Note that this is not a recommended
'practice for most software!
'
'Example:
'
' Program always expects dates to be entered in U.S. format,
' i.e. mm/dd/yyyy but some users have U.K. settings which make
' locale-aware conversions such as CDate() expect strings in
' dd/mm/yyyy format.
'
Private Const S_OK As Long = 0
Private Const VARIANT_ALPHABOOL As Long = &H2&
Private Const VARIANT_LOCALBOOL As Long = &H10&
Public Enum LOCALE_VALUES
'Add more as you need them here:
LOCALE_INVARIANT = &H7F&
LOCALE_USER = &H100&
LOCALE_SYSTEM = &H200&
LOCALE_GERMANY = &H407&
End Enum
Private Declare Function VariantChangeTypeEx Lib "oleaut32" ( _
ByRef vargDest As Variant, _
ByRef varSrc As Variant, _
ByVal desiredlcid As Long, _
ByVal wFlags As Integer, _
ByVal vt As VbVarType) As Long
Public LCID As LOCALE_VALUES
'Default property:
Public Property Get Convert(ByVal Value As Variant, ByVal ToType As VbVarType) As Variant
If VariantChangeTypeEx(Convert, _
Value, _
LCID, _
VARIANT_ALPHABOOL _
Or VARIANT_LOCALBOOL, _
ToType) <> S_OK Then
Err.Raise 5, TypeName(Me), "Conversion failed"
End If
End Property
Private Sub Class_Initialize()
LCID = LOCALE_INVARIANT
End Sub
Option Explicit
Private VConvert As VConvert
Private List As Collection
Public Sub AddItem(ByVal Value As String)
'Value: String of the form "xxx(dot-grouped-long-number)xxx"
' i.e. a Long value that may have period as a "digit
' group separator char" i.e. the German convention.
Dim PosStart As Long
Dim PosEnd As Long
Dim OrderBy As Variant 'String, then Long.
Dim I As Long
If List Is Nothing Then Set List = New Collection
'This code assumes valid input:
PosStart = InStr(Value, "(")
PosEnd = InStr(PosStart, Value, ")")
OrderBy = Mid$(Value, PosStart + 1, PosEnd - PosStart - 1)
OrderBy = VConvert.Convert(OrderBy, vbLong)
'Fancy searching isn't warranted, since a ComboBox with a huge
'number of items is beyond absurd.
With List
For I = 1 To .Count
If .Item(I)(1) > OrderBy Then Exit For
Next
If I <= .Count Then
.Add Array(Value, CLng(OrderBy)), , I
Else
.Add Array(Value, CLng(OrderBy))
End If
End With
End Sub
Public Sub Populate(ByVal Combo As ComboBox)
Dim Item As Variant
For Each Item In List
Combo.AddItem Item(0)
Next
Set List = Nothing 'Clean out List for possible reuse.
End Sub
Private Sub Class_Initialize()
Set VConvert = New VConvert
VConvert.LCID = LOCALE_GERMANY
End Sub
Option Explicit
Private Sub Form_Load()
Dim F As Integer
Dim Text As String
Dim Parts() As String
With New ComboBuilder
F = FreeFile(0)
Open "some.txt" For Input As #F
Do Until EOF(F)
Line Input #F, Text
Parts = Split(Text, "|") 'Pipe-delimited fields.
.AddItem Parts(1) 'Use 2nd field.
Loop
Close #F
.Populate Combo1
End With
End Sub
A|0100-(26.867)-AAA
B|0101-(6.155)-BBB
C|0102-(40)-CCC
D|0103-(5.048)-DDD
E|0143-(12.949)-AAA
F|0146-(9.677)-DDD
G|0149-(12.039)-FFF
K&R2 said:... explicitly coerce the pointer into the desired type with a cast
K&R2 said:A unary expression preceded by the parenthesized name of a type causes conversion of the
value of the expression to the named type. This construction is called a cast.