This action might not be possible to undo. Are you sure you want to continue?
Activity 1 : Building User Interface
Intrinsic Controls • • • • Text Box Command Buttons Controls for making choices Special purpose control
Example: Poll system
The Code Dim total, Excel_total, VG_total, G_total, Sat_total, Bad_total As Integer Dim Excel_percent, VG_percent, G_percent, Sat_percent, Bad_percent As Single Dim done As Boolean Private Sub cmd_Vote_Click() Picture1.Cls If Option_Excel.Value = True Then Excel_total = Excel_total + 1 Lbl_ExcelTotal = Excel_total ElseIf Option_VG.Value = True Then VG_total = VG_total + 1 Lbl_VGTotal = VG_total ElseIf Option_G.Value = True Then G_total = G_total + 1 Lbl_GTotal = G_total ElseIf Option_Sat.Value = True Then Sat_total = Sat_total + 1 Lbl_SatTotal = Sat_total ElseIf Option_Bad.Value = True Then Bad_total = Bad_total + 1 Lbl_BadTotal = Bad_total End If total = Excel_total + VG_total + G_total + Sat_total + Bad_total Lbl_Total = total
Excel_percent = Excel_total / total VG_percent = VG_total / total G_percent = G_total / total Sat_percent = Sat_total / total Bad_percent = Bad_total / total Lbl_Excel.Caption = Format(Excel_percent, "Percent") Lbl_VG.Caption = Format(VG_percent, "Percent") Lbl_G.Caption = Format(G_percent, "Percent") Lbl_Sat.Caption = Format(Sat_percent, "Percent") Lbl_Bad.Caption = Format(Bad_percent, "Percent") Picture1.Line (100, 750)-(3800 * Excel_percent, 950), vbRed, BF Picture1.Line (100, 1450)-(3800 * VG_percent, 1650), vbMagenta, BF Picture1.Line (100, 2150)-(3800 * G_percent, 2350), vbGreen, BF Picture1.Line (100, 2850)-(3800 * Sat_percent, 3050), vbBlue, BF Picture1.Line (100, 3550)-(3800 * Bad_percent, 3750), vbYellow, BF End Sub Example : Pythagoras Theorem
The Code Private Sub Command1_Click() Dim AB, AC, BC As Single AB = Val(Txt_AB.Text) AC = Val(Txt_AC.Text) BC = Val(Txt_BC.Text) If AB <> 0 And AC <> 0 Then BC = Sqr(AB ^ 2 + AC ^ 2) Txt_BC.Text = Round(BC, 2) ElseIf AB <> 0 And BC <> 0 Then AC = Sqr(BC ^ 2 - AB ^ 2) Txt_AC.Text = Round(AC, 2) ElseIf AC <> 0 And BC <> 0 Then AB = Sqr(BC ^ 2 - AC ^ 2) Txt_AB.Text = Round(AB, 2) End If End Sub Example : Digital Clock In this program, you have to insert a timer control into the form . Then go to the properties window to set the timer's interval value to 1000 so that Visual Basic updates the time every 1000 milliseconds, or once a second. Other properties that you ought to set are to change the caption (here is My Clock) and to set Form1's MaxButton to false (so that it cannot be maximized by the user) The Design Interface
The Run Time Interface
Now, double click the timer and enter the one line code as follows: Private Sub Timer1_Timer() Label1.Caption = Time End Sub
Exercise 1 Develop a simple calculator using Intrinsic controls. Develop an application using Timer control and Image control. Image should move around the form and when it hits the slide of the form its direction should be changed
Exercise 2 Design Course Details form with the following controls Combo box for course name Option button to display batch no Check box to display timings List Box for centre names
Exercise 3 Change the size of label by using HScroll Bar Change the speed of label by using HScrollBar
Popup Menus Context Menus EXAMPLE 1: The code .Activity 2 : Working with Menu and Dialog Boxes Menu Editor Drop down menus Nested Menus Access key n short cut keys to menus Writing code for menu item Adding and deleting menu commands at Run Time.
"Menu Demo" End Sub Private Sub mnuFileSave_Click() MsgBox "Code for 'Save' goes here. "Menu Demo" End Sub Private Sub mnuFileSaveAs_Click() MsgBox "Code for 'Save As' goes here.". vbInformation.". "Menu Demo" End Sub Private Sub mnuFilePrint_Click() MsgBox "Code for 'Print' goes here. vbInformation.Private Sub mnuFileOpen_Click() MsgBox "Code for 'Open' goes here.".com". "Menu Demo" End Sub Private Sub mnuFileExit_Click() Unload Me End Sub Private Sub mnuHelpAbout_Click() MsgBox "Menu Demo" & vbCrLf _& "Copyright " & Chr$(169) & " 2004 thevbprogrammer. Format and Exit For exit option design confirmation Message Box Pop up menu for cut. vbInformation.". vbInformation. copy and paste . . _ "About" End Sub Exercise 1 Create Notepad with following menu options Edit.
... I need to divide this number by all the numbers from 3.up to N-1. I use the Select Case .End Select statement to determine whether a number entered by a user is a prime number or not. if the number is 2.4..Text) Select Case N Case Is < 2 . it is not a prime number. it is a prime number... In Case 2.. otherwise it is a prime number... D As Single Dim tag As String N = Val(TxtNumber... Here I also used a tag="Not Prime' to identify the number that is not prime.. if the number N is more than 2. if it can be divided by any of these numbers. so that when the routine exits the loop.... In the last case. all numbers that are less than 2 are prime. For case 1....6. the label will display the correct answer. I use a Do.Loop While statement to control the program flow.5. Private Sub Command1_Click() Dim N.Activity 3 : Programming Constructs Variables and Constants Data Types: Built-in and UDT Arrays Operators Control Flow statements Unconditional Branch With statement Example : Prime Number tester In this program.
1 If tag <> "Not Prime" Then Lbl_Answer. then it has one root.Caption = "It is not a prime number" tag = "Not Prime" Exit Do End If D=D+1 Loop While D <= N . then it has two roots and if b2 -4ac=0.Lbl_Answer. To obtain the roots.Caption = "It is a prime number" Case Is > 2 D=2 Do If N / D = Int(N / D) Then Lbl_Answer.Caption = "It is not a prime number" Case Is = 2 Lbl_Answer. If b2 -4ac>0. the program uses the standard quadratic formula : . It uses the determinant b2 -4ac to solve the problems. else it has no root.Caption = "It is a prime number" End If End Select End Sub Example: Quadratic Equation Solver Quadratic equation is a fairly straight forward high school mathematics problem. The quadratic equation solver was programmed to determine the number of roots the equation has as well as to compute the roots.
Caption = "" .The Code Private Sub Exit_Click() End End Sub Private Sub Form_Load() Dim a. det As Integer Dim root1.Text = "" Coeff_c. c. root2 As Single Dim numroot As Integer End Sub Private Sub new_Click() ' To set all values to zero Coeff_a.Text = "" Answers.Text = "" Coeff_b. b.
Text = Round(root2.Visible = False Lbl_numroot.Text) b = Val(Coeff_b.Caption = 0 Answers.txt_root1.Text = "" txt_root2.Visible = True txt_root1.Text) 'To compute the value of the determinant det = (b ^ 2) .Caption = "There is no root " End If End Sub .(4 * a * c) If det > 0 Then Lbl_numroot. 4) ElseIf det = 0 Then root1 = (-b) / 2 * a Lbl_numroot.Sqr(det)) / (2 * a) Answers.Caption = "The root is " txt_root1.Caption = "The roots are " Lbl_and.Text = "" Lbl_and.Text) c = Val(Coeff_c.Caption = 2 root1 = (-b + Sqr(det)) / (2 * a) root2 = (-b .Visible = True txt_root1.Visible = True txt_root1. 4) txt_root2.Caption = "" End Sub Private Sub Solve_Click() a = Val(Coeff_a.Visible = False txt_root2.Text = root1 Else Lbl_numroot.Visible = True txt_root2.Visible = False txt_root1.Caption = 1 Answers.Text = Round(root1.
cancel and do the following: Set the password Write a loop that continues until the password is right Display proper message if password is incorrect Clear the password text box and redisplay login form Password should be in “*” form Count the number of times the user types wrong password.Exercise 1: Design a Login Form allowing to enter User Name and password and command buttons ok . When the user types right password. display a message box stating the number of attempts made to get to the right password. Exercise 2: Design a user Interface which accepts Principal. . term and Monthly Payment. Form should be invoked when user presses ok from the log in screen of exercise 1. annual Interest. There should be calculate button on this form which should calculate the monthly payment. Exercise 3: Write code to find Factorial of number entered by the user.
as mentioned above: Sub TestSub() MsgBox "Code in TestSub()" End Sub . Any code inserted between these lines will be executed when the subroutine is called. These lines represent the start and end of the subroutine. A subroutine can be called in one of two ways: using the Call keyword. the second line will automatically be added for you. or by simply stating its name.Activity 4: Working with Forms and Procedures Form and its properties Multiple Forms Events MDI Procedures Functions Reusing procedures and Functions Example : simple subroutine Sub TestSub() End Sub After you enter the first line and press Enter. Sub TestSub() MsgBox "Code in TestSub()" End Sub Private Sub Form_Load() MsgBox "Code in Form_Load()" TestSub MsgBox "Back in Form_Load()" End Sub You can also use the Call keyword.
ByVal y As Integer) As Integer Add = x + y End Function Private Sub Form_Load() Dim a As Integer Dim b As Integer Dim c As Integer a = 32 b = 64 c = Add(a.Private Sub Form_Load() MsgBox "Code in Form_Load()" 'This line is functionally equal as the line in the previous example Call TestSub MsgBox "Back in Form_Load()" End Sub Example: Function Private Function Add(ByVal x As Integer. b) MsgBox c End Sub .
Exercise 3 Create a standard exe form and whenever the user releases the right mouse button a dialog box should be displayed.Exercise 1 Create an application which should accept customer information from the customer like customer name. If he clicks ‘Yes’ then the application should end otherwise not. address. Also write the code for all the menus. he will be prompted with a message box whether he really wants to quit the application. When the user closes the form. Exercise 2 Create Multiple forms in project and call one from another. edit. Exercise 5 Create MDI form with menus open. Use GotFocus or LostFocus events to validate this. phone and email. The customer name and address cannot be left blank. When another form is called the form that has lost focus should be hidden. format and exit. Exercise 4 Create a standard module for accepting integer data only. .
Activity 5: Control Array and Collection Control array Collections Form Collection Control Collection Example: Mimic a Cell Phone Using Control Arrays Private Sub ResetAll() NumTimesClicked = 0 LastButtonClicked = -1 HasBeenDisplayed = False End Sub Private Sub Form_Load() Buttons(2).Caption = "3" + vbCrLf + "DEF" Buttons(4).Caption = "4" + vbCrLf + "GHI" .Caption = "2" + vbCrLf + "ABC" Buttons(3).
Caption = "8" + vbCrLf + "TUV" Buttons(9).Caption = "9" + vbCrLf + "WXYZ" End Sub Private Sub Buttons_Click(Index As Integer) ' If a button without characters is clicked then exit out If Index < 2 Or Index > 9 Then ResetAll Exit Sub End If If LastButtonClicked = Index Then ' We have been clicked before so keep track of it If Index = 7 Or Index = 9 Then ' 7 & 9 have 4 characters so MOD 4 NumTimesClicked = (NumTimesClicked Mod 4) + 1 Else ' The rest of 3 characters so MOD 3 NumTimesClicked = (NumTimesClicked Mod 3) + 1 End If Else ' We haven't clicked this button before so reset everything LastButtonClicked = Index NumTimesClicked = 1 .Caption = "7" + vbCrLf + "PQRS" Buttons(8).Buttons(5).Caption = "5" + vbCrLf + "JKL" Buttons(6).Caption = "6" + vbCrLf + "MNO" Buttons(7).
Here is all the code: Private Sub DisplayCurrentCharacter() Dim curChar As String Select Case LastButtonClicked Case 2 Select Case NumTimesClicked Case 1 curChar = "a" Case 2 curChar = "b" Case 3 curChar = "c" End Select Case 3 Select Case NumTimesClicked Case 1 curChar = "d" Case 2 curChar = "e" Case 3 . What this routine does is read the value of our three global variables and display the correct character corresponding to which key was last pressed.HasBeenDisplayed = False End If DisplayCurrentCharacter End Sub The sub DisplayCurrentCharacter is our largest chunk of code.
curChar = "f" End Select Case 4 Select Case NumTimesClicked Case 1 curChar = "g" Case 2 curChar = "h" Case 3 curChar = "i" End Select Case 5 Select Case NumTimesClicked Case 1 curChar = "j" Case 2 curChar = "k" Case 3 curChar = "l" End Select Case 6 Select Case NumTimesClicked Case 1 curChar = "m" Case 2 .
curChar = "n" Case 3 curChar = "o" End Select Case 7 Select Case NumTimesClicked Case 1 curChar = "p" Case 2 curChar = "q" Case 3 curChar = "r" Case 4 curChar = "s" End Select Case 8 Select Case NumTimesClicked Case 1 curChar = "t" Case 2 curChar = "u" Case 3 curChar = "v" End Select Case 9 .
Text = Left(Text1.1) End If ' Now add the current char to the display Text1.SelLength = 1 ' Set the flag so we know we have displayed this character HasBeenDisplayed = True .SelStart = Len(Text1.Select Case NumTimesClicked Case 1 curChar = "w" Case 2 curChar = "x" Case 3 curChar = "y" Case 4 curChar = "z" End Select End Select If HasBeenDisplayed = True Then ' We have already displayed once so remove it and change it Text1.SetFocus Text1.Text = Text1.Text + curChar Text1.Text.Text) .Text) . Len(Text1.1 Text1.
Enabled = False Select Case LastButtonClicked Case 2 Select Case NumTimesClicked Case 1 curChar = "a" .Enabled = False End Sub Private Sub DisplayCurrentCharacter() Dim curChar As String Timer1.Text) ResetAll End Sub Also add the following code to the current routine (bold lines are the new code) Private Sub ResetAll() NumTimesClicked = 0 LastButtonClicked = -1 HasBeenDisplayed = False Timer1.End Sub Private Sub Timer1_Timer() ' This gets called if nothing has been pressed for 2 seconds Text1.SelStart = Len(Text1.SelLength = 0 Text1.
Case 2 curChar = "b" Case 3 curChar = "c" End Select Case 3 Select Case NumTimesClicked Case 1 curChar = "d" Case 2 curChar = "e" Case 3 curChar = "f" End Select Case 4 Select Case NumTimesClicked Case 1 curChar = "g" Case 2 curChar = "h" Case 3 curChar = "i" End Select Case 5 Select Case NumTimesClicked .
Case 1 curChar = "j" Case 2 curChar = "k" Case 3 curChar = "l" End Select Case 6 Select Case NumTimesClicked Case 1 curChar = "m" Case 2 curChar = "n" Case 3 curChar = "o" End Select Case 7 Select Case NumTimesClicked Case 1 curChar = "p" Case 2 curChar = "q" Case 3 curChar = "r" Case 4 .
curChar = "s" End Select Case 8 Select Case NumTimesClicked Case 1 curChar = "t" Case 2 curChar = "u" Case 3 curChar = "v" End Select Case 9 Select Case NumTimesClicked Case 1 curChar = "w" Case 2 curChar = "x" Case 3 curChar = "y" Case 4 curChar = "z" End Select End Select If HasBeenDisplayed = True Then .
. Len(Text1. Exercise 2 Write an application to display the name of all controls which are present on the form Exercise 3 Write an application to add and remove command button at run time.Text) .SelLength = 1 ' Set the flag so we know we have displayed this character HasBeenDisplayed = True ' Start the timer incase they don't click anyting for 2 seconds Timer1.Text = Text1.Enabled = True End Sub Exercise 1 Create a city catalog. Implement a collection for storing city names and temparatures.Text) .' We have already displayed once so remove it and change it Text1.1 Text1.Text + curChar Text1.Text.Text = Left(Text1.SelStart = Len(Text1.1) End If ' Now add the current char to the display Text1.SetFocus Text1.
phone and email. 2 space separator. The determination for how these fields are to be laid out on the printed line is as follows: 19 characters for the first and last name combined. 26 characters for the address. and page number as well as column headings will appear at the top of each page. 5 characters for the zip code. first name. time.Activity 6 : Class Module Building class module Property procedure Class builder Exercise 1 Create a class called customer with properties like First name. Main headings with date. 2 characters for the state. city. There are two command buttons: "Print Customer List". which causes the above report to be printed. 1 space separator. 23 characters for the city. and zip code. Address. address. The user interface isn't much to speak of. Exercise 2 Create a class supplier using the class builder. opening and Printing Documents Example: Printing in VB6 using the Printer Object The application reads in a comma-delimited text file of customer records. . 1 space separator. Each record contains the following fields: last name. Activity 7: Object Linking and Embedding OLE container control Runtime embedding and linking OLE Automation Saving. Create methods for accepting details and displaying the details of the customer. and an "Exit" button which ends the application. 1 space separator. Add method fullname to concatenate first name and last name of the supplier.Last name. state.
The commented code for this application is shown below. Option Explicit '----------------------------------------------------------------------------Private Sub cmdPrint_Click() '----------------------------------------------------------------------------Dim intLineCtr As Integer Dim intPageCtr As Integer Dim intX As Integer Dim strCustFileName As String Dim strBackSlash As String Dim intCustFileNbr As Integer Dim strFirstName As String Dim strLastName As String Dim strAddr As String Dim strCity As String Dim strState As String . A handful of techniques are introduced here which have not been covered in the previous tutorials. but will be in later tutorials.
...1 If Printer. vbOKCancel.FontSize = 10 ' initialize report variables .FontName = Printer.. For intX = 0 To Printer.FontCount .".. if available (otherwise.Fonts(intX) Exit For End If Next Printer.Fonts(intX) Like "Courier*" Then Printer. "Check Printer") = vbCancel _ Then Exit Sub End If ' Set the printer font to Courier.. intPageCtr = 0 intLineCtr = 99 ' initialize line counter to an arbitrarily high number . we would be relying on the default font for the Windows printer. which may or may not be set to an appropriate font) .Dim strZip As String Const intLINE_START_POS As Integer = 6 Const intLINES_PER_PAGE As Integer = 60 ' Have the user make sure his/her printer is ready . If MsgBox("Make sure your printer is on-line and " _ & "loaded with paper.
strCity. _ Tab(21 + intLINE_START_POS). _ strFirstName & " " & strLastName.' to force the first page break ' prepare file name & number strBackSlash = IIf(Right$(App. 1) = "\". strState.txt" intCustFileNbr = FreeFile ' open the input file Open strCustFileName For Input As #intCustFileNbr ' read and print all the records in the input file Do Until EOF(intCustFileNbr) ' read a record from the input file and store the fields there into VB variables Input #intCustFileNbr. _ . invoke the PrintHeadings subroutine to do a page break If intLineCtr > intLINES_PER_PAGE Then GoSub PrintHeadings End If ' print a line of data Printer. strAddr.Path. "\") strCustFileName = App. strLastName. strFirstName.Path & strBackSlash & "customer. strZip ' if the number of lines printed so far exceeds the maximum number of lines ' allowed on a page. "".Print Tab(intLINE_START_POS).
the EndDoc method of the Printer object must be invoked. _ Tab(48 + intLINE_START_POS). ' releasing the document to the print device or spooler. _ strState. _ Tab(72 + intLINE_START_POS). Printer. ' The EndDoc method terminates a print operation sent to the Printer object.SetFocus Exit Sub . _ strCity. _ strZip ' increment the line count intLineCtr = intLineCtr + 1 Loop ' close the input file Close #intCustFileNbr ' Important! When done.EndDoc cmdExit.strAddr. _ Tab(76 + intLINE_START_POS).
NewPage End If ' increment the page counter intPageCtr = intPageCtr + 1 ' Print 4 blank lines.' internal subroutine to print report headings '-----------PrintHeadings: '-----------' If we are about to print any page other than the first.Print Printer.Print ' Print the main headings . These four lines do NOT ' count toward the limit of 60 lines.Print Printer. invoke the NewPage ' method to perform a page break. Printer. The NewPage method advances to the next ' printer page and resets the print position to the upper-left corner of the ' new page. If intPageCtr > 0 Then Printer. which provides a for top margin.Print Printer.
_ Format$(Time. _ Tab(intLINE_START_POS + 31).Print ' Print the column headings Printer. _ Tab(21 + intLINE_START_POS). _ .Printer.Print Tab(intLINE_START_POS). _ "Print Date: ". _ Tab(intLINE_START_POS + 73). _ "CUSTOMER LIST" Printer.COM". _ "CITY".Print Tab(intLINE_START_POS). _ "ADDRESS". _ "Print Time: ". "mm/dd/yy").Print Tab(intLINE_START_POS). _ "THE VBPROGRAMMER. "@@@") Printer. _ Tab(48 + intLINE_START_POS). _ "Page:". _ Tab(intLINE_START_POS + 33). _ Format$(intPageCtr. "hh:nn:ss"). _ "CUSTOMER NAME". _ Format$(Date.
_ "ZIP" Printer. _ "--". _ Tab(76 + intLINE_START_POS). _ Tab(72 + intLINE_START_POS). _ Tab(21 + intLINE_START_POS). _ "----". intLineCtr = 6 Return End Sub .Print Tab(intLINE_START_POS). _ "---" Printer.Print ' reset the line counter to reflect the number of lines that have now ' been printed on the new page. _ Tab(76 + intLINE_START_POS). _ "-------------".Tab(72 + intLINE_START_POS). _ Tab(48 + intLINE_START_POS). _ "ST". _ "-------".
'----------------------------------------------------------------------------Private Sub cmdExit_Click() '----------------------------------------------------------------------------End End Sub Exercise In Excel create a sheet for recording the marks obtained by students in various subjects like Maths. The >> button skips to the next row and << skips to the previous row When skipping the rows ensure that you do not go beyond the range of the data rows available in your sheet. Using OLE automation display the data rows from Excel sheet. In VB . The Quit button should terminate the link and end the application. The start button establishes the OLE link with the desired Excel sheet and displays the first data row from the sheet.chemistry and Biology. create a form containing text boxes for student’s name Maths.<< and quit.physics. chemistry and biology and command buttons start.>>.physics. Activity 8: Ms Windows common and Third party controls ImageList control ListView control TreeView control TabStrip control Controls for accepting User Input ImageCombo control UpDown control MonthView control DTPicker control StatusBar control ProgressBar control Slider Control Animation Control Common Dialog control .
Color dialog Font dialog Open. with the names and property settings for each control on the form. . shown in callouts. as well as for the form itself. The design-time form is shown below. SaveAs dialog Print dialog Help dialog RichTextBox control LoadFile method SaveFile method Example: COMMON DIALOG DEMO PROGRAM The Common Dialog demo program shows how to use each of the six types of dialog boxes.
FileName = "" .Flags = cdlOFNHideReadOnly .txt|All Files(*.*)|*.ShowOpen strFileToOpen = .Filter = "Text Files(*.Path End Sub Private Sub cmdOpen_Click() Dim strBuffer As String Dim intDemoFileNbr As Integer Dim strFileToOpen As String On Error GoTo cmdOpen_Click_Exit With dlgDemo .*" .Option Explicit Private mstrLastDir As String Private Sub Form_Load() mstrLastDir = App.CancelError = True .txt)|*.FileName End With On Error GoTo cmdOpen_Click_Error .InitDir = mstrLastDir .
1) Exit Sub cmdOpen_Click_Error: MsgBox "The following error has occurred:" & vbNewLine _ & "Err # " & Err. _ vbCritical.Number & " .intDemoFileNbr = FreeFile Open strFileToOpen For Binary Access Read As #intDemoFileNbr strBuffer = Input(LOF(intDemoFileNbr).Text = strBuffer Close #intDemoFileNbr mstrLastDir = Left$(strFileToOpen. intDemoFileNbr) txtTestFile.Description. InStrRev(strFileToOpen." & Err. "\") . _ "Open Error" cmdOpen_Click_Exit: End Sub Private Sub cmdSave_Click() Dim strBuffer As String Dim intDemoFileNbr As Integer Dim strFileToSave As String On Error GoTo cmdSave_Click_Exit .
Flags = cdlOFNOverwritePrompt + cdlOFNPathMustExist .Text Put #intDemoFileNbr.FileName End With On Error GoTo cmdSave_Click_Error intDemoFileNbr = FreeFile Open strFileToSave For Binary Access Write As #intDemoFileNbr strBuffer = txtTestFile.With dlgDemo .FileName = "" .*" .InitDir = mstrLastDir .*)|*.1) Exit Sub cmdSave_Click_Error: . . InStrRev(strFileToSave.ShowSave strFileToSave = .txt)|*. "\") .txt|All Files(*.CancelError = True .Filter = "Text Files(*. strBuffer Close #intDemoFileNbr mstrLastDir = Left$(strFileToSave.
_ vbCritical.ShowPrinter intCopies = . _ "Save Error" cmdSave_Click_Exit: End Sub Private Sub cmdPrint_Click() Dim intX As Integer Dim intCopies As Integer On Error GoTo cmdPrint_Click_Exit With dlgDemo .Copies End With On Error GoTo cmdPrint_Click_Error For intX = 0 To Printer.FontCount .Number & " .MsgBox "The following error has occurred:" & vbNewLine _ & "Err # " & Err.Flags = cdlPDHidePrintToFile + cdlPDNoPageNums _ + cdlPDNoSelection .1 .Description.CancelError = True ." & Err.
Print txtTestFile. _ vbCritical. _ "Print Error" cmdPrint_Click_Exit: .Number & " .FontName = Printer.Fonts(intX) Like "Courier*" Then Printer.If Printer." & Err.Text Next Printer.NewPage End If Printer.FontSize = 10 For intX = 1 To intCopies If intX > 1 Then Printer.Fonts(intX) Exit For End If Next Printer.EndDoc Exit Sub cmdPrint_Click_Error: MsgBox "The following error has occurred:" & vbNewLine _ & "Err # " & Err.Description.
BackColor = lngColor Exit Sub cmdColor_Click_Error: MsgBox "The following error has occurred:" & vbNewLine _ & "Err # " & Err.Color End With On Error GoTo cmdColor_Click_Error lblColor.Description.Number & " ." & Err. _ vbCritical. _ "Color Error" cmdColor_Click_Exit: End Sub .ShowColor lngColor = .CancelError = True .End Sub Private Sub cmdColor_Click() Dim lngColor As Long On Error GoTo cmdColor_Click_Exit With dlgDemo .
FontName & ".FontSize & " pt" End With Exit Sub cmdFont_Click_Error: .FontUnderline = .FontUnderline lblFont.FontSize lblFont.FontName = .FontStrikethru lblFont.Flags = cdlCFBoth + cdlCFForceFontExist + cdlCFEffects .Color lblFont.FontItalic = . " & .Private Sub cmdFont_Click() Dim lngFont As Long On Error GoTo cmdFont_Click_Exit With dlgDemo .FontBold = .FontName lblFont.FontItalic lblFont.FontStrikethru = .Caption = .FontSize = .CancelError = True .ShowFont On Error GoTo cmdFont_Click_Error lblFont.ForeColor = .FontBold lblFont.
MsgBox "The following error has occurred:" & vbNewLine _ & "Err # " & Err.CancelError = True .ShowHelp End With Exit Sub cmdHelp_Click_Error: End Sub .Number & " .Path & "\JETSQL35. _ "Font Error" cmdFont_Click_Exit: End Sub Private Sub cmdHelp_Click() On Error GoTo cmdHelp_Click_Error With dlgDemo .HelpFile = App." & Err.HelpCommand = cdlHelpForceFile .Description. _ vbCritical.HLP" .
The procedure to open the common dialog box to browse the picture files as well as to load the selected picture into the picture box is :- CommonDialog1.ShowOpen Picture1. In this program. click on the picture property and select a picture that resembles an open file icon.ico|All Files(*.Filter = "Bitmaps(*.jpg)|*.Example 2: Let’s create a program that enables the users to open and choose files from the folders in their PC. you need to insert a picture box. In the image properties windows.FileName) .WMF|Jpeg Files(*.BMP)|*.WMF)|*. This can be done easily using a picture box and a common dialog box.*)|*.Picture = LoadPicture(CommonDialog1.gif)|*.*" CommonDialog1.ico)|*.gif|Icon Files(*.jpg|GIF Files(*.BMP|Metafiles(*. a common dialog box and an image.
Exercise 2 Create a small application which has a ImageCombo box control which stores any of three font types of the label control.jpg|GIF Files(*. the size of text box control should decrease. jpg.BMP)|*.BMP)|*.ico)|*. and uses the pipe line | to separate different file types. the text box control should grow in size. gif.ShowOpen Picture1.gif|Icon Files(*. ico(icon) and cur(cursor) files.Picture = LoadPicture (CommonDialog1.Filter = "Bitmaps(*. Also add an UpDown control which is used for incrementing ar decrementing the font size. wmf. As the slider moves towards the right. the font of the caption of label control should change accordingly. Visual Basic supports most of the picture formats namely bmp.WMF)|*.BMP|Metafiles(*. . The whole program is shown below and the output is shown in the figure below: Private Sub Image1_Click() CommonDialog1.*)|*. When the slider is moved towards left.The filter property of the common dialog box uses the format as shown below Bitmaps(*.jpg)|*.BMP to specify the file type.FileName) End Sub Exercise 1 Create a small application which consists of a slider control and a text control.Picture = LoadPicture (CommonDialog1.WMF|Jpeg Files(*.gif)|*.ShowOpen is to open the common dialog box and the command Picture1.ico|All Files(*. When the user selects the specific font from ImageCombo box.*" CommonDialog1.FileName) is to load the selected picture file into the picture box. The command CommonDialog1.
The TreeView Control should help in viewing the list of students in each of three tracks. On your form.Exercise 3 Save and load files using Common Dialog box Exercise 4 A training Institute runs two types of the courses (a) Short term (b) Long term courses. and the RecordSource is the name of the table in that database that you want to use. Create a small application which uses a TabStrip control and TreeView control such that the TabStrip control enables to switch between two courses Short Term and Long Term. Put a data control (an intrinsic control.) Set the properties of the three textboxes as follows: . (If you were to open the database in Access. Activity 9: Managing database Access Using data Control Database Access using Visual Basic Designing a database Data Control and Data Bound Control Data Control events Creating database file using Visual Data Manager Creating Form using Data Form wizard Data Access objects DataBondGrid Control DataBound List and Combo Box control Example 1: Connecting to an Access Database Using the VB Data Control STEPS: 1. in that order. Open a new Visual Basic project. and Year Born. DatabaseName is the name of the database you want to use. the properties that must be set are DatabaseName and RecordSource. 3. located in the VB toolbox) on the form and set the properties . Notes: When you use the Data Control in a project. you would see that the three fields of the Authors table are Au_ID. Each type of course can then again have students which do courses in (a) Fast track (b) Slow track and (c) Superfast track. create a text box for each field in the Authors table. 2. with labels. Author.
At this point. then move back to the record you changed. When you want a control (such as a text box) to display data from a database. your form should resemble the screen-shot below: 4. The data control automatically updates a record when you move off of the record. and the DataField is the name of the particular field in the database that should be displayed in the control (this field will be in the table that was chosen for the RecordSource of the data control). set the Enabled property of txtAuthID to False. Use the arrows on the data control to scroll through the data. Save and run the project. . On any record. Note that your changes remain in effect. the properties that must be set are DataSource and Datafield. Move ahead. 5.Name txtAuthID txtAuthor txtYearBorn DataSource datAuthors datAuthors datAuthors DataField Au_ID Author Year Born In addition. The DataSource is the name of the data control on the form (it should already be configured). change the data in the author name or year born field.
STEPS: 1. Make four command buttons with the following properties: Name cmdMoveNext cmdMoveLast cmdMovePrevious cmdMoveFirst Caption Next Record Last Record Previous Record First Record At this point. your form should resemble the screen-shot below 4. you could move to the first. which is what this exercise demonstrates. or last record. Put the following four lines of code in the appropriate Click events for the buttons: . you saw that by clicking specific buttons of the data control. Set the Visible property of the data control (datAuthors) to False. MoveNext. 2. next. 3. and MoveLast methods. MovePrevious. You can also invoke these methods through code. previous. Copy the files from Exercise #1 into a new folder and open the VBP file in the new folder.Example 2: Using Navigation Buttons with a Data Control In the previous exercise. What is happening is that the data control is automatically invoking specific methods of the recordset object: namely the MoveFirst.
Recordset. Name. The Form will only access the database using a data control and eleven text box controls. The form should be used to browse the customer data. the save button should be disabled.MoveNext datAuthors. Exercise 2 Using Data Form Wizard create the same application as given in Exercise 1. Code datAuthors.MovePrevious datAuthors. . Add more functionalities to the application such that you are able to add and update the records.Recordset. instead of that use Data Access object. In this application when the form is loaded. Price. Last. What happens? (Fix this) Exercise 1 Access Customers table from NorthWind database. Quantity. Move to the last record and then click the Move Next button twice.Recordset. No field should be left blank and the item no should accept only numeric values.MoveLast datAuthors. In this application you will not use data control to access the database. The table should have the following fields like Item No. When Delete button is clicked ask user for confirmation.Event cmdMoveNext_Click cmdMoveLast_Click cmdMovePrevious_Click cmdMoveFirst_Click 5. Save and run your program. Update. Exercise 3 Create a table called item using Visual data manager. The Update button should update the table with the contents displayed on the form. Next. The form will have First. New. Following Validity checks must be done before updating and saving data. Delete and save command buttons .Recordset. The delete button should delete the current record displayed on the form.MoveFirst 6. Design and develop an application which will access all the fields of the customers table and show each record at a time on the form using bound controls. Previous. Develop an application which accesses the Item table.
After the user has entered a record. . fields should be blanked out and Save button should get enabled and caption of close button should be changed to cancel and accordingly the function of the button should also change. display the message when the previous button is pressed when user is on first record. When New button is clicked. On clicking the Next button when the user is on the last record. Similarly. the save button should be clicked for the record to be actually added to the table and also the caption of cancel button should again change to close. an appropriate message should be displayed.
.Activity 10: Data Project and Report Handling Data Project Data Report Crystal Reports Exercise Make Reports depending upon your project requirements.
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.