Sabtu, 04 Agustus 2007

visual basic programming

Visual Basic Programming is a programming languge based on graph, windows, and belongs to 5th generation language, object oriented programming. This programming language is oriented to business(business applications). The very supportive to this language programming are Microsoft access, sql server ver 7.x, sql server 2000, or higher. This programming language is created by Microsoft not only for people with computer knowledge background, programmers, but also for computer hobbyists. So, anyone is made possible to learn Visual Basic Programming. All you need is a habit of working systematically in completing each work, organizing problem solving steps in sequence or what is called algorithm.

As mentioned above, not only people who have computer background can create a programmed application but a computer hobbyist can also become a programmer. Why? Because the Vendor of Visual Basic Programming provides a Wizard that helps users to create a programmed application. All you need to know is the steps of how to operate the Wizard to develop an application.

The application discussed here is called Author, where the database file being used is called Biblio.

Let’s start to create a new application by using Visual Basic Programming. Let’s name this project as Biblio.vbp. The database file used is called Biblio.mvb which is placed in the C:\Program Files\Microsoft Visual Studio. The purpose of creating an Author application is that users can enter author data and save them into Biblio database.

The task is started when a new project is opened, then the program can be created by using Wizard. The name of the program to be created is Author and the name of the form is called FormAuthors.

There are two stages of the task of developing an Authors program. They are
Creating program by using Wizard
Running program

1. Create Program By Using Wizard



Picture 1: Create a New Form




Picture 2: Choice The Form VB Data Form Wizard

Choose one type of the form. In this session it is assumed that you have selected the form VB Data Form Wizard by double clicking it.



Picture 3: Data FormWizard – Introduction

The Data Form Wizard will help you create a form with objects bound to a local or remote data source.

Wizard will display FormAutor.rwp. Now you can click the dot icon if you want to use the others. After that click Next.



Picture 4: Data FormWizard – Database Type

Select a database formats from the list that are Microsoft Access or Remote Access. Click Next.



Picture 5: Data Form Wizard – Database

Click the browse button to select a database file. Then click the Browse button again to find a database. After that select a BIBLIO.MDB that is a default database. You can find it on the C:\Program Files\Microsoft Visual Studio folder. Click Next.


Picture 6: Data Form Wizard – Form

Select the desired form type and a data binding type to use to access the data. Fill in “FormAuthors” on the question What name do you want for the form ?. Select a Master/Detail Form layout. Select a ADO Code binding type. Click Next


Picture 7: Data Form Wizard – Master Record Source

Select the record source and fields for the Master section of the Master/Detail form. The Master section displays one record at a time. Assumed that you have chosen an Author record source. Click the right double arrow to move all fields to the right. Click next.

Picture 8: Data Form Wizard – Detail Record Source

Select a record source for the Detail Section of the Master/Detail Form, and then select the fields to use. Click Next



Picture 9: Data Form Wizard – Record Source Relation

Select a field from each record source that links the two sources together. Click field Au_ID on the Master and Detail. Click next.


Picture 10: Data Form Wizard – Control Selection

Select the desired controls to place on the form, Click Next

Picture 11: Data Form Wizard – Finised

Select the FormAuthor.rwp in the question To what profile do you want to save your settings ? click Finish..


Picture 12: Form Authors





2. Running Biblio.vbp program

In this step, you have been successful to create a new form with the help of the Wizard.
When the Authors Visual basic programming is running as appears in the picture 12. How to operate it ?. By using the form you can input Authors data into the BIBLIO.MDB database file. There are two parts of the form. One, a part of the Authors Form that is used to input the Authors data and the other to view data.



Picture 13: Running Form Authors

Authors Visual Basic Programming Code.

You can see the code of the following FormAuthors program. The Wizard has generated the code by himself, without your being aware of it.
.

You can also write the following code without the help of Wizard, and the result will be the same.



Dim WithEvents adoPrimaryRS As Recordset
Dim mbChangedByCode As Boolean
Dim mvBookMark As Variant
Dim mbEditFlag As Boolean
Dim mbAddNewFlag As Boolean
Dim mbDataChanged As Boolean

Private Sub Form_Load()
Dim db As Connection
Set db = New Connection
db.CursorLocation = adUseClient
db.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB;"

Set adoPrimaryRS = New Recordset
adoPrimaryRS.Open "select Au_ID,Author,[Year Born] from Authors", db, adOpenStatic, adLockOptimistic

Dim oText As TextBox
'Bind the text boxes to the data provider
For Each oText In Me.txtFields
Set oText.DataSource = adoPrimaryRS
Next

mbDataChanged = False
End Sub

Private Sub Form_Resize()
On Error Resume Next
lblStatus.Width = Me.Width - 1500
cmdNext.Left = lblStatus.Width + 700
cmdLast.Left = cmdNext.Left + 340
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If mbEditFlag Or mbAddNewFlag Then Exit Sub

Select Case KeyCode
Case vbKeyEscape
cmdClose_Click
Case vbKeyEnd
cmdLast_Click
Case vbKeyHome
cmdFirst_Click
Case vbKeyUp, vbKeyPageUp
If Shift = vbCtrlMask Then
cmdFirst_Click
Else
cmdPrevious_Click
End If
Case vbKeyDown, vbKeyPageDown
If Shift = vbCtrlMask Then
cmdLast_Click
Else
cmdNext_Click
End If
End Select
End Sub

Private Sub Form_Unload(Cancel As Integer)
Screen.MousePointer = vbDefault
End Sub

Private Sub adoPrimaryRS_MoveComplete(ByVal adReason As ADODB.EventReasonEnum, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
'This will display the current record position for this recordset
lblStatus.Caption = "Record: " & CStr(adoPrimaryRS.AbsolutePosition)
End Sub

Private Sub adoPrimaryRS_WillChangeRecord(ByVal adReason As ADODB.EventReasonEnum, ByVal cRecords As Long, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
'This is where you put validation code
'This event gets called when the following actions occur
Dim bCancel As Boolean

Select Case adReason
Case adRsnAddNew
Case adRsnClose
Case adRsnDelete
Case adRsnFirstChange
Case adRsnMove
Case adRsnRequery
Case adRsnResynch
Case adRsnUndoAddNew
Case adRsnUndoDelete
Case adRsnUndoUpdate
Case adRsnUpdate
End Select

If bCancel Then adStatus = adStatusCancel
End Sub

Private Sub cmdAdd_Click()
On Error GoTo AddErr
With adoPrimaryRS
If Not (.BOF And .EOF) Then
mvBookMark = .Bookmark
End If
.AddNew
lblStatus.Caption = "Add record"
mbAddNewFlag = True
SetButtons False
End With

Exit Sub
AddErr:
MsgBox Err.Description
End Sub

Private Sub cmdDelete_Click()
On Error GoTo DeleteErr
With adoPrimaryRS
.Delete
.MoveNext
If .EOF Then .MoveLast
End With
Exit Sub
DeleteErr:
MsgBox Err.Description
End Sub

Private Sub cmdRefresh_Click()
'This is only needed for multi user apps
On Error GoTo RefreshErr
adoPrimaryRS.Requery

Exit Sub
RefreshErr:
MsgBox Err.Description
End Sub

Private Sub cmdEdit_Click()
On Error GoTo EditErr

lblStatus.Caption = "Edit record"
mbEditFlag = True
SetButtons False
Exit Sub

EditErr:
MsgBox Err.Description
End Sub
Private Sub cmdCancel_Click()
On Error Resume Next

SetButtons True
mbEditFlag = False
mbAddNewFlag = False
adoPrimaryRS.CancelUpdate
If mvBookMark > 0 Then
adoPrimaryRS.Bookmark = mvBookMark
Else
adoPrimaryRS.MoveFirst
End If
mbDataChanged = False

End Sub

Private Sub cmdUpdate_Click()
On Error GoTo UpdateErr

adoPrimaryRS.UpdateBatch adAffectAll

If mbAddNewFlag Then
adoPrimaryRS.MoveLast 'move to the new record
End If

mbEditFlag = False
mbAddNewFlag = False
SetButtons True
mbDataChanged = False

Exit Sub
UpdateErr:
MsgBox Err.Description
End Sub

Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub cmdFirst_Click()
On Error GoTo GoFirstError

adoPrimaryRS.MoveFirst
mbDataChanged = False

Exit Sub

GoFirstError:
MsgBox Err.Description
End Sub

Private Sub cmdLast_Click()
On Error GoTo GoLastError

adoPrimaryRS.MoveLast
mbDataChanged = False

Exit Sub

GoLastError:
MsgBox Err.Description
End Sub

Private Sub cmdNext_Click()
On Error GoTo GoNextError

If Not adoPrimaryRS.EOF Then adoPrimaryRS.MoveNext
If adoPrimaryRS.EOF And adoPrimaryRS.RecordCount > 0 Then
Beep
'moved off the end so go back
adoPrimaryRS.MoveLast
End If
'show the current record
mbDataChanged = False

Exit Sub
GoNextError:
MsgBox Err.Description
End Sub

Private Sub cmdPrevious_Click()
On Error GoTo GoPrevError

If Not adoPrimaryRS.BOF Then adoPrimaryRS.MovePrevious
If adoPrimaryRS.BOF And adoPrimaryRS.RecordCount > 0 Then
Beep
'moved off the end so go back
adoPrimaryRS.MoveFirst
End If
'show the current record
mbDataChanged = False

Exit Sub

GoPrevError:
MsgBox Err.Description
End Sub

Private Sub SetButtons(bVal As Boolean)
cmdAdd.Visible = bVal
cmdEdit.Visible = bVal
cmdUpdate.Visible = Not bVal
cmdCancel.Visible = Not bVal
cmdDelete.Visible = bVal
cmdClose.Visible = bVal
cmdRefresh.Visible = bVal
cmdNext.Enabled = bVal
cmdFirst.Enabled = bVal
cmdLast.Enabled = bVal
cmdPrevious.Enabled = bVal
End Sub


oleh m a loebis
07:19:00

Hapus



1 – 1 dari 1