Mar 30

How to read image or picture from database that stored in binary format

‘How to Read Image/picture from Database file that stored in Binary format.

 

 

 

 

 

 

Visual Basic Source Code

Private Sub ShowPic()
DataFile = 1
Open “pictemp” For Binary Access Write As DataFile
Fl = Rec1!logo.ActualSize ‘ Length of data in file
If Fl = 0 Then Close DataFile: Exit Sub
Chunks = Fl \ ChunkSize
Fragment = Fl Mod ChunkSize
ReDim Chunk(Fragment)
Chunk() = Rec1!logo.GetChunk(Fragment)
Put DataFile, , Chunk()
For I = 1 To Chunks
   ReDim Buffer(ChunkSize)
   Chunk() = Rec1!logo.GetChunk(ChunkSize)
   Put DataFile, , Chunk()
Next I
Close DataFile
FileName = “pictemp”
Picture1.Picture = LoadPicture(FileName)
End Sub
Mar 25

How to open the CSV File and Sort in Visual Basic

How to open a Excel CSV file format and Sort the result in listview control.

 

 

 

 

 

 

 

 

'Visual Basic Source Code
Private Sub btnLoad_Click()
Dim Conn As ADODB.Connection
Dim Rec1 As ADODB.Recordset
Dim SQL_Str As String
Dim field_num As Integer
Set Conn = New ADODB.Connection
Conn.ConnectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"DefaultDir=" & txtDir.Text
Conn.Open
LvResults.Visible = False
DoEvents
SQL_Str = "Select * FROM " & txtFile.Text & " ORDER BY " & txtField.Text
field_num = CInt(txtField.Text) - 1
Set Rec1 = Conn.Execute(SQL_Str)
LvResults.Clear
While Not Rec1.EOF
If IsNull(Rec1.Fields(field_num).Value) Then
LvResults.AddItem "<null>"
Else
LvResults.AddItem Rec1.Fields(field_num).Value
End If
Rec1.MoveNext
Wend
LvResults.Visible = True
End Sub
Mar 24

How to use Find and Next in Visual Basic

A visual basic 6 sample programming code on how to use the Find and Next String.

 

 

 

 

 

 

 

Visual Basic Code Sample

Public Function FindNext(ByVal Text As String, _
ByVal Search As String, _
Optional ByVal Position As Long, _
Optional CaseSensitive As Boolean, _
Optional ByVal Up As Boolean) _
As Long

Dim lPos As Long Dim lFind As Long
If Text <> "" Then
If Position < 1 Then
Position = 1
If Position > Len(Text) Then
Position = Len(Text) If Up Then
lPos = Position - 1
While lPos > 0
lFind = InStr(lPos, Text, Search, Abs(Not CaseSensitive))
If lFind = lPos Then
lPos = 0
Else
lFind = 0
lPos = lPos - 1
End If
Wend
FindNext = lFind
Else
FindNext = InStr(Position + 1, Text, Search, _ Abs(Not CaseSensitive))
End If
End If
End Function
Mar 23

Convert Hexadecimal to ASCII

Visual Basic 6 function that will convert the Hexadecimal value into ASCII value. Please copy the code below to module area and call the function name hexToascii like sample below.

 

 

 

 

 

 

Visual Basic Code

Command1_Click()
Text1.Text = hexToascii(Text2.Text)
End Sub

For x = 1 To Len(hextext)
num = Mid(hextext, x, 2)
Value = Value & Chr(Val("&h" & num))
x = x + 1 Next x
hexToascii = Value
End Function
Mar 22

Visual Basic Function to Convert Numeric to String Format

This visual basic programming function that convert the numeric value into string format. This code can be use in Accounting System or Payroll System in printing checks the amount in word format.

Please copy this function in module area and make a code in form, please check the code below to call this function.

In Form1 please add control Text1.Text, Text2.Text and 1 command button, double the command1 button.

Code like this


 Command1_Click
 'Put the result in Text1.Text
 Text1.Text = NumToString(Text2.Text)  'Note : You may also validate Text2 into numbers only.
 End Sub
 
'Function to Convert the Numeric Value into string format.
 
Public Function NumToString(ByVal nNumber As Currency) As String
Dim bNegative As Boolean
 Dim bHundred As Boolean
If nNumber < 0 Then
 bNegative = True
 End If
nNumber = Abs(Int(nNumber))
If nNumber < 1000 Then     If nNumber \ 100 > 0 Then
 NumToString = NumToString & _
 NumToString(nNumber \ 100) & " hundred"
 bHundred = True
 End If
 nNumber = nNumber - ((nNumber \ 100) * 100)
 Dim bNoFirstDigit As Boolean
 bNoFirstDigit = False
 Select Case nNumber \ 10
 Case 0
 Select Case nNumber Mod 10
 Case 0
 If Not bHundred Then
 NumToString = NumToString & " zero"
 End If
 Case 1: NumToString = NumToString & " one"
 Case 2: NumToString = NumToString & " two"
 Case 3: NumToString = NumToString & " three"
 Case 4: NumToString = NumToString & " four"
 Case 5: NumToString = NumToString & " five"
 Case 6: NumToString = NumToString & " six"
 Case 7: NumToString = NumToString & " seven"
 Case 8: NumToString = NumToString & " eight"
 Case 9: NumToString = NumToString & " nine"
 End Select
 bNoFirstDigit = True
 Case 1
 Select Case nNumber Mod 10
 Case 0: NumToString = NumToString & " ten"
 Case 1: NumToString = NumToString & " eleven"
 Case 2: NumToString = NumToString & " twelve"
 Case 3: NumToString = NumToString & " thirteen"
 Case 4: NumToString = NumToString & " fourteen"
 Case 5: NumToString = NumToString & " fifteen"
 Case 6: NumToString = NumToString & " sixteen"
 Case 7: NumToString = NumToString & " seventeen"
 Case 8: NumToString = NumToString & " eighteen"
 Case 9: NumToString = NumToString & " nineteen"
 End Select
 bNoFirstDigit = True
 Case 2: NumToString = NumToString & " twenty"
 Case 3: NumToString = NumToString & " thirty"
 Case 4: NumToString = NumToString & " forty"
 Case 5: NumToString = NumToString & " fifty"
 Case 6: NumToString = NumToString & " sixty"
 Case 7: NumToString = NumToString & " seventy"
 Case 8: NumToString = NumToString & " eighty"
 Case 9: NumToString = NumToString & " ninety"
 End Select
 If Not bNoFirstDigit Then
 If nNumber Mod 10 <> 0 Then
 NumToString = NumToString & "-" & _
 Mid(NumToString(nNumber Mod 10), 2)
 End If
 End If
 Else
 Dim nTemp As Currency
 nTemp = 10 ^ 12 'trillion
 Do While nTemp >= 1
 If nNumber >= nTemp Then
 NumToString = NumToString & _
 NumToString(Int(nNumber / nTemp))
 Select Case Int(Log(nTemp) / Log(10) + 0.5)
 Case 12: NumToString = NumToString & " trillion"
 Case 9: NumToString = NumToString & " billion"
 Case 6: NumToString = NumToString & " million"
 Case 3: NumToString = NumToString & " thousand"
 End Select
nNumber = nNumber - (Int(nNumber / nTemp) * nTemp)
 End If
 nTemp = nTemp / 1000
 Loop
 End If
 
If bNegative Then
 NumToString = " negative" & NumToString
 End If

End Function

Public Function DollarToString(ByVal nAmount As Currency) As _
 String

Dim nDollar As Currency
 Dim nCent As Currency

nDollar = Int(nAmount)
 nCent = (Abs(nAmount) * 100) Mod 100

DollarToString = NumToString(nDollar) & " dollar"

If Abs(nDollar) <> 1 Then
 DollarToString = DollarToString & "s"
 End If

DollarToString = DollarToString & " and" & _
 NumToString(nCent) & " cent"

If Abs(nCent) <> 1 Then
 DollarToString = DollarToString & "s"
 End If
 

End Function