Another idea to check an IP Address format
The Code:
‘will make the text black if its a valid IP address
‘or red if its not
Private Sub Text1_Change()
Text1.ForeColor = 255
If IsIP(Text1.Text) Then Text1.ForeColor = 0
End Sub
‘place this part in a module
Public Function IsIP(TestAddress As String) As Boolean
Dim IPt As String
Dim TQ As Long
Dim TT As Long
Dim TW As Long
Dim IPTemp As Long
IsIP = False ‘Set return value as false
On Error GoTo cockup
‘if an error occures the string is not valid
If Left(TestAddress, 1) = “.” Then Exit Function
If Right(TestAddress, 1) = “.” Then Exit Function
‘check first and last are not “.”
For TQ = 1 To Len(TestAddress) ‘test all chars
IPt = Mid(TestAddress, TQ, 1)
If IPt “.” Then ‘if its not a “.” it must be 0-9
If Asc(IPt) > 57 Or Asc(IPt) < 48 Then Exit Function
End If
Next TQ
TQ = InStr(1, TestAddress, “.”, vbTextCompare)
‘find the three dots
TT = InStr(TQ + 1, TestAddress, “.”, vbTextCompare)
TW = InStr(TT + 1, TestAddress, “.”, vbTextCompare)
If InStr(TW + 1, TestAddress, “.”, vbTextCompare) 0 Then Exit Function
‘if there is a fourth then the string is invalid
‘test each number is between 0 and 255
IPTemp = Val(Left(TestAddress, TQ - 1))
If IPTemp > 255 Or IPTemp < 0 Then Exit Function
IPTemp = Val(Mid(TestAddress, TQ + 1, TT - TQ - 1))
If IPTemp > 255 Or IPTemp < 0 Then Exit Function
IPTemp = Val(Mid(TestAddress, TT + 1, TW - TT - 1))
If IPTemp > 255 Or IPTemp < 0 Then Exit Function
IPTemp = Val(Right(TestAddress, Len(TestAddress) - TW))
If IPTemp > 255 Or IPTemp < 0 Then Exit Function
IsIP = True ‘it has passed all tests so make it true
cockup:
End Function
Omar Abid Blog

Written by Omar Abid on December 23rd, 2007 with comments disabled.
Read more articles on News.
This is the inverse of the last post!
The code :
Imports System
Imports System.Data
Imports System.Data.SqlClient
Private Sub btnReadXMLData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadXMLData.Click
Dim dsPubs As New DataSet()
‘ Read in XML from file
dsPubs.ReadXml(”Pubs.xml”)
‘ Bind DataSet to Data Grid
grdData.DataMember = “publishers”
grdData.DataSource = dsPubs
End Sub
Hope it helps and good luck, any suggestion or questions post a comment !
Omar Abid Blog

Written by Omar Abid on December 23rd, 2007 with comments disabled.
Read more articles on News.
This is a cool idea to save a DataSet as XML file with VB.net, So far you can convert an SQL DataBase to XML format.
The code :
Private Sub btnWriteXMLData_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnWriteXMLData.Click
Dim dsSales As New DataSet()
Dim cn As New SqlConnection _
(”data source=localhost;initial catalog=pubs;user id=sa”)
Dim daAuthors As New SqlDataAdapter(”select * from sales “, cn)
Dim daPublishers As New SqlDataAdapter(”select * from stores “, cn)
‘ Load data from database
daAuthors.Fill(dsSales, “Sales”)
daPublishers.Fill(dsSales, “Stores”)
‘ Write XML to file
dsSales.WriteXml(”XMLFile.xml”)
End Sub
Hope it helps and good luck, any suggestion or questions post a comment !
Omar Abid Blog

Written by Omar Abid on December 23rd, 2007 with comments disabled.
Read more articles on News.
Hi friends,
This is a method to create a CSV file with VB6.
To make it with vb2005 just upgrade the code with the vb2005 tool
The Code :
Dim nfile As Integer
Dim conx as Integer
Private Sub cmdExport_Click()
nfile = FreeFile
ProgressBar1.Value = 1
Open “filea” For Output As #nfile
For conx = 1 To nbr
StatusBar1.Panels(1).Text = CInt(conx * 100 / nbr) & “% completed”
‘Print to file each field of the structure “Customer”
Print #nfile, conx & “, ” & Customer(conx).Field1 & “, ” & Customer(conx).Field2 & “, ” & Customer(conx).Field3 & “, ” & Customer(conx).Field4 & “, ” & Customer(conx).Field5 & “, ” & Customer(conx).Field6 & “, “
ProgressBar1.Value = conx * 100 / nbr
Next conx
Close #nfile
FileCopy “filea”, App.Path & “\ExportedFile.csv”
Kill “filea”
If err.Number = 0 Then
MsgBox “File exported to working directory!”
Else
MsgBox “Error exporting file: ” & err.Number
End If
End Sub
‘The data structure has to be defined like this one:
Type Customer
Field1 as String
Field2 as String
‘You can add as many fields as you need
…
End Type
Hope it helps and good luck, any suggestion or questions post a comment !
Omar Abid Blog

Written by Omar Abid on December 23rd, 2007 with comments disabled.
Read more articles on News.
This a good and short code to show and hide the Windows Tray.
It compatible with VB6 and also .net
Declaration :
Private Declare Function ShowWindow Lib “user32″ (ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib “user32″ Alias “FindWindowA” (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib “user32″ Alias “FindWindowExA” (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Const Tray_SHOW = 5
Private Const Tray_HIDE = 0
Dim Trayhwnd As Long
Code :
Private Sub Form_Load()
Trayhwnd = FindWindow(”Shell_TrayWnd”, “”)
Trayhwnd = FindWindowEx(Trayhwnd, 0, “TrayNotifyWnd”, vbNullString)
ShowWindow Trayhwnd, Tray_HIDE ‘To Hide
‘To show the System Tray add this code insteed of above
‘ShowWindow Trayhwnd, Tray_SHOW ‘To Show
End Sub
Omar Abid Blog

Written by Omar Abid on December 23rd, 2007 with comments disabled.
Read more articles on News.
Hi there!
This is a small tutorial on using files with VB.net.
As VB.net uses new technologies it will be strange for some new developers!
First we must import the Syste.IO Class
Imports System.IO
Reading Text Files:
the easiest way to open a text file is with the System.IO.File.OpenText() method.
Code:’Dim your StreamReader
Dim TextFileStream As System.IO.TextReader
‘Load the textfile into the stream
TextFileStream = System.IO.File.OpenText(”C:\MyTextFile.txt”)
‘Read to the end of the file into a String variable.
Dim MyFileContents As String = TextFileStream.ReadToEnd
‘Close the Stream object
TextFileStream.Close()
The above code will open a text file on your C drive called MyTextFile.txt. It will load all of the text into a string variable called MyFileContents.
It won’t create a file of that name if it doesn’t already exist (and will throw an exception which you should handle - but see later in the FAQ for how to create a new file).
Creating Text Files:
Code:’Dim your Text Writer
Dim TW As System.IO.TextWriter
‘Create a Text file and load it into the TextWriter
TW = System.IO.File.CreateText(”C:\MyTextFile.txt”)
‘Write a line
TW.WriteLine(”Hello World”)
‘Write another line
TW.WriteLine(”Hello World Again”)
‘Flush the text to the file
TW.Flush()
‘Close the File
TW.Close()
Append to an existing Text file:
Code:’Dim your Stream Writer or Text Writer.
Dim SW As IO.TextWriter
‘Open a file for Appending
SW = IO.File.AppendText(”C:\MyTextFile.txt”)
‘Write a line to the bottom of the text file
SW.WriteLine(”This is another line added to the bottom of the text file.”)
‘Flush to the file
SW.Flush()
‘Close the object
SW.Close()
Here is how to get a random line from a text file with the smallest code I can think of.
Extracting One Line From a File:
Code:Dim RandomNumber As New Random()
Dim Tr As IO.TextReader = System.IO.File.OpenText(”C:\MyTextFile.txt”)
Dim FileLines() As String = Split(Tr.ReadToEnd(), vbCrLf)
Tr.Close
Dim MyRandomLine As String = FileLines(RandomNumber.Next(0, UBound(FileLines)))
MsgBox(MyRandomLine)
If you know what line you want to access then you can make it smaller like this :
Code:
Dim Tr As IO.TextReader = System.IO.File.OpenText(”C:\MyTextFile.txt”)
Dim MyFileLine As String = Split(Tr.ReadToEnd(), vbCrLf)(3)
Tr.Close
MsgBox(MyFileLine)
Note that the above code will return the fourth line from the file. It’s zero based, so you would enter (0) for line 1, (1) for line 2 and so on.
If you need more, I advice you to focus on the MSDN Library!
Omar Abid Blog

Written by Omar Abid on December 23rd, 2007 with comments disabled.
Read more articles on News.
Microsoft has finally started talking publicly about the next release of its Internet Explorer Web browser, and expects to deliver the first beta for IE 8 in the first half of 2008.The IE (Internet Explorer) development team is also pledging that while…
Written by ShaDow on December 23rd, 2007 with comments disabled.
Read more articles on News.
Recently I got a video clip in MPEG-4 (.mp4) format but I was unable to play it with Windows Media Player. Later I only found out MP4 format is not supported by Windows Media Player.But wait, I thought I had already install the K-lite Codec Pack which …
Written by ChampDog on December 23rd, 2007 with comments disabled.
Read more articles on News and Windows and Windows Downloads and Windows Tips and Windows Vista and Windows XP and software.
No older articles
Newer articles »