Category Archives: Microsoft

Sort Dates in a Listview, Correctly

This was a killer i saw it in the MSDN libraries and they went to much deep into it but there is an easier way…it works better. In Visual basic 5.0 or 6.0 you cann’ot sort by date in the listview. The problem is that when a value is entered in the cell, the control treats it as… Read More »

Improved String Concatenation Performance

Joining strings together, or concatentation, can take a toll on performance. Especially in a long loop. Here is a method to increase performance by 20x. Sub Concat(Dest As String, Source As String)’First Dest must have one space more’Example : Dest = ‘Hello ”and not Dest = ‘Hello’    Dim string1 As Long   Dim string2 As Long      string1 = Len(Dest)   string2 = Len(Source)… Read More »

How to embed an error code in a standard function return

I have read many accounts in various books and articles about how it would be nice to be able to return an error code with every function return. It’s fairly easy to achieve this. Simply create a User-Defined-Type (or class) called tReturn (or whatever) in a global module (assuming the use of a UDT). ‘———————————————Type tReturn    Value as… Read More »

Floating Point Numbers Validation

Just like the title says, the code in the attached file will validate the floating point numbers in a text box. Usage Create a form with a textbox, and insert the following into the form code: Private Sub Form_Load()    Text1.Text = Empty End Sub Private Sub Text1_KeyPress(KeyAscii As Integer)    ‘ Precision is 2 in this case   ‘ Send the… Read More »

Remove a Record from an Array

Remove any record from an array, without leaving a gap in the records. Option Explicit Private Type my_type   field1 As String   field2 As Long   field3 As IntegerEnd Type Const MAX_ARRAY = 10Dim my_array(0 To MAX_ARRAY – 1) As my_type ‘Delete the record RecPos (from 0 to MaxRecs)…MaxRecs is the maximum array dimensionPublic Sub DeleteRecordFromMyArray(RecPos As Integer, MaxRecs As Integer)   Dim i As… Read More »

Time Between Dates

Calculate the time elapsed between two dates. Option ExplicitPublic Function ElapsedTime(tStart, tStop) As String’ *******************************************************************’ Function Name : ElapsedTime *’ Created By : Herry Hariry Amin *’ Email : h2arr@cbn.net.id *’ Language : VB4, VB5, VB6 *’ Example : sYourVariable = ElapsedTime(tStartTime,tStopTime) *’ *******************************************************************    Dim dtr, dtl, jml As Long    dtl = (Hour(tStart) * 3600) + (Minute(tStart) *… Read More »

Find if a value exists in an Array

This handy little function will determine if a value is present in an array, and return true or false. Option Explicit Public Function IsInArray(FindValue As Variant, arrSearch As Variant) As Boolean   On Error GoTo LocalError   If Not IsArray(arrSearch) Then Exit Function   If Not IsNumeric(FindValue) Then FindValue = UCase(FindValue)   IsInArray = InStr(1, vbNullChar & Join(arrSearch, vbNullChar) & vbNullChar, _vbNullChar & FindValue & vbNullChar)… Read More »

Get the Full Path of a File

This is a very simple way to get just the full directory path of a file passed in. Public Function GetTheFilePath(ByVal strFile) As String   Files = Split(strFile, “\”)   For I = 0 to (UBound(Files) – 1)      FilePath = FilePath & Files(I) & “\”   Next   GetTheFilePath = FilePathEnd Function Usage MyFilePath = GetTheFilePath(“C:\testing\jim\file.txt”) will return: C:\testing\jim\

Multiple Files with CommonDialog

Ever wonder how to return multiple files using the CommonDialog control? Here is a short demonstration of how to get it done. Private Sub cmdOpen_Click()   Dim sFileNames() As String   Dim iCount As Integer    cd.Filter = “All Files|*.*”   cd.Flags = cdlOFNAllowMultiselect   cd.ShowOpen    If cd.FileName <> “” Then      sFileNames = Split(cd.FileName, Chr(32))      For iCount = LBound(sFileNames) To UBound(sFileNames)         MsgBox sFileNames(iCount), vbInformation      Next    End IfEnd Sub

Convert a Decimal Number in a Given Base

Just a little something to help you do math in different numbering systems. Option Explicit ‘Convert a decimal number in another basePublic Function CBase(ByVal number As Long, ByVal base As Long) As String   ‘symbols to code the number   Const chars = “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”   Dim r As Long    ‘verify if there are enought symbols to code the number in the selected base   If base… Read More »