Using SQL queries in Visual Basic 6

<Previous Lesson><<Home>>< Next Lesson>


In the previous lesson, we have learned to use the DataGrid Control to display data from a database in Visual Basic 6 environment. However, it does not allow users to search for and select the information they want to see. In order to search for a certain information, we need to use SQL query. SQL stands for Structures Query Language. Using SQL keywords, we are able to select specific information to be displayed based on certain criteria.
SELECT fieldname1,fieldname2,.....,fieldnameN  FROM  TableName
                                         SELECT  * FROM  TableNam
Next, we will start Visual Basic and insert an ADO control, a DataGrid and three command buttons. Name the three command buttons as cmdAuthor, cmdTitle and cmdAll. Change their captions to Display Author ,Display Book Title and Display All respectively. You can also change the caption of the form to My Books. The design interface is shown below:
lesson 25 for the details. However, you need to make one change. At the ADODC property pages dialog box, click on the Recordsource tab and select 1-adCmdText  under command type and under Command Text(SQL) key in SELECT * FROM book.

Next, click on the command button cmdAuthor and key in the following statements:
Private Sub cmdAuthor_Click()
Adodc1.RecordSource = "SELECT Author FROM book"
Adodc1.Refresh
Adodc1.Caption = Adodc1.RecordSource

End Sub
and for the command button cmdTitle, key in
Private Sub cmdTitle_Click()
Adodc1.RecordSource = "SELECT Title FROM book"
Adodc1.Refresh
Adodc1.Caption = Adodc1.RecordSource

End Sub
Finally for the command button cmdAll, key in
Private Sub cmdAll_Click()
Adodc1.RecordSource = "SELECT * FROM book"
Adodc1.Refresh
Adodc1.Caption = Adodc1.RecordSource


End Sub
Now, run the program and when you click on the Display Author button, only the names of authors will be displayed, as shown below:
and when you click on the Display Book Title button, only the book titles will be displayed, as show below:
Lastly, click on the Display All button and all the information will be displayed.
Share on Google Plus
    Google Plus Comment
    Facebook Comment

0 comments :

Post a Comment

//]]>