First knowledge of VB Database Development Instance 1 (system logon)

Source: Internet
Author: User

Design Analysis

There are two basic methods for the logon dialog box of the database management system.

Method 1: create a data table in the database to save the System user login information. In the design Login Dialog Box, query the data table to complete verification. (General)

Method 2: For some database management systems that support security verification, such as SQL, you can define the System user as a database user, and then design the Login Dialog Box to connect to the database using the entered username and login password, let the database management system complete the verification.

 

Technical Essentials

Set the default button: Set the default attribute of the command button control to true. After the setting, press enter to select the button.

Set the cancel button: Set the cancle attribute of the command button to true. After setting, Press ESC or press enter to select it when the button obtains focus.

 

Custom function Process

Function Name (parameter 1 as data type, parameter 2 as data type ......) As data type

......

Function name = return value

......

End Function

 

Note: to return a value from a function, you only need to assign the value to the function name. This value can be assigned anywhere in the process. If no return value is specified for the function process, the process returns a default value: the value function returns 0, the string function returns a zero-length string (""), and the variant function returns empty

 

Operation

1. Create a database

(1. Select "external program"> "visualized Data Manager" in VB)

(2) click File> New> Microsoft
Access-> version7.0 MDB command, write "instance 1" in the file name as the database file name

(3) Click Save

Because it is a new database, only one properties node is displayed in the Database window. The attribute of this node is

(4) Right-click the database window and choose "New table"

(5) Enter "system user" in the table name text box as the data table name,

(6) Click the add field button. In this dialog box, you need to specify the field name, type, size, sequence position, verification text, verification rules, default value, and auto-increment, the field size is fixed or variable, whether it is zero length, whether it is necessary field and other information

(7) after setting, click OK to add a field. The add field dialog box is reset to continue adding.

(8) Click Add Index

(9) table structure after the index is added

(10) after adding fields and indexes to the data table, click Generate table to create a data table.

(11) Right-click "system user" and select "open"

(12) Click Add.

(13) enter the user name and password, and click the update button.

(14) Exit the visual data manager and create a database.

 

2. design the form

The form contains two text boxes, two labels, and two buttons.

Note: Change the text box name to txtpassword and txtusername, and set the passwordchar attribute of txtpassword to * (use * instead of user input to hide the password)

The button name is changed to cmdok and canccancel.

3. Create an ADO reference

Select project> reference

4. write code

Option explicitconst maxlogtimes as integer = 3' defines the maximum number of logon information that can be severe.'Cancel buttonPrivate sub login cancel_click () dim intresult as integer 'request the user to log out of the system. intresult = msgbox ("Do you really want to log out of the system? You cannot start the management system! "& Vbcrlf _ &" are you sure you want to exit? ", Vbyesno," Logon Verification ") if intresult = vbyes then endend sub'OKPrivate sub cmdok_click () Static intlogtimes as integerdim intchecked as integer, strname as string, strpassword as stringintlogtimes = intlogtimes + 1' count the number of logins if intlogtimes> maxlogtimes then msgbox "you have exceeded the allowed number of logins! "& Vbcr _ &" the application will end! ", Vbcritical," Logon Verification "endelse strname = trim (txtusername. text) 'obtain the input username strpassword = trim (txtpassword. text) 'obtain the entered password' check the validity of the user name and password, and execute the corresponding operation select case check_password (strname, strpassword) according to the check return value) case 0 msgbox "<" & strname & _ "> it is not a system user. Check whether the user name is entered correctly! ", Vbcritical," Logon Verification "txtusername. setfocus txtusername. selstart = 0 txtusername. sellength = Len (txtusername) Case 1 msgbox" Password error. Please log on again! ", Vbcritical," Logon Verification "txtpassword =" "txtpassword. setfocus case 2": the password is correct. Unload me msgbox ": The system program is started successfully! ", Vbinformation," login verification "case else msgbox" login verification failed! Run the logon program again. "& vbcrlf _ &" if you still cannot log on, report it to the system administrator! ", Vbcritical," Logon Verification "End select end if end sub'User name and password verification processPrivate function check_password (byval username as string, byval password as string) as byteon error goto gperrordim objcn as new connection, objrs as new recordset, strcn as stringdim strsql as string 'establish database connection' msgbox (App. path) objcn. connectionstring = "provider = Microsoft. jet. oledb.3.51; "& _" Data Source = "&" D: \ vb98 \ System logon \ instance 1.mdb" 'msgbox (objcn. connectionstring) objcn. open 'Run the query command to obtain the user logon password strsql = "select password from system user where username ='" & username & "'" set objrs. activeconnection = objcnobjrs. open (strsql) 'determines whether there are any query results if objrs. EOF then check_password = 0' no query result, indicating that the user is an illegal user else if password <> trim (objrs. fields ("password "). value) Then check_password = 1' the password is incorrect else check_password = 2' the password is correct end ifend if 'to close the database connection and release the object objcn. closeset objrs = nothingset objcn = nothingexit functiongperror: check_password = 255 set objrs = nothing set objcn = nothing end Function

 

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.