Sunday, August 24, 2008

Creating Help File in Visual Basic 6.0

We can create Help File in MS Word as well as in HTML. Here I would like to describe how to create a Help File for VB using MS Word.

First of all open a new document in MS Word. You can give a title for this document. Create some necessary topic on which you want to provide help to the client. Put any ID numbers along with the help topics without keeping any space. Make all the topics double underlined and hide the IDs.

Give a page break and write the description of help topic 1 and give another page break and write the help description for topic 2 and so on for all help topics.

Keep the mouse cursor at the beginning of the topic description and from the insert menu select reference and add footnote for all the topics description one by one and the footnote number should be the same as the ID number that we have given along with the topic name.

The custom mark for the footnote should be # symbol.


The MS Word page will look like this.



Put a reference for the first topic also.

Save the document in a Rich Text Format.

You should have Microsoft Help Workshop installed in your machine. If you don’t have it, just download hcw.exe from internet and install the same.

Open start --> Microsoft Help Workshop --> Help Workshop

File --> New --> Help Project --> OK --> Enter a Project Name (for a new help project) --> OK.


Click on Files --> Add --> Browse the location of Rich Text File that we have saved before.

Click on Map --> Enter the Topic Id (we have used in .rtf file) and put Numeric value for the same that we will use in future.




Now click on “Save and Compile” button.

Our Help File has been created where we had saved the Help Project.

We need to copy the Help file from that location and paste into the VB Project location in which Project we want to add this Help File.

In order to make this Help file available in our visual basic project, we can go to Project menu and Project Properties. Then browse the location of help file and put the numeric id of the main topic --> OK.


Run the project and press F1 to invoke the help window.



Generating Crystal Report in Visual Basic 6.0

We can create crystal report in Visual Basic 6.0. Sometimes we generate the report based on parameter entered by the user and sometimes we generate the report without any input of user. If we need to pass parameter, it is better to create stored procedure. If we don’t need to pass parameter, we can directly display the required data from one or more than one table. We want to create a report, which will display a student’s detail based on the roll number entered by the end-user as a parameter. The procedure to create a report is given below-

In Project Explorer Window, Right Click on Designers --> Add --> Data Environment.

You will get the data environment window with a new connection option listed in it. Right click on the connection name -->properties -->select appropriate provider to connect to database, select “Microsoft OLEDB Provider for SQL Server” in order to connect to SQL Server --> Next -->Select Server Name --> Authentication(user id, password if required) --> select the database from where you want to display the data -->OK.

Then again right click on Connection name and select Add Command submenu.

Right Click on the Command Name -->Properties.

Here we can write the SQL Statement directly or we can select a stored procedure from Database Object drop down list.

We will select the Parameters Tab to check whether the data type of the parameter matches with the data type of the field in database or not. Here in my database the Roll Number has been declared as Varchar so the parameter is adVarChar in report.

Grouping and Aggregate Tabs are necessary in case we need to perform any calculation using group by keyword. Finally we press on OK button.

At this stage, the Data Environment window will look like follows-

Now we will add a Data Report from the Project Explorer by right clicking on the Designer folder.

We need to set 2 directives from the properties of Data Report, i.e. Data Source = DataEnvironment1 and DataMember = Command1.

Then right click on the body of the Data Report and select Retrieve Structure.

Finally we will drag the necessary field from the Data Environment window and drop in Data Report window. Arrange the fields to make better look. We can add some additional fields also to make the report attractive.

In order to pass the Roll Number as a parameter, we can create a simple window like below-

And on click of View Report button, we can write the following code-


Private Sub cmdViewReport_Click ()

DataEnvironment1.Command1 txtRoll.Text

DataReport1.Show

End Sub 

The Crystal Report of Visual Basic 6.0 is ready to use.




Monday, August 18, 2008

Visual Basic 6.0 and SQL Server Connectivity

In order to develop easy, user friendly but reliable software, we can use Visual Basic 6.0 and SQL Server. Visual Basic provides us with a nice IDE to create user interface very easily. The only complex thing is database connectivity. Here I would like to show you the connectivity between VB and SQL Server.

First of all, it is better to create a module from the Project Explorer window. And in the General Declaration of that module we need to declare 3 objects i.e. RecordSet, Connection and Command. In order to access these 3 objects we need to add the reference of Microsoft ActiveX Data Object 2.1 or any other version from Project à Reference menu.

And we can create a Procedure in it, in order to reuse the procedure from every form where we need the database connectivity. So this procedure will reduce the size of our code. After writing all necessary objects and procedure the module will look like below-


Public rs As Recordset
Public con As Connection
Public com As Command
_____________________________________
Public Sub prcConnect ()
Set rs = New Recordset
Set con = New Connection
Set com = New Command
With con
. ConnectionString = "data source= shafiq; initial catalog= studentadmin; user id= sa; password=1234;" [for SQL Server Authentication]
. ConnectionString = "data source= shafiq; initial catalog= studentadmin; integrated security=SSPI" [for Windows NT authentication]
.Provider = "SQLOLEDB"
.Open
End With
com.ActiveConnection = con
End Sub


Then open a new form and create the necessary interface to display the required field from the database. And on form load event, write the following codes-


Private Sub Form_ Load ()
prcConnect
rs.Open "select * from student", con, adOpenDynamic, adLockOptimistic
rs.MoveFirst
While Not rs.EOF
Combo1.AddItem rs.Fields ("Roll") & ""
rs.MoveNext
Wend
rs.MoveFirst
prcDisplay
' App.HelpFile = MYHELPPROJECT.hlp
txtRoll.Visible = False
End Sub


Here is the code of prcDisplay () function-


Public Sub prcDisplay ()
Combo1.Text = rs.Fields ("Roll") & ""
txtName.Text = rs.Fields ("Name") & ""
txtAddress.Text = rs.Fields ("Address") & ""
txtBatch.Text = rs.Fields ("Batch") & ""
End Sub  


There are several ways to navigate between the records of the database i.e. we can use buttons or combo box etc. I have used here a combo box, so on choose of a roll number from the combo box, the relevant record of that particular roll number should be displayed. Here is the code on click of combo box event to do so-


Private Sub Combo1_ Click()
rs.MoveFirst
Dim check As Integer
check = Combo1.ListIndex
rs.Move check
prcDisplay
End Sub  

For adding a new record into the database we can write the following code on click of a button-


Private Sub cmdNew_ Click ()
Combo1.Visible = False
TxtRoll.Visible = True
cmdUpdate.Visible = False
cmdSave.Visible = True
For Each Control In frmStudent
If TypeOf Control Is TextBox Then
Control.Text = ""
End If
Next
Rs.MoveLast
rs.AddNew
txtRoll.SetFocus
End Sub  

For saving a new record in database, we can write the following code-


Private Sub cmdSave_ Click ()
rs.Fields ( "Roll") = txtRoll.Text & ""
rs.Fields ( "Name") = txtName.Text & ""
rs.Fields ( "Address") = txtAddress.Text & ""
rs.Fields ( "Batch") = txtBatch.Text & ""
rs.update
MsgBox "Saved Successfully", vbInformation, "Saved"
Combo1.Visible = True
Combo1.AddItem txtRoll.Text & ""
txtRoll.Visible = False
rs.Requery
Combo1.Refresh
cmdUpdate.Visible = True
cmdSave.Visible = False
End Sub 

Updating data in database is depended on data type of the field. For updating the record in database, we can write the following code-


Private Sub cmdUpdate_ Click ()
If txtName.Text = "" Then
rs.Fields ( "Name") = Null
Else
rs.Fields ( "Name") = txtName.Text
End If
If txtAddress.Text = "" Then
rs.Fields ( "Address") = Null
Else
rs.Fields ( "Address") = txtAddress.Text
End If
If txtBatch.Text = "" Then
rs.Fields ( "Batch") = Null
Else
rs.Fields ( "Batch") = txtBatch.Text
End If
rs.update
MsgBox "Saved Successfully", vbInformation, "Saved"
End Sub


OR we can write like follows-


Private Sub cmdUpdate_ Click ()
On Error GoTo err_q
rs.Fields ( "Name") = txtName.Text & ""
rs.Fields ( "Address") = txtAddress.Text & ""
rs.Fields ( "Batch") = txtBatch.Text & ""
rs.update MsgBox "Saved Successfully", vbInformation, "Students Detail"
Exit Sub
err_q :
MsgBox "Could not connect to the server", vbCritical, "Student Admin"
End Sub 

For deleting a record from the database, we can write the following code-


Private Sub cmdDelete_ Click ()
com.CommandText = "delete from student where roll='" & Combo1 & "'"
com.Execute MsgBox "Record has been deleted ", vbInformation, "Delete Record"
Combo1.RemoveItem Combo1.ListIndex
rs.MoveFirst
rs.Requery
Combo1.Refresh
prcDisplay
End Sub