A very very very basic program, write bad, but the general file read and write operations and character processing functions are related to the. For beginners to learn

Source: Internet
Author: User
Tags error handling

The following is saved as "contacts." FRM "Can

VERSION 5.00
Begin VB. Form Form1
Caption = "My Contacts"
ClientHeight = 2985
ClientLeft = 60
ClientTop = 345
ClientWidth = 8235
LinkTopic = "Form1"
ScaleHeight = 2985
ScaleWidth = 8235
startupposition = 3 ' window default
Begin VB. Frame Frame2
Caption = "prompt message:"
Height = 495
left = 120
TabIndex = 13
Top = 2400
Width = 8055
Begin VB. Label labmsg
Height = 240
left = 480
TabIndex = 14
Top = 200
Width = 7095
End
End
Begin Vb.commandbutton Cmdadd
Caption = "Add"
Height = 495
left = 2400
TabIndex = 12
TOP = 1800
Width = 1215
End
Begin Vb.commandbutton Cmdref
Caption = "Modify"
Height = 495
left = 6240
TabIndex = 11
TOP = 1800
Width = 1215
End
Begin Vb.commandbutton Cmddel
Caption = "Delete"
Height = 495
left = 4320
TabIndex = 10
TOP = 1800
Width = 1215
End
Begin Vb.commandbutton Cmdqury
Caption = "Query"
Height = 495
Left = 600
TabIndex = 9
TOP = 1800
Width = 1215
End
Begin VB. Frame Frame1
Caption = "Personnel Information:"
Height = 1455
left = 120
TabIndex = 0
Top = 120
Width = 7935
Begin VB. TextBox Txtarr
Appearance = 0 ' Flat
Height = 375
Index = 3
left = 3960
TabIndex = 8
Top = 840
Width = 3855
End
Begin VB. TextBox Txtarr
Appearance = 0 ' Flat
Height = 375
Index = 2
left = 840
TabIndex = 5
Top = 840
Width = 2415
End
Begin VB. TextBox Txtarr
Appearance = 0 ' Flat
Height = 375
Index = 1
left = 2760
TabIndex = 3
Top = 240
Width = 5055
End
Begin VB. TextBox Txtarr
Appearance = 0 ' Flat
Height = 375
Index = 0
left = 840
TabIndex = 2
Top = 240
Width = 1095
End
Begin VB. Label Label4
Caption = "Company:"
Height = 375
left = 3360
TabIndex = 7
Top = 960
Width = 735
End
Begin VB. Label Label3
Caption = "Phone:"
Height = 375
left = 240
TabIndex = 6
Top = 960
Width = 1215
End
Begin VB. Label Label2
Caption = "Address:"
Height = 255
left = 2160
TabIndex = 4
TOP = 360
Width = 735
End
Begin VB. Label Label1
Caption = "Name:"
Height = 255
left = 240
TabIndex = 1
TOP = 360
Width = 495
End
End
End
Attribute vb_name = "Form1"
Attribute Vb_globalnamespace = False
Attribute vb_creatable = False
Attribute Vb_predeclaredid = True
Attribute vb_exposed = False
Option Explicit
Dim mpathname As String ' Save string variable for address book path and name

Private Sub Form_Load ()
' First, for the convenience of the back, but also to improve efficiency, we put the directory file path and name into a variable
Mpathname = App.Path & "/Address Book. txt"
' Here, we open the check first, we save the Address Book text file exists, if not exist, build it
If Dir (mpathname) = "Then
Open Mpathname for output as #1 ' opens the text file in sequential output, this file will be created automatically if the file does not exist
Close #1
End If
End Sub


' Next, we implement the added functionality
' Before this, first think about how we should preserve this information, how different people's information is separated, how different information about the same person is separated, and how the various functions can be achieved with a separate approach.
The code uses this method: Different people's records in different lines, a line of one person (separated by carriage return newline character), the same person's different information between the semicolon (separated by semicolons)
' In the form of the following
' Puppy dog; 25252525; professional Housekeeping Limited liability company
' Kitten; 737373; professional Fish stealing AG

Private Sub Cmdadd_click ()
On Error GoTo MERR ' Error handling
' To add data, enter it in order! ' Connect each data into the text, and don't forget to add a separator in the middle.
Open mpathname for Output as #1
Print #1, Txtarr (0). Text & ";" & Txtarr (1). Text & ";" & Txtarr (2). Text & ";" & Txtarr (3). Text
Close #1
' Write it, test it!
' How to feel a little bit less ... Yes, there is no information to suggest that our program does not tell the user, the user does not know how the operation is done.
' Fill in an informational cue, simple, with a label control
labmsg.caption = "Add Operation complete"
MERR:
MsgBox Err.Number & ":" & Err.Description
labmsg.caption = "Add operation failed"
Exit Sub
' There is a question Oh ~ If the name is repeated, is it possible to prompt the user to repeat the addition, or overwrite it, leaving the landlord
End Sub


'************************************************************************************************************** *************************


' OK, fill it out, then do the query, here are some basic functions, apply the separation we set up just now

Private Sub Cmdqury_click ()
Dim mlinestr as String ' used to store variables for each row of data
Dim MSTR () as String to store individual information for each person
Dim Mindex as Long ' for loop variable used
' First, open read like the file content, and then split by the delimiter, and then find the line we want to show it

Open mpathname for Input as #1 ' here, we line read this file, find the match on the display, no on the prompt did not find
Do and not EOF (1)
Line Input #1, Mlinestr
If InStr (1, mlinestr, Txtarr (0). Text) Then ' if the string we're looking for appears in this line of data, then we'll show it and exit the loop.
MSTR () = Split (Mlinestr, ";") ' Separate the information in the row that holds the information of the queried person, and then display it
For mindex = 0 to 3
Txtarr (Mindex) = MSTR (Mindex)
Next
Exit Do ' Now that we've found it, we don't have to continue to read the file, but don't use the exit SUB, so close #1 not executed:)
End If
Loop
Close #1
labmsg.caption = "Query Complete"
' Well, another one, can be tested, but we did not consider the situation with the same name Oh! And, we can also achieve by telephone and other means of inquiry, but also can be "fuzzy query": Because the use of InStr function Hemp ~, you can find all the surname Zhang, the name of three, and so on, this left the landlord to do it
End Sub
'************************************************************************************************************** **************************
' Then delete it, because the modified function is also used to delete
Private Sub Cmddel_click ()
' Delete, nothing but to replace the existing information with empty, then the query to some information to empty it and then save to the file will be!
' First, read the contents of the file back, here use 2 read, mainly the application of these functions
Dim Marray () as Byte, Mfilelen as Long, mstr as String, Mtmpstr () As String, mindex as Long ' for loop variable
Open mpathname for Binary as #1 ' read a file into an array
Mfilelen = LOF (1)
ReDim Marray (mFileLen-1)
Get #1, Marray ()
Close #1
MSTR = StrConv (Marray (), Vbunicode) ' combines the numbers into a string
MTMPSTR () = Split (MSTR, VbCrLf) ' separates the string into a string array
' Query the records containing that name and delete them
For mindex = LBound (mtmpstr) to UBound (MTMPSTR)
If INSTR (1, Mtmpstr (Mindex), Txtarr (0)) then Mtmpstr (mindex) = "" If the specified name is included, the variable is emptied, we do not exit the loop, and all records containing the name are deleted.
Next
' Write the rest of the records in the file.
Open mpathname for Output as #1
For mindex = LBound (mtmpstr) to UBound (MTMPSTR)
Print #1, Mtmpstr (Mindex)
Next
Close #1
labmsg.caption = "Delete Complete"
' Test it.
End Sub
'************************************************************************************************************** **************************
' Below ... We write changes
Private Sub Cmdref_click ()
' Modify Ah, is to replace the original empty, and then write a new go in ~ so the operation first Delete ~ Call Cmddel_click directly, and then call Cmdadd_click to be able to
Cmddel_click
Cmdadd_click
End Sub
' Well, just write here, the basic function will be able to use, error handling a lot of did not write! Leave the landlord to do their own ~ ~ ~
' File operation part of the need to learn something combined with character processing function is actually quite a lot, but commonly used can be written together, do a project, often look after, there may be many revelations, at least forget how to use when you can take to copy! haha

The comments are very comprehensive. The basic functions involved are many, have been introduced more clearly ... In fact, we can open the file in the Form_Load event without closing it and closing it in the Form_Unload event .... This is the point to add ...

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.