This is one of the best code for input fields that only require
numbers. The textbox only accept numbers and ignore string or
characters.
1.) In this sample we use only one textbox so add textbox to your form.
2.)Lets code. In order for us to get the right one we will use the KEYPRESS event
******paste this********
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim KeyChar As String
If KeyAscii > 31 Then 'ignore all string or characters except backspace
KeyChar = Chr(KeyAscii)
If Not IsNumeric(KeyChar) Then
KeyAscii = 0 'disable keys except numbers
End If
End If
End Sub
**********************************
3.)Run the project. I hope this is helpful
Enjoy :)
Wednesday, August 15, 2012
Visual Basic 6.0 Part 7 - Full Review of Part 1 - Part 6
Instrcutions:
1.) build first this objects and do not rename the names of the objects
-3 Textbox -
-3 Command Button
-1 Combo Box {put MALE & FEMALE on property list}
-1 Listview {it must be Report View ,4 Column or check it here in step 3 }
-1 Module
2.)Create a database
-"DB" name of the database
-create table "tblData"
-put this field
*ID - autonumber
*Fullname - text
*Age - number
*Gender - text
3.)After making all of this Save you projects in 1 folder also your database.so here's the view
of our database and project.
4.) Check if the Microsoft Activex Data Object is already selected. If you don't know what it is check here.
5.) Copy and Paste the code
****Go to module and paste this code .****
Public con As New ADODB.Connection
Public rs As New ADODB.Recordset
Public view As ListItem 'this is for listitem subitem
Public key As Integer
Sub conn() '<--- for easy database connection in every form
con.Provider = "microsoft.jet.oledb.4.0;" '<--- if your using mdb extension of your database
'<----"microsoft.ace.oledb.12.0;" this for accb extension
con.ConnectionString = App.Path & "/db.mdb"
con.Open
'MsgBox "CONNECTED" '<---- after successfull connecttion erase this message box
End Sub
***********************************************************************
****Go to form double click and clear the code and paste this in you form.****
Private Sub Command1_Click()
Dim sql As String 'declare string variable
sql = "Insert into tblData (fullname,age,gender) Values ('" & Text1.Text & "' , " & Text2.Text & " , '" & Combo1.Text & "') "
con.Execute sql
Call displayData 'for automatic display of data
MsgBox "successfully added!"
End Sub
Private Sub Command2_Click()
sql = "Update tblData set fullname = '" & Text1.Text & "' , age = " & Text2.Text & " , gender= '" & Combo1.Text & "' where id = " & key & ""
con.Execute sql
Call displayData 'for automatic display updated data
MsgBox " successfully updated!"
End Sub
Private Sub Command3_Click()
sql = "DELETE from tblData where id = " & key & ""
con.Execute sql
Call displayData 'automatic delete from display without refreshing
MsgBox "successfully deleted"
End Sub
Private Sub Form_Load()
Call conn '<--- requesting form for the connection
Call displayData '<----- calling the displayData function to display the records automatically when form load
End Sub
Function displayData()
Set rs = New ADODB.Recordset '<-- declaring new recordset
sql = "SELECT * from tblData" '<--- SELECT statement
rs.Open sql, con, adOpenStatic, adLockOptimistic '<--- opening recordset ,connection source
ListView1.ListItems.Clear
If rs.RecordCount > 0 Then 'checking if the record is greater than zero
rs.MoveFirst
Do While Not rs.EOF 'looping the data until goes to end of file
Set view = ListView1.ListItems.Add(, , rs.Fields(0)) 'this is number of fields
view.SubItems(1) = rs!Fullname 'or you can do it like this
view.SubItems(2) = rs.Fields(2)
view.SubItems(3) = rs.Fields(3)
rs.MoveNext
Loop
End If
End Function
Private Sub ListView1_DblClick()
Set rs = New ADODB.Recordset
sql = "SELECT * from tblData where id = " & ListView1.SelectedItem & ""
rs.Open sql, con, adOpenStatic, adLockOptimistic
key = rs.Fields(0)
Text1 = rs.Fields(1) 'you can do like this or
Text2 = rs!Age 'you can do it like this
Combo1 = rs.Fields(3)
End Sub
Private Sub Text3_Change()
Call searchh
End Sub
Function searchh()
Set rs = New ADODB.Recordset
sql = "SELECT * from tblData where fullname like '%" & Text3.Text & "%'" '
rs.Open sql, con, adOpenStatic, adLockOptimistic
ListView1.ListItems.Clear
If rs.RecordCount > 0 Then
rs.MoveFirst
Do While Not rs.EOF
Set view = ListView1.ListItems.Add(, , rs.Fields(0))
view.SubItems(1) = rs!Fullname
view.SubItems(2) = rs.Fields(2)
view.SubItems(3) = rs.Fields(3)
rs.MoveNext
Loop
End If
End Function
***********************************************************************
6.) RUN the program..
Enjoy :)
1.) build first this objects and do not rename the names of the objects
-3 Textbox -
-3 Command Button
-1 Combo Box {put MALE & FEMALE on property list}
-1 Listview {it must be Report View ,4 Column or check it here in step 3 }
-1 Module
2.)Create a database
-"DB" name of the database
-create table "tblData"
-put this field
*ID - autonumber
*Fullname - text
*Age - number
*Gender - text
3.)After making all of this Save you projects in 1 folder also your database.so here's the view
of our database and project.
4.) Check if the Microsoft Activex Data Object is already selected. If you don't know what it is check here.
5.) Copy and Paste the code
****Go to module and paste this code .****
Public con As New ADODB.Connection
Public rs As New ADODB.Recordset
Public view As ListItem 'this is for listitem subitem
Public key As Integer
Sub conn() '<--- for easy database connection in every form
con.Provider = "microsoft.jet.oledb.4.0;" '<--- if your using mdb extension of your database
'<----"microsoft.ace.oledb.12.0;" this for accb extension
con.ConnectionString = App.Path & "/db.mdb"
con.Open
'MsgBox "CONNECTED" '<---- after successfull connecttion erase this message box
End Sub
***********************************************************************
****Go to form double click and clear the code and paste this in you form.****
Private Sub Command1_Click()
Dim sql As String 'declare string variable
sql = "Insert into tblData (fullname,age,gender) Values ('" & Text1.Text & "' , " & Text2.Text & " , '" & Combo1.Text & "') "
con.Execute sql
Call displayData 'for automatic display of data
MsgBox "successfully added!"
End Sub
Private Sub Command2_Click()
sql = "Update tblData set fullname = '" & Text1.Text & "' , age = " & Text2.Text & " , gender= '" & Combo1.Text & "' where id = " & key & ""
con.Execute sql
Call displayData 'for automatic display updated data
MsgBox " successfully updated!"
End Sub
Private Sub Command3_Click()
sql = "DELETE from tblData where id = " & key & ""
con.Execute sql
Call displayData 'automatic delete from display without refreshing
MsgBox "successfully deleted"
End Sub
Private Sub Form_Load()
Call conn '<--- requesting form for the connection
Call displayData '<----- calling the displayData function to display the records automatically when form load
End Sub
Function displayData()
Set rs = New ADODB.Recordset '<-- declaring new recordset
sql = "SELECT * from tblData" '<--- SELECT statement
rs.Open sql, con, adOpenStatic, adLockOptimistic '<--- opening recordset ,connection source
ListView1.ListItems.Clear
If rs.RecordCount > 0 Then 'checking if the record is greater than zero
rs.MoveFirst
Do While Not rs.EOF 'looping the data until goes to end of file
Set view = ListView1.ListItems.Add(, , rs.Fields(0)) 'this is number of fields
view.SubItems(1) = rs!Fullname 'or you can do it like this
view.SubItems(2) = rs.Fields(2)
view.SubItems(3) = rs.Fields(3)
rs.MoveNext
Loop
End If
End Function
Private Sub ListView1_DblClick()
Set rs = New ADODB.Recordset
sql = "SELECT * from tblData where id = " & ListView1.SelectedItem & ""
rs.Open sql, con, adOpenStatic, adLockOptimistic
key = rs.Fields(0)
Text1 = rs.Fields(1) 'you can do like this or
Text2 = rs!Age 'you can do it like this
Combo1 = rs.Fields(3)
End Sub
Private Sub Text3_Change()
Call searchh
End Sub
Function searchh()
Set rs = New ADODB.Recordset
sql = "SELECT * from tblData where fullname like '%" & Text3.Text & "%'" '
rs.Open sql, con, adOpenStatic, adLockOptimistic
ListView1.ListItems.Clear
If rs.RecordCount > 0 Then
rs.MoveFirst
Do While Not rs.EOF
Set view = ListView1.ListItems.Add(, , rs.Fields(0))
view.SubItems(1) = rs!Fullname
view.SubItems(2) = rs.Fields(2)
view.SubItems(3) = rs.Fields(3)
rs.MoveNext
Loop
End If
End Function
***********************************************************************
6.) RUN the program..
Enjoy :)
Visual Basic 6.0 part 6 - Searching Data
This is the last part of our tutorial since were done all the basic statement lets do the searching of data.
1.)Add a textbox and put in the top of the listview.
2.)Since this is a basic search we will just separate the code of search to make it for you to understand and not to be confused. As far as you remember part 2 code of display here is the same as search but we will just add a few line. see image below
3.) After done coding the fucntion go to textbox and click then call the function.
4.)Run the program and search.
Enjoy :)
This is the last part of our basic visual basic 6.0 databasing.
Part 7 - The Overview of the basic from part 1 - part 6
1.)Add a textbox and put in the top of the listview.
2.)Since this is a basic search we will just separate the code of search to make it for you to understand and not to be confused. As far as you remember part 2 code of display here is the same as search but we will just add a few line. see image below
3.) After done coding the fucntion go to textbox and click then call the function.
4.)Run the program and search.
Enjoy :)
This is the last part of our basic visual basic 6.0 databasing.
Part 7 - The Overview of the basic from part 1 - part 6
Visual Basic 6.0 part 5 - Deleting Data
DELETE statement this is one of the easiest and shortest code among
the rest. All you have to do is to get unique identifier of the selected
record and then delete using delete statement.
1.)Add a new command button and rename the caption to Delete.
2.) Click the button and add the code . see image below.
3.) Save first a new records name it delete me.. see image below..
4.)Note: This is the continuation of our Part 3. You must SELECT first the data in the listview before deleting. The code of selecting data from listview is in part 4. To those who skip here's the link here
- Select the added data and then delete. see image below
Enjoy :)
Part 6 - Searching data
1.)Add a new command button and rename the caption to Delete.
2.) Click the button and add the code . see image below.
3.) Save first a new records name it delete me.. see image below..
4.)Note: This is the continuation of our Part 3. You must SELECT first the data in the listview before deleting. The code of selecting data from listview is in part 4. To those who skip here's the link here
- Select the added data and then delete. see image below
Enjoy :)
Part 6 - Searching data
Visual Basic 6.0 part 4 - Updating Data
Were done already in SELECT and INSERT and this time we will use the UPDATE statement.
1.)Add the command button , rename the caption to UPDATE
2.)Go to Module and declare or add this code "Public Key as Integer" this holds the unique key of the selected records.
3.)Before updating records first thing to do is to SELECT which record you want to update. Double click the listview and then set the event to Listview1_Dbclick and then start coding. "see image below". Analyze the SELECT statement which means that the ID of the record you selected is in listview. first column.
tips (if the unique identifier of the record is in the other column of listview use this statement ex : (" ListView1.SelectedItem.SubItems(1) ") this means that your unique identifier is in the column 2 of lisview.
4.)Go to button update and click and code for the UPDATE statement. Always remember the syntax of string and integer. see the arrows.. and as what you notice that ("Where ID = "& key &" see image below) it is referring to the code SELECT where the veralue of the unique identifier is equal to Key which we declared as integer because our ID is integer.
tips: you can also string if your unique identifier is string. ( KEY as string )
1.)Add the command button , rename the caption to UPDATE
2.)Go to Module and declare or add this code "Public Key as Integer" this holds the unique key of the selected records.
3.)Before updating records first thing to do is to SELECT which record you want to update. Double click the listview and then set the event to Listview1_Dbclick and then start coding. "see image below". Analyze the SELECT statement which means that the ID of the record you selected is in listview. first column.
tips (if the unique identifier of the record is in the other column of listview use this statement ex : (" ListView1.SelectedItem.SubItems(1) ") this means that your unique identifier is in the column 2 of lisview.
4.)Go to button update and click and code for the UPDATE statement. Always remember the syntax of string and integer. see the arrows.. and as what you notice that ("Where ID = "& key &" see image below) it is referring to the code SELECT where the veralue of the unique identifier is equal to Key which we declared as integer because our ID is integer.
tips: you can also string if your unique identifier is string. ( KEY as string )
5.) After coding run the program.
Enjoy :)
Part 5 - Deleting data
Visual Basic 6.0 part 3 - Displaying Data
Displaying data using LISTVIEW object and SELECT statement. just follow the steps below.
1.) First thing to do is add the Components for LISTVIEW press CTRL+T to open components window and find Microsoft Windows Common Controls 6.0 (sp6) and select and then apply. see image.
2.)Add Listview in your form
3.)to set up the listview property press mouse right click and then select properties. Follow the image below.
4.)Go to module and declare this variables.To import listview subitems we need to declare and assign to a variable and then for our recordset also.see image.
See database on what option you like to use in calling the fields.
its either rs.Fields(index) or rs!FieldsName
5.)After done coding run the program. If you successfully run it without bugs it means you copy the code correctly.
6.)Additional Info
Since we have done already our step 2.
Call the display function in your SAVE button.
and run your program and then add new data. It will automatically display the data without refreshing.
Enjoy :)
Part 4 will be updating data from you database
1.) First thing to do is add the Components for LISTVIEW press CTRL+T to open components window and find Microsoft Windows Common Controls 6.0 (sp6) and select and then apply. see image.
2.)Add Listview in your form
3.)to set up the listview property press mouse right click and then select properties. Follow the image below.
4.)Go to module and declare this variables.To import listview subitems we need to declare and assign to a variable and then for our recordset also.see image.
See database on what option you like to use in calling the fields.
its either rs.Fields(index) or rs!FieldsName
5.)After done coding run the program. If you successfully run it without bugs it means you copy the code correctly.
6.)Additional Info
Since we have done already our step 2.
Call the display function in your SAVE button.
and run your program and then add new data. It will automatically display the data without refreshing.
Enjoy :)
Part 4 will be updating data from you database
Visual Basic 6.0 part 2 - Inserting Data
This is a basic data inserting using Insert SQL statement for beginners.
1.) Make a table in your database name it tblData and has 3 fields name , age and gender . see the image below
2.) In your form drag objects 2 textbox ,1 combo box and 1 command button. Do not rename the objects and change the caption of the command button to SAVE.
3.) Go to combo box properties and go to list and then input Male and Female for your dropdown list.
4.)Lets start coding. Double-click the save button and then input the code. see the image below.
tips:BE CAREFUL number and string has different syntax if your database value is string like ex. fullname use this statement ' " & text1 & " ' while the number value has no single quote " & text2 &".
5.)After coding run the program.Input your name ,age and gender. Error will occurred if you leave it blank or input string to age.
6.)after it successfully added!! check your database..
Enjoy :)
Part 3 will be displaying data from database using listview
1.) Make a table in your database name it tblData and has 3 fields name , age and gender . see the image below
2.) In your form drag objects 2 textbox ,1 combo box and 1 command button. Do not rename the objects and change the caption of the command button to SAVE.
3.) Go to combo box properties and go to list and then input Male and Female for your dropdown list.
4.)Lets start coding. Double-click the save button and then input the code. see the image below.
tips:BE CAREFUL number and string has different syntax if your database value is string like ex. fullname use this statement ' " & text1 & " ' while the number value has no single quote " & text2 &".
5.)After coding run the program.Input your name ,age and gender. Error will occurred if you leave it blank or input string to age.
6.)after it successfully added!! check your database..
Enjoy :)
Part 3 will be displaying data from database using listview
Visual Basic 6.0 part 1 - Database Connection
All we have to do here is only the database connection using
Microsoft Access and nothing else. If you already know what it is,skip
to other part which makes helpful for you.
1)Open VB6.0 application. Go to project tab and select references. check if Microsoft ActiveX Data Objects is selected if not SELECT only one. see the image below
2.)Add module and Save the Project in one folder and lets the coding.
3.)Go to module and code. follow the code see image below
4.)After coding at the module go to Form1 double click and then call the function. Be sure it is coded at the form_load.
5.) After done coding click SAVE and then Run the program. If the msgbox pop-up it means your successfully connected to database
Enjoy :)
Part 2 is SQL Statement, Inserting Information to the database.
1)Open VB6.0 application. Go to project tab and select references. check if Microsoft ActiveX Data Objects is selected if not SELECT only one. see the image below
2.)Add module and Save the Project in one folder and lets the coding.
3.)Go to module and code. follow the code see image below
4.)After coding at the module go to Form1 double click and then call the function. Be sure it is coded at the form_load.
5.) After done coding click SAVE and then Run the program. If the msgbox pop-up it means your successfully connected to database
Enjoy :)
Part 2 is SQL Statement, Inserting Information to the database.
Subscribe to:
Posts (Atom)