"Visual Basic" VB6 ListView Control, Access2003 Database additions and deletions to determine whether there is a Chinese, multi-form operation

Source: Internet
Author: User
Tags access database

VB6 to Access2003 database additions and deletions is not complicated, can be easily done by ADO object, below is a small example, also shows the use of the ListView control in VB6. Although in the "Visual Basic" list control of the ListView Change, modal dialog box, disable window resizing " VB.net's ListView control has been described in detail, but proved that Microsoft is a pit Daddy, VB6 for the ListView implementation of code actually with vb.net has a completely different, seems to change a language like. Change the code what's the most annoying.

First, in the VB6 generated project folder There is a Db1.mdb this Access2003 file,


Db1.mdb password for the admin, inside a userinfo, as follows, a simple to no longer simple user information table, there are three fields, where the ID has no meaning, purely because the Access2003 in any table are required to set the primary key, the key is the user name and password of the two fields.


Specifically how to create, set Access2003 password, I am in the "Access2003" Table of new, with SQL statement query, close the warning message, modify the database password, repair the database (click to open the link) has already said, here no longer repeat.

Following the establishment of a VB project, the use of Db1.mdb to store user information, each time you open this EXE, automatically load Access2003 user information. Provides the ability to add data, modify data, and delete data. In the process of adding and removing changes, the contents of Db1.mdb are modified at the same time. Regardless of whether this exe exits or not, information is automatically stored.

You must not insert Chinese data into the database.

When deleting information, automatically update the number, keep 1,2,3,4,.... The ascending relationship does not resemble the Access2003 in the self-increment ID fault.


The production process is as follows

1, first, a new VB project, such as, through the engineering and parts, open the part manager, VB6 provides the basic control is not enough. There is simply no connection to the database, with the ListView component.

Some lite versions of VB6 may be problematic, and the full version of VB6 is recommended


Select Microsoft ADO Data Control 6.0 OLE DB to add database functionality to this project.


Add Microsoft Windows Common controls, which we need in the ListView control.


2, after the Form1 to the following layout, dragged into a ListView1, three button, the Form1 title of the caption to change, while the border style changed to 1-fixed style, which is much simpler than vb.net. At the same time, Maxbutton and Minbutton are changed to Flase, disable the maximum minimum button.


3, then, such as create a new Form2 form

4, the Form2 layout adjustment to the following layout, the same is disabled the maximum minimum button, drag into two label text, its alignment property is 2-center, two edit box, a button. In the process of adjusting the form, you can select multiple controls and adjust to an aesthetically pleasing level by using buttons such as format, alignment, and uniform dimensions.


5, in the case of FORM1 form editing state activation, press F7 Open the Code interface, write the following code, here the idea and "Visual Basic" List control ListView additions and Deletions, modal dialog box, no window resizing "Similarly, Just because the VB6 and vb.net to the ListView control operation, resulting in a slight modification of the code, while adding VB6 to the Access2003 database additions and deletions of content, in fact, is VBScript is the ASP of Access database operations, this in the " ASP "ASP to Access database connection, add and Remove Check and ASP basic Syntax" (click Open link) has been discussed. The only notable thing is that all of the tables, columns (fields) in the statements of the VB Operations database must be marked with [], brackets, and if not indicated, an error. Look at the notes in detail:

' The process of initializing the ListView control, while the process of loading the Form_Load form will be called Sub Listview_init () Listview1.view = Lvwreport ' Set list display mode Listview1.gridlin Es = True ' Display network line listview1.labeledit = lvwmanual ' prohibit label edit Listview1.fullrowselect = True ' Select whole line ' Initialize three column header, here the width note do not  Over 100%, there will be an unsightly horizontal scrollbar ListView1.ColumnHeaders.Add,, "number", Listview1.width/4 ListView1.ColumnHeaders.Add,, "username",    LISTVIEW1.WIDTH/3 ListView1.ColumnHeaders.Add,, "Password", listview1.width/3end Sub ' Add button private Sub Command1_Click () Form2.selectitemindex =-1 form2.show vbmodal ' Form2 form opens the End Sub ' modify button as a modal box private Sub Command2_Click () If LISTVI Ew1.        Listitems.count <> 0 Then ' if the user has a selection Form2.selectitemindex = ListView1.SelectedItem.Index ' to pass user-selected content to Form2 Form2.Text1.Text = Listview1.listitems (ListView1.SelectedItem.Index). SubItems (1) Form2.Text2.Text = Listview1.listitems (ListView1.SelectedItem.Index). SubItems (2) form2.show vbmodal Else MsgBox "Please select one of them!" ", vbOKOnly," warning "End IfEndSub ' delete button private Sub Command3_Click () if ListView1.ListItems.Count <> 0 Then ' if user has selected ' Connect database Dim C Onn as New ADODB. Connection Dim rs as New ADODB. The Recordset ' Data source is followed by the path and file name of database password, which is the password conn for this database. Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & App.Path & "/db1.mdb; Jet oledb:database password=admin "' Deletes an item in the database username_deleted = Listview1.listitems (Listview1.selecteditem. Index). SubItems (1) Conn.        Execute "Delete from [UserInfo] where [username]= '" & username_deleted & "';" ' Delete an item in ListView1 ListView1.ListItems.Remove ListView1.SelectedItem.Index ' update the number in the ListView1 for i = 1 T o Form1.ListView1.ListItems.Count listview1.listitems (i). Text = I ' cannot use subitems (0) to fetch the first column Next Conn. Close Else MsgBox "Please select one of these items! ", vbOKOnly," warning "End IfEnd Sub ' form load function private Sub Form_Load () ' Initialize form call listview_init ' Connect database Dim Conn as New ADODB. Connection Dim rs as New ADODB. The Recordset ' Data source is followed by the path and file name of database password, which is the password conn for this database. Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & App.Path & "/db1.mdb;    Jet oledb:database password=admin "' Last parameter, 1 is open read-only, read-write is 3. ' Write down the query statement for the database Select, implement the feature you want Rs. Open "SELECT * from UserInfo", Conn, 1, 3 ' will query to the things that are loaded into ListView1 If (Rs. BOF and Rs. EOF) then MsgBox ("The database has no data! ") Else i = 0 do and not Rs. EOF i = i + 1 ListView1.ListItems.Add,, I listview1.listitems (i). SubItems (1) = Rs. Fields ("username") Listview1.listitems (i). SubItems (2) = Rs. Fields ("Password") Rs. MoveNext Loop End If Rs. Close Conn. CloseEnd Sub

6, after the Form2 code, according to Form1 passed over the designation, to achieve the corresponding function, the function of the judgment according to the global variable public selectitemindex As Integer control, see note:

Public Selectitemindex as Integer ' is a global variable controlled by FORM1, used to determine the modification,    Or add item ' to determine whether the process of Chinese, in VB has a return value of the process written function, no return value of the process written as SubFunction Ischineseexist (s as String) as a Boolean Dim I as Integer        For i = 1 to Len (s) If ASC (Mid (S, I, 1)) < 0 Then ischineseexist = True Exit Function        End if Next ischineseexist = FalseEnd Function ' form's loading process private Sub Form_Load () If selectitemindex =-1 Then Me.caption = "Add data" Command1.Caption = "Add Data" Else me.caption = "Modify data" Command1.Caption = "Modify The click process of the "End IfEnd Sub" button private Sub Command1_Click () ' connects to the database Dim conn as New ADODB. Connection Dim rs as New ADODB. The Recordset ' App.Path represents the path conn where EXE resides. Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & App.Path & "/db1.mdb; Jet oledb:database password=admin "If Selectitemindex = 1 Then ' If it is added if Ischineseexist (text1.text) Or Ischines Eexist (Text2.text) then MsgBox "User name, password, any one can not contain Chinese!     ", vbOKOnly," warning "   Else If Text1.Text = "" Or text2.text = "Then MsgBox" username, password, any item cannot be empty! ", vbOKOnly," Warning "Else" form items are added Form1.ListView1.ListItems.Add, form1.listview1. ListItems (Form1.ListView1.ListItems.Count) + 1 Form1.ListView1.ListItems (Form1.ListView1.ListItems.Count). S Ubitems (1) = Text1.Text Form1.ListView1.ListItems (Form1.ListView1.ListItems.Count). SubItems (2) = Text2.text ' database added Conn.                 Execute "INSERT into [userinfo] ([Username],[password]) VALUES ('" & Text1.Text & "', '" & Text2.text & "');" ' Empty the contents of two edit boxes Text1.Text = ' Text2.text = ' ' Close Form2 this form, VB6 No There are me.close only me.hide me.hide End If the If Else ' if is modified one of the If Ischineseexist (Tex T1. Text) Or ischineseexist (text2.text) then MsgBox "username, password, any one cannot contain Chinese! ", vbOKOnly," warning "Else IF text1.text = "" Or text2.text = "Then MsgBox" username, password, any item cannot be empty! ", vbOKOnly," Warning "Else" first stores the user name and password for this item, and then modifies it with the UPDATE statement, note that the UPDATE statement can only modify one field at a time Oldus Ername = Form1.ListView1.ListItems (Selectitemindex). SubItems (1) OldPassword = Form1.ListView1.ListItems (Selectitemindex). SubItems (2) ' modifies the contents of the form Form1.ListView1.ListItems (Selectitemindex). SubItems (1) = Text1.Text Form1.ListView1.ListItems (selectitemindex). SubItems (2) = Text2.text ' modifies the database conn.                Execute "Update [userinfo] set [username]= '" & Text1.Text & "' Where [username]= '" & Oldusername & "';" Conn.                Execute "Update [userinfo] set [password]= '" & Text1.Text & "' Where [password]= '" & OldPassword & "';"                ' Empty the contents of two edit boxes Text1.Text = ' Text2.text = ' ' Close this dialog box     Me.hide End If   End If End If ' closes the connection conn for the database. CloseEnd Sub


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Visual Basic" VB6 ListView Control, Access2003 Database additions and deletions to determine whether there is a Chinese, multi-form operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.