Just wanted to show this one..
Well, lets say you want to read a dat file with your own tags.
You have a file on you root lets say "C:\IniFile.Dat" with two line A and B
A=Hello
B=VB-World
Code:
Public Declare Function MessageBox Lib "user32" Alias "MessageBoxA" _
(ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, _
ByVal wType As Long) As Long
Public Const MB_ICONINFORMATION = &H40&
Public Const MB_OK = &H0&
Public Const FILENAME = "C:\IniFile.Dat"
Public Function ReadFileString(TagString As String) As String
On Error GoTo Err_ReadFile
Dim sString As String
Dim Infile As Integer
Infile = FreeFile
' Open the file for reading
Open (FILENAME) For Input As Infile
' Check if there are any data aviable the file
If EOF(Infile) = True Then GoTo Err_ReadFile
' Read all the lines in the file
While Not EOF(Infile)
Line Input #Infile, sString
' If the selected tag string is be founden, place it as a return value
If Left(UCase(sString), Len(TagString)) = UCase(TagString) Then
ReadFileString = Right(sString, Len(sString) - (Len(TagString) + 1))
' Close the file
Close Infile
Exit Function
End If
Wend
' Close the file if the tag string coulnt found.
Close Infile
Exit Function
Err_ReadFile:
Call MessageBox(0, "Error reading file!", _
App.Title, MB_OK Or MB_ICONINFORMATION)
' Close the file.
Close Infile
End Function
Place this code in a module. now you can use the function to find you lines (strings).
Code:
Dim MyString As String
MyString = ReadFileString("A") & Space(1) & _
ReadFileString("B")
Do you like it.. ;)
I like this. Here is two tips from me.
I know alot of people strip filename by using loops. Use the third method will only require one line of code and no loops.
Code:
Sub Main()
Dim str_Data As String
str_Data = "C:\Visual Basic\Test.txt"
'Method1
' Dim int_Last As Integer
' Dim int_Pos As Integer
' Do
' int_Last = int_Pos
' int_Pos = InStr(int_Pos + 1, str_Data, "\", vbTextCompare)
' If int_Pos = 0 Then Exit Do
' Loop
'
' MsgBox Mid(str_Data, int_Last + 1)
'Method2
' Dim int_X As Integer
' For int_X = Len(str_Data) To 1 Step -1
' If Mid(str_Data, int_X, 1) = "\" Then
' MsgBox Mid(str_Data, int_X + 1)
' Exit For
' End If
' Next
'Method3
MsgBox Mid(str_Data, InStrRev(str_Data, "\") + 1)
End Sub
This tip replace five lines of code with one. Use Method 2 when dealing with booleans.
Code:
Private Sub Form_Click()
' 'Method1
' If Command1.Visible = True Then
' Command1.Visible = False
' Else
' Command1.Visible = True
' End If
'Method2
Command1.Visible = Not Command1.Visible
End Sub