VB's first project (1) and vb's first project

Source: Internet
Author: User

VB's first project (1) and vb's first project

Foreword ----- I am new to VB. I can understand the company's VB code, but I can only develop it myself. Generally, when you are learning a new language, you may encounter some misunderstandings of course. so, write down and share the development and learning process, and hope to correct it. ----------- The world is wonderful for sharing, and Android is more powerful for open source.

 

 

I don't know if anyone is like me and can't read those VB books. In this case, it's better to study in projects, when I was learning java, my first project was fuck's student information management system. I started with a lot of things.

First, I will analyze this little project, so it is very informal and straightforward to look at what it looks like. (generally, regular companies have to make a lot of pictures, what is the table? 60% of the time in a case is so hot ).

Function module division

  • Log on registration (IndexForm) is divided into four stages: 1. Comparison string, character string level implementation (one person ). 2. Compare objects and implement object layers (one person ). 3. array, set, and list hierarchy (multiple persons ). 4. Database-level implementation (multiple individuals), connecting to oracle and Access database.
  • Display, add, delete, modify, and query (Mainform ). Displays the basic information of students to achieve basic maintenance of student information data.

 

This is a simple Excel-level management system. Solve the following problems before starting the Code.

Question 1: What does a VB code file look like.

 

Generally, the following files are generated. Normally, at least three files are generated.

First, vbp. vbw. frm is required, where. vbp stores version copyright and other information,.Vbwsave is a piece of information from the project, .frm's window code, which is generally not modified into A. EXE file yet. To run it, you must run the. vbp file. The Black Arrow. scc file is generated only when the external team works with the software VSS .. Cls is the product of the class module,. bas is the product of the module, other. pag is the suffix of the attribute file, and. ctl is the suffix of the user control file. Is it annoying? Only. java and. class files are available in java.

  

Question 2: How to debug the program in VB.

The old master told me on the first day I entered the company that F8 was executed in one step. Normally, pressing F8 will directly reach the program portal. Shift + F8. the return value is directly obtained without entering the function method. F5 advances at full speed. Generally, it is used together with breakpoint. It is set at the end of the loop and then at full speed.

In VB, there seems to be no console output. I have been searching for a long time, and I have tried a lot and failed to output it in the console. Some people know it. Let me know, just like in java, System. out. console in println and c. writeLine (). My solution is to open the ''immediate "window ,? + "Variables copied from code" + enter, and a value is output.

The shortcut key is ctrl + G. The lowest way is to point the mouse to the variable.

 

Question 3: What is the folder architecture.

When I don't know what spring, mybits, and so on, the javaEE frameworks that have been blown up by the industry, I always feel that these frameworks are awesome. When I really master these frameworks, it gives me the feeling that folders and configuration files are XML-loaded X. Of course, this is my joke, and the framework is still awesome. In contrast, the folders in VB seem to have been limited, which makes me very uncomfortable. Although there is a three-tier architecture, that is, the file division of the model-controller-view, but in the actual project, how to divide the file is determined by myself, but in VB, it seems to have changed, there are only naming rights.

You can only click to add these items under the project. Three of them are form, module, and class module. Naturally, question 4 exists.

Question 4: differences between the module and the class module and how the standards in the two are defined. For example, javaBean has a very strict format, including attribute permissions and empty parameter construction, parameter construction, set/get function, etc.

  

 

  The general meaning is that the objects defined in the module cannot be instantiated, the objects defined in the class module can be instantiated, and there is also a difference in scope. There are some common Sub and functions stored in the module ). The class is probably a standard class!

Let's take a look at two examples of standard modules and class modules to see what is defined and how it is defined. The specific difference is that when I define it myself, try to summarize.

Module:

1 Public fMainForm As frmMain 2 Private IsConnect As Boolean 'Mark whether the database is connected to 3 4 Private cnn As ADODB. connection 'Connection object of the database 5 Private rs As ADODB. recordset 'Save result set Recordset object 6 Public strSQL As string' save String 7 Public Conn As string' connection String 8 Public MyUser As New clsuser' user object 9 Public CurUser as New clsUser 'current user object 10 Public strDB As string' database name 11 Public Pension_Rate As Single 'pension insurance payment 12 Public Medi_Rate As Single 'medical insurance payment ratio 13 Public Tax_Start As Currency 'tax start point 14 Sub Main () 15 Dim fLogin As New frmLogin 16 strDB = "\ DATA \ salary. mdb "17 Conn =" Provider = Microsoft. jet. OLEDB.4.0; Data Source = "& _ 18 App. path + strDB + "; Persist Security Info = False" 19 20 fLogin. show vbModal 21 If Not fLogin. OK Then 22' Login Failed, exit application 23 End 24 End If 25 Unload fLogin 26 frmSplash. show 27 frmSplash. Refresh 28 Set fMainForm = New frmMain 29 Load fMainForm 30 Unload frmSplash 31 fMainForm. show 32 End Sub 33 34 Public Sub Connect () 'connect to database 35 If IsConnect = True then' If the connection mark is True, return. Otherwise, the error 36 Exit Sub 37 End If 38 Set cnn = New ADODB will occur. connection 'key new' is used to create a New object cnn 39 cnn. connectionString = conn' set the connection string ConnectionString attribute 40 cnn. open 'Open the connection to the database 41 42 If cnn. state <> adStateOpen then' determines the connection status 43 MsgBox "database connection failed", vbOKOnly + vbCritical, "warning" 'If the connection fails, a message is displayed, exit program 44 End 45 End If 46 47 IsConnect = true' sets the connection flag, indicating that the database has been connected to 48 End Sub 49 50 Public Sub Disconnect () 'disconnect from the database 51 Dim rc As Long 52 If IsConnect = False Then Exit Sub 'If the connection is marked As False and the connection is disconnected, 53 cnn is returned directly. close 'close connection 54 55 Set cnn = Nothing 56 IsConnect = False 57 End Sub 58 '59' Public Sub DB_Connect () 'Use Connect_Num to control database connection 60' Connect_Num = Connect_Num + 1 61 'connect 62' End Sub 63' Public Sub DB_Disconnect () 64 'if Connect_Num> = CONNECT_LOOP_MAX Then 65 'Connect_Num = 0 66 'disconnect 67' End If 68 'End Sub 69' Public Sub DB_Disconnect_Forced () 'force the database to be shut down, counter reset 70 'connect_num = 0 71 'disconnect 72' End Sub 73 74 Public Sub SQLExt (ByVal strSQLstmt As String) 'execute database operation statement 75 Dim cmd As New ADODB. command 'create Command object cmd 76 connection' to Connect to database 77 Set cmd. activeConnection = cnn 'sets the ActiveConnection attribute of cmd and specifies the connection to the associated database 78 cmd. commandText = strSQLstmt 'set the command text to be executed 79 cmd. execute 80 Set cmd = Nothing 81 Disconnect 82 End Sub 83 84 Public Function QueryExt (ByVal strSQLstmt As String) As ADODB. recordset 'execute the database query statement 85 Dim rst As New ADODB. recordset 86 Connect 'connect to database 87 88 Set rst. activeConnection = cnn 'sets the ActiveConnection attribute of rst and specifies the connection to the associated database 89 rst. cursorType = adOpenDynamic 'sets the cursor type 90 rst. lockType = adLockOptimistic 'sets the locking type 91 rst. open strSQLstmt 'open record Set 92 Set QueryExt = rst' return record Set 93 End Function 94 95 Public Function CountTax (curPay As Currency) 'definition process, used to calculate income tax 96 Dim curTemp As Currency, curTax As Currency 97 curTemp = curPay-Tax_Start 98 Select Case curTemp 99 Case Is <= 0100 curTax = 0101 Case 0.01 To 500102 curTax = curTemp * 0.05103 Case 500.01 To 2000104 curTax = curTemp * 0.1 -25105 Case 2000.01 To 5000106 curTax = curTemp * 0.15-125107 Case 5000.01 To 20000108 curTax = curTemp * 0.2-375109 Case 20000.01 To 40000110 curTax = curTemp * 0.25-1375111 Case 40000.01 To 60000112 curTax = curTemp * 0.3-3375113 Case 60000.01 To 80000114 curTax = curTemp * 0.35-6375115 Case 80000.01 To 100000116 curTax = curTemp * 0.4-10375117 Case Is> 100000118 curTax = curTemp * 0.45-15375119 End select120 CountTax = curTax121 End Function122 123 Public Function MakeStr (ByVal Str As String) as String124 MakeStr = Trim (Replace (Str, "'", "'' ") 125 End Function

Class module:

1 Public strName As String 'user name 2 Public strPWD As string' password 3 Public iUserClass As Integer 'user type 4 Public Sub Init () 5 strName = "" 6 strPWD = "" 7 iUserClass = 0 8 End Sub 9 10 Public Sub DeleteUser (ByVal UserName As String) 'delete User data 11 If UserName = "Admin" Then12 Exit Sub13 End If14 strSQL = "delete from [User] WHERE name = '" + Trim (UserName) + "'" 15 SQLExt (strSQL) 16 End Sub17 18 Public Function GetInfo (ByVal UserName As String) As Boolean19 Dim rs As New ADODB. recordset20 strName = UserName21 strSQL = "SELECT * FROM [User] WHERE name = '" + Trim (UserName) + "'" 22 Set rs = QueryExt (strSQL) 23 If rs. EOF Then24 Init 'Call the initialization method of this Class 25 GetInfo = False26 Exit Function27 Else28 strPWD = Trim (rs. fields (1) 29 iUserClass = rs. fields (2) 30 GetInfo = True31 End If32 End Function33 34 Public Function ExistUser (ByVal UserName As String) As Boolean 'user 35 Dim rs As New ADODB. recordset36 strSQL = "SELECT * FROM [User] WHERE Name = '" + Trim (UserName) + "'" 37 Set rs = QueryExt (strSQL) 38 If Not rs. EOF Then39 ExistUser = True40 Else41 ExistUser = False42 End If43 End Function44 45 Public Sub AddUser () 'add User 46 strSQL = "insert into [User] Values ('" + Trim (strName) + "','" + Trim (strPWD) _ 47 + "'," + Trim (Str (iUserClass) + ")" 48 SQLExt (strSQL) 49 End Sub50 Public Sub UpdateUser (ByVal UserName As String) 'Change User information 51 strSQL = "Update [User] Set name = '" + Trim (strName) _ 52 + "', PWD = '"+ Trim (strPWD) +"', UserClass = "+ Trim (Str (iUserClass) _ 53 +" WHERE name = '"+ Trim (UserName) + "'" 54 SQLExt (strSQL) 55 End Sub56 57 Public Sub UpdatePassword (ByVal UserName As String) 'Change User Password 58 strSQL = "Update mima Set PWD = '" + Trim (strPWD) _ 59 + "'where name ='" + Trim (UserName) + "'" 60 SQLExt (strSQL) 61 End Sub

 

After solving these general problems, you can start to write a cosmic student information management system.

 

Related Article

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.