1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft Excel VBA Programming for the Absolute Beginner Second Edition phần 7 pdf

50 543 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 1,09 MB

Nội dung

For example, a sequential file containing names and phone numbers may look something like what’sshown here: Public Sub CreateSeqFile Dim filePath As String Dim I As Integer filePath = Ac

Trang 1

The Open Statement

The Openstatement is used to read or write data to a file Table 7.3 summarizes the type ofaccess, and modes or functions available for reading and writing data to a file with VBA

There is also a Binary access type for reading and writing to any byte position in

a file as might be done with an image; however, this technique is beyond thescope of this book

The Openstatement requires several arguments, including a string that designates the path

to a specified file If the file does not exist, then one will be created The Openstatement alsorequires an access mode (Append, Binary, Input, Output, or Random) and a file number Optionalparameters include an access parameter (Read, Write, or Read Write), lock (used to restrictoperations on the file from other programs), and record length (specifies the length of thebuffer or record)

Open “C:\Data\Test.txt” For Input As #1

The preceding line opens a file named Test.txtfound at the designated path for input, andassigns the file to the file number 1 If the file is not found, then one will be created at thedesignated location with the name Test.txt

You can open multiple files in your VBA programs, but they must be assignedunique file numbers

Trang 2

Member Description

ChDir Changes the current directory.

ChDrive Changes the current drive.

CurDir Returns the current directory path.

Dir Returns the name of a file, directory, or folder that matches a pattern,

file attribute, or the volume label of a drive.

FileAttr The mode used to open a file with the Open statement.

FileCopy Copies a file from a source path to a destination path.

FileDateTime Returns the date and time that a file was created or last modified.

FileLen Returns the length of a file in bytes.

FreeFile Returns an Integer representing the next file number available for

use by the Open statement.

GetAttr Returns an Integer representing the attributes of a file or directory.

Kill Deletes a file or files.

Loc Specifies the current read/write position within an open file.

LOF Returns a Long integer specifying the length of an open file in bytes.

MkDir Creates a new directory.

Reset Closes all disk files opened using the Open statement.

RmDir Deletes an empty directory.

Seek Returns a Long integer specifying the current read/write position within an open file.

SetAttr Sets attribute information for a file.

TA B L E 7 2 ME M B E R S O F T H E FI L ESY S T E M OB J E C T

These methods are primarily designed to be used with the Open statement, but you may also

find them useful with the other objects and methods discussed in this chapter.

Trang 3

Example Return Value

ChDir “C:\Documents and Settings” or ChDir “ ” N/A

in a message box.

fileName = Dir(“C:\test.txt”, vbNormal) The file name if it exists

Otherwise an empty string.

the end of an opened file (specified with a file number) has been reached Mode = FileAttr(fileNum, 1) Returns a Long integer indicating the

mode used to open a file (Input, Output, Random, and so on).

FileCopy “C:\TestFile.txt”, “D:\TestFile.txt” N/A

fileDate = FileDateTime(“C:\test.txt”) For example, 1/23/2004 10:25:14 AM fileSize = FileLen(“C:\test.txt”) For example, 4

myAttr = GetAttr(CurDir) 0=Normal, 1=Read-Only, 2=Hidden,

4=System, 16=Directory, 32=Archive

it returns the number of the next record, otherwise it returns the current byte position in the file.

SetAttr “C:\test.txt”, vbReadOnly N/A

Trang 4

Sequential Access Files

Writing information to a sequential access file is sort of like recording music to a cassettetape The songs vary in length and are recorded one after the other Because it is hard toknow the location of each song on the tape, it is difficult to quickly access a particular song.When information is written to a sequential file, the individual pieces of data (usuallystored in variables) vary in length and are written to the file one after the other For example,

a sequential file containing names and phone numbers may look something like what’sshown here:

Public Sub CreateSeqFile()

Dim filePath As String

Dim I As Integer

filePath = ActiveWorkbook.Path & “\SeqPhone.txt”

Open filePath For Output As #1

Access Type Writing Data Reading Data

Sequential Print#, Write# Input#, Input

TA B L E 7 3 FI L E AC C E S S MO D E S W I T H V B A

Trang 5

Using Write #places quotes around each value written to the file The file contains threelines of data because Write #adds a new line character to the end of the last value written

to the file; because the For/Nextloop iterates three times, the Write #statement was executedthree times, resulting in three lines of data

Because the structure of the file is known, it is a simple task to alter the CreateSeqFile()cedure to create a new procedure that reads the data

pro-Public Sub ReadSeqFile()

Dim filePath As String

Dim I As Integer

Dim theName As String

Dim theNumber As String

I = 1

filePath = ActiveWorkbook.Path & “\SeqPhone.txt”

Open filePath For Input As #1

Do While Not EOF(1)

Input #1, theName, theNumber

Cells(I, “A”).Value = theName

Cells(I, “B”).Value = theNumber

Trang 6

Random Access Files

Random access files allow the programmer to access specific values within the file withouthaving to load the entire file into memory This is accomplished by ensuring that the indi-vidual data elements are of the same length before writing to the file Again, consider theexample of a phone book Instead of storing the information as variable-length strings, thename and phone number can be stored with fixed length strings The combination of the twofixed length strings that follow require the same amount of memory for every line written

to the file This will make it easy to locate a particular line in the file when the data is input.Dim theName As String*20

Dim theNumber As String*8

If the name to be stored is less than 20 characters, then spaces are added to match thedefined length If the string exceeds 20 characters, only the first 20 characters of the stringare stored; therefore, it is important to define the length of the string so that it will be longenough to contain any possible value, yet not so long that too much memory is wasted bysaving lots of spaces The resulting data file might then look something like this:

Trang 7

Rather than declare the individual elements of a record as separate variables, it is useful todefine a custom data type that can be used in a variable declaration The variable of thenewly defined type can include all the desired elements of the record To define a phonerecord for the previous example, a custom data type that includes both string elementsmust be declared in the general declarations section of a module.

With the new data type definition, any variable can now be declared in a procedure as typePhone as shown in the CreateRanAccessFile() sub procedure Individual elements of thephoneRecvariable are accessed using the dot operator To take full advantage of the customdata type, I write the phoneRecvariable to a file using random access

Private Type Phone

theName As String*20

theNumber As String*8

End Type

Public Sub CreateRanAccessFile()

Dim phoneRec As Phone

Dim filePath As String

Dim I As Integer, recNum As Integer

recNum = 1

filePath = ActiveWorkbook.Path & “\randomPhone.dat”

Open filePath For Random As #1 Len = Len(phoneRec)

For I = 1 To 3

phoneRec.theName = Cells(I, “A”).Value

phoneRec.theNumber = Cells(I, “B”).Value

Put #1, recNum, phoneRec

to insert the value within the file The record number (indicated by the variable recNuminthe CreateRanAccessFile()procedure) must begin with the value 1

Trang 8

Chapter Project: Word Find

The Word Find project is an Excel VBA program that creates word search puzzles Words for

a puzzle are associated with a topic that the program uses to sort the data The topics andwords used in a puzzle are stored in a random access file The file containing the words andtopics is accessed and displayed by the program New words and topics for puzzles can beadded to the file by the user A puzzle is created when the user selects individual words andplaces them within a fifteen by fifteen grid running in any direction After placing thewords, the empty spaces in the puzzle are randomly filled with letters before printing The

Word Find program is stored on the accompanying CD-ROM as Wordfind.xls

Requirements for Word Find

The objectives for the Word Find project are to demonstrate some basic techniques for file I/O

and error handling in a VBA program To accomplish the task, I use an Excel worksheet asthe grid for a word search puzzle and a VBA form for updating the data required by the pro-gram The requirements for the program follow:

1 A VBA form (UserFormobject) shall be used as the interface for updating the program’sdata (words and topics) stored in a random access file

2 The form shall display all unique topics stored in the data file

3 The form shall display all words stored in the data file that are associated with auser-selected topic

In the Real World

Many applications save data to a type of random access file that is more commonly referred to

as a database Database files such as those created by MS Access (.mdb extension) offer a lot more power to the programmer relative to the random access file created by VBA’s Open state- ment A single database file normally contains multiple tables of data that are linked together through related fields (columns) Furthermore, it is usually possible to use a database’s pro- gramming engine to link your VBA program to a database file such that you can quickly retrieve and update very specific data.

With Excel, it is possible to link your VBA program to an Access database (and many others) even

if the Access GUI has not been installed on your computer Unfortunately, it would take at least

an entire book to properly discuss database normalization, the Structured Query Language (SQL), and ActiveX Data Objects (ADO)—the topics required to understand and use database files in your VBA program.

Trang 9

4 The form shall allow the user to add new records to the data file.

5 The form shall allow the user to update (edit) previously stored words in the data filefor an existing topic Note that the program will not allow for existing topics to beupdated

6 The form shall display new and updated records as they are created

7 An Excel worksheet shall be used to create the word search puzzle

8 The puzzle worksheet shall isolate an area of cells (fifteen by fifteen cells in size) fordisplaying a puzzle

9 The puzzle worksheet shall isolate an area of cells for displaying the list of wordsthat have been added to a puzzle

10 The puzzle worksheet shall isolate an area of cells for displaying help/error messages

to the user when creating a puzzle

11 The puzzle worksheet shall isolate an area of cells for displaying a puzzle’s title

12 The puzzle worksheet shall display a list of unique topics from the data file for theuser to choose from when creating a puzzle

13 The puzzle worksheet shall display a list of words from the data file associated withthe topic selected by the user

14 The user shall be able to select a word from the displayed list of words on the puzzleworksheet and add it to the puzzle by indicating a starting position on the puzzlegrid

15 The user shall be able to select a direction for a word added to the puzzle from aseries of buttons on the worksheet

16 The program shall validate the user’s selection for the location of a word to ensurethe entire word fits within the defined area of the puzzle grid There will be no validation to prevent a word from overwriting another word(s)

17 The user shall be able to clear the contents of the puzzle, the list of words in the puzzle, the list of topics, and the list of words associated with the selected topic from

a button on the worksheet

18 The user shall be able to finish a puzzle by adding randomly selected uppercase letters

to the empty cells in the puzzle grid from a button on the worksheet

19 The user shall be able to print the puzzle and the list of words contained in the puzzlefrom a button on the worksheet

20 The user shall be able to display the form used to update the data in the data filefrom a button on the worksheet Note that the user will not be able to edit the data

in the file directly from the worksheet

Trang 10

21 The user shall be able to refresh the list of topics, and list of words associated with

a topic from a button on the worksheet

22 The data for the program shall be stored in a random access file containing threefields of data per record The first field contains the numbers used to identify spe-cific records (rows) of data The second field contains the topics, and the third fieldcontains the words associated with the topics in the second field

23 The data from the file shall be stored in a worksheet that is hidden from the

user The data from the file shall be written to the worksheet when the user elects

to show the update form

24 When the user chooses to edit an existing record or add a new record, the programshall write the new data to the text file and the hidden worksheet

As with every program you write, you will edit the requirement list after you have designedit; sometimes even after you started writing the program because it is nearly impossible tothink of everything from the beginning I added and removed requirements from the pre-vious list after careful consideration of the program’s design and objectives

Designing Word Find

The Word Find program’s design features objects that are now familiar to you, including a

VBA form and a worksheet The form serves to allow the user to update the data file withnew records as well as edit existing records The worksheet serves as the interface for creating

a word search puzzle The program takes advantage of the grid-like nature of a worksheet tocreate the puzzle Words are written to the puzzle using individual cells for holding the letters.After the puzzle is finished, it is a relatively simple task to print it for someone’s enjoyment

Designing the Form

The program is divided into two parts: the first part contains the form used to update thedata file and the second part contains the worksheet used to create a puzzle Figure 7.16shows the form’s design from the IDE

Whenever a program is required to display a list, you should immediately think List Box orCombo Box control I have added a Combo Box control to the form for displaying the list oftopics and a List Box control for displaying the list of words associated with a selected topic.That is, when the user changes the selection in the Combo Box control the list of words inthe List Box control will also change to match the topic

Trang 11

Although I have not previously discussed the Text Box control, you should find it fairly easy

to use It is a simple control that serves to collect textual input from a user As with all trols, you should edit the Nameand appearance (size, font, and so on) properties of the TextBox control at Design Time The value of the Text property stores whatever is currentlyentered in the Text Box, so this property is frequently accessed in your programs Otherproperties of interest include: MaxLength, MultiLine, PasswordChar, SpecialEffect, TextAlign,and WordWrap You should take the time to familiarize yourself with the Text Box control andsome of its properties The Text Box controls I added to the form serve to display the cur-rently selected topic and word I have used Text Box controls instead of Label controlsbecause the user must be able to enter new values into these controls

con-The purpose of the form is to allow the user to enter new records or edit existing recordsstored in the Wordfind.txtdata file I added the Text Box and Command Button controls tothe form in order to achieve this purpose To add a new record, the user can enter a new word

in the appropriate text box control with an existing topic, or the user can enter a new topicbefore clicking the Add New button To edit an existing record, the user must first select aword from the List Box to enable the Update button Next, the user can edit the word andclick Update When the Update button is clicked, the existing record will be overwritten in thedata file

The data from the file must also be added to a hidden worksheet I decided to use the sheet because I wanted to display the data alphabetically sorted; thus taking advantage ofExcel’s ability to quickly sort data It also makes sense to store the data somewhere it can bequickly and easily accessed, yet still protected A worksheet that is hidden from the userworks quite well, although further protections should probably be added (password protec-tion of the worksheet) It is also important that the data in the hidden worksheet be updated

work-as the file is updated by the user

Figure 7.16

Design Time

view of the form

used for updating

the Word Find

program’s

data file.

Text Box controls

Command Button controls

Label control Combo Box control

List Box control

Trang 12

I have left a couple of potential problems in the form’s design It does not protect againstadding identical records to the data file, does not allow records to be deleted, and it doesnot allow the user to edit the name of an existing topic (in case of a misspelling) I have leftthe challenge of solving these limitations to the reader along with several other enhancements

to the program (see the Challenges at the end of the chapter)

Designing the Worksheet

A word search puzzle is created on a worksheet—the design of which is shown in Figure 7.17

A worksheet makes an ideal interface for this program since it is easy to write letters to thecells and print a portion of the worksheet (as you will see in the program code)

As was done with the form, the worksheet contains a Combo Box and List Box control for displaying the topic and word lists, respectively The data listed in the ActiveX controls on thepuzzle worksheet will be read only from the hidden worksheet and not from the data file.Nothing is included directly on the worksheet that will allow the user to edit the data file.Instead, a Command Button control with the caption Update Listsis used to show the formthat updates the data file Other Command Button controls added to the worksheet includethe following:

• Clear All:Clears data from the puzzle and ActiveX controls

• Refresh: Refreshes the topic and word lists This button is meant to be used after the user has updated the data using the form

List Box control

Merged cells Command Button controls

Trang 13

• Print Puzzle: Prints the puzzle.

• Pictures of arrows: Eight buttons for selecting the direction in which the programwrites the word in the puzzle

• Fill: Randomly fills the empty cells in the puzzle with uppercase letters

In addition to the ActiveX controls, several areas of the worksheet are reserved for the puzzle,the puzzle title, the word list, and help messages The puzzle grid is fifteen by fifteen cellsand is formatted with borders and color The range A1 : Q1 is merged and formatted with alight colored font so that the title of the puzzle (topic) is centered in the middle of the puzzle.The fifteen cells in the eight rows immediately below the puzzle are merged into three cellsper row This gives a total of twenty-four cells for holding the list of words that have beenadded to a puzzle Finally, an area of cells just to the right of the puzzle are merged and for-matted in a large font to hold error and help messages output by the program All of theseareas were assigned defined names (Puzzle, Topic, WordList, and Output) to make the codethat accesses these ranges easier to read

Program execution is meant to proceed as follows:

1 The user populates the Combo Box and List Box controls with topics and words byclicking the Refreshbutton

2 The user selects a topic from the Combo Box control and a new list of words is played in the List Box

dis-3 The user selects a word from the list in the List Box

4 The user selects a cell in the puzzle grid

5 The user clicks a button indicating the direction in which to write the word and theword is added to the puzzle

6 The user continues to add words to the puzzle until he or she is satisfied with thepuzzle

7 The user clicks the Fillbutton and the program fills empty cells in the puzzle grid

8 The user clicks the Print Puzzlebutton and the puzzle and word list is printed

Writing the Code for Word Find

As the interface for this program has two distinct parts, so will the code The form and sheet components can be written and tested as independent programs, then added together

work-to complete this project I will start with the code module for the UserForm object thatupdates the data file

Trang 14

Writing the Code for the Userform Module

The program’s data is stored in a random access file The advantage to this type of file is that

it can be quickly updated as long as your program correctly tracks the record number Inorder to do this, a custom data type is required to ensure each record uses the same amount

of memory The custom data type named PuzzleList is built from three elements: a longinteger and two strings The long integer is an identification number (IDNum), and as you willsee, I use it to make finding specific records easier The two strings will hold the topics andwords The integer variable recNumis still required to serve as a place locator for I/O operations

on a random access file The value of the recNumvariable will match that of the identificationnumber which makes it easier to locate and update records in the data file A variable array

of type PuzzleList is declared at module level to be used in reading the data from the fileand writing it out to the hidden worksheet

Option Explicit

Private recNum As Integer

Private Type PuzzleList

IDNum As Long

topic As String * 30

word As String * 15

End Type

Private curList() As PuzzleList

When the form is shown, its Activate()event procedure is triggered and its code calls theprocedures that load the data from the file and writes it to the hidden worksheet The datafile’s topics are retrieved from the worksheet by passing the dynamic array variable topicstothe GetUniqueTopics()sub procedure The name of the procedure, GetUniqueTopics(), impliesits function Remember that the data file, and thus the hidden worksheet, contains a topicfor every word; therefore numerous repeat values for the topic exist The array is passed byreference, so when it is re-dimensioned and filled with values in the GetUniqueTopics()subprocedure, it can be added to the Combo Box control via its Listproperty (the Listproperty

of the Combo Box control is a variant array) The last line of code in the Activate()event cedure sets the topic that will be displayed in the Combo Box control Be aware that settingthe Valueproperty of a Combo Box control triggers its Change()event

pro-Private Sub UserForm_Activate()

Dim topics() As String

‘—————————————————-‘Initialize worksheet and controls.

Trang 15

The purpose of the GetAllRecords()sub procedure is to load the data from the file and store

it in the module level variable array curList Because the procedure involves file I/O, somevalidation and error handling code is included

To avoid file I/O errors that may crash the program, first the path to the Workdfind.txtfilemust be validated and appropriate action taken if the file is not found The Dir()functionserves to validate the file path The Dir()function is a member of the FileSystemobject, butcan be used without its object qualifier In the GetAllRecords()sub procedure, the Dir()function returns the string “WordFind.txt”if this file is found at the specified path If thefile does not exist at the specified path then the Dir() function returns a zero-length string(“”) and the GetFile()function is called to display a file dialog and give the user a chance

to find and select the file to open If the user finds the file and selects OK, then its path isreturned to the variable filePath If the file’s path is not found and the user selects Cancel,then code execution continues

The second step in avoiding a program crash from a file I/O error is adding an error handler.Although it is difficult to foresee errors other than an invalid path and/or file name, cer-tainly more possibilities for file I/O errors exist (for example, a corrupt file); therefore, theerror handler is added to display a message box indicating the nature of the error and endthe program The error handler is also called if the file path is invalid and the user chooses

to cancel the file dialog used to find the file (this returns an empty string for the file path)

I handle the error this way because of the difficulty in predicting what error might occur.All I know is that the Openstatement has failed, so the program must end Most importantly,the error handler prevents the program from crashing and starting the debugger

Normally, I would place the call to the GetFile()sub procedure in the error handler, but theOpenstatement does not fail if a valid path is used and the file is not found at this location.Instead a new file is created and that’s not the required action

Private Sub GetAllRecords()

Dim filePath As String

Dim curRec As PuzzleList

Trang 16

‘—————————————————————————-‘Load all records from random access text file into

‘variable array of custom data type.

‘—————————————————————————-On Error GoTo FileIOError

filePath = ActiveWorkbook.Path & “\Wordfind.txt”

Open filePath For Random Access Read As #1 Len = Len(curRec)

Do While Not EOF(1)

Get #1, recNum, curRec

ReDim Preserve curList(recNum - 1)

curList(recNum - 1).IDNum = curRec.IDNum

curList(recNum - 1).word = curRec.word

curList(recNum - 1).topic = curRec.topic

MsgBox “The program has encountered an error trying to “ & _

“access the file Wordfind.txt “ & vbCrLf & Err.Description, _

Trang 17

vbCritical, “Error “ & Err.Number

End

End Sub

The GetFile()sub procedure is only called from the GetAllRecords()sub procedure when thedata file is not found at the specified path The procedure shows a FileDialogobject to allowthe user to search the computer’s file structure in order to locate the file If the user locatesthe file and clicks the OK button, then the file’s path is returned to the calling function Private Function GetFile() As String

Dim fileDiag As FileDialog

‘——————————————————-‘Configure and show the open dialog.

‘Return path to calling function.

‘——————————————————-Set fileDiag = Application.FileDialog(msoFileDialogFilePicker)

With fileDiag ‘Configure dialog box

.Filters.Clear

.Filters.Add Description:=”All files”, Extensions:=”*.*”

.Filters.Add Description:=”Text”, Extensions:=”*.txt”, Position:=1

.AllowMultiSelect = False

.FilterIndex = 1

.Title = “Select Wordfind.txt File”

.InitialFileName = “”

If Show = -1 Then ‘User clicked Open

GetFile = SelectedItems(1) ‘Return path to selected file

The last statement in the procedure sorts the data alphabetically, first by topic and then byword This is the major reason I write the data to the worksheet—to take advantage of its fastsorting capabilities so the data is listed alphabetically in the ActiveX controls Furthermore,

Trang 18

when the topics are sorted alphabetically, it’s easier to pick out the unique values from thelist Note that I passed the Sort()method of the Rangeobject several arguments They are alloptional, but at the very least, Key1and Key2must be included in order to specify the primaryand secondary keys on which to sort, which in this case, are the topic and word, respectively.

I also included the MatchCaseargument to specify a case-insensitive sort You can also pass theSort()method arguments that specify the sort order for each key (Order1, Order2), whether

or not to ignore a header row (Header), whether to sort by rows or columns (Orientation), andwhether or not to treat numbers as text for each key (DataOption1, DataOption2)

Excel worksheets are hidden and unhidden by selecting Format, Sheet, Hide/Unhide

in the application window

Private Sub WriteToWorksheet()

Dim lastRow As Integer

ws.Cells(I, “A”).Value = Trim(curList(I - 2).topic)

ws.Cells(I, “B”).Value = Trim(curList(I - 2).word)

ws.Cells(I, “C”).Value = Trim(curList(I - 2).IDNum)

Trang 19

ws.Range(“A2:C” & recNum).Sort Key1:=ws.Range(“A2”), Key2:=ws.Range(“B2”), _ MatchCase:=False

End Sub

When the user selects a new topic, the Change()event of the Combo box is triggered and theList Box is updated with the words associated with the selected topic This event is also trig-gered from the Activate()event of the UserFormobject when the Listproperty of the ComboBox is assigned the values in the variable array topics The words are added to the List Box

by the GetWords()sub procedure which reads the values from the hidden worksheet.Private Sub cmbTopics_Change()

‘Add word list to list box associated with

‘topic on combo box.

‘—————————————————————

lstWords.Clear

Set ws = Worksheets(“Lists”)

For I = 2 To ws.UsedRange.Rows.Count

If ws.Cells(I, “A”).Value = cmbTopics.Value Then

lstWords.AddItem ws.Cells(I, “B”).Value

Trang 20

Label control serves as a convenient location for storing the number of the record currentlydisplayed on the form The record number is required for updating the file so it can simply

be read from the Label control when the user selects the Update button If you like, you canset the Visibleproperty of the Label control to false to prevent the user from seeing therecord number Figure 7.18 shows an example of how the form appears when a word hasbeen selected from the List Box control

Private Sub lstWords_Click()

‘Loop through columns A and B in Lists worksheet to find

‘the correct topic and word and then return ID number.

‘————————————————————————————

Set ws = Worksheets(“Lists”)

For Each c2 In ws.Range(“A2:A” & ws.UsedRange.Rows.Count)

If c2.Value = cmbTopics.Value Then

For Each c1 In ws.Range(“B2:B” & ws.UsedRange.Rows.Count)

If c1.Value = lstWords.Text Then GetIDNum = ws.Range(“C” & c1.Row).Value Exit Function

End If

Figure 7.18

The update form

from the Word

Find program

displaying a

user-selection.

Trang 21

Private Sub cmdAddNew_Click()

‘—————————————————————

‘If nothing in text boxes then exit the sub.

‘—————————————————————

If txtWord.Text = “” Or txtTopic.Text = “” Then

MsgBox “You must enter a topic and word before updating the list.”, _

vbOKOnly, “No Entry”

txtWord.SetFocus

Exit Sub

End If

‘———————————————————————————

‘Add the new record to the Lists worksheet, the file,

‘the List box, and the combo box.

Trang 22

‘Update the “Lists” worksheet with the new record.

‘—————————————————————————

Set ws = Worksheets(“Lists”)

ws.Cells(recNum + 1, “A”).Value = txtTopic.Text

ws.Cells(recNum + 1, “B”).Value = txtWord.Text

ws.Cells(recNum + 1, “C”).Value = recNum

ws.Range(“A2:C” & recNum + 1).Sort Key1:=ws.Range(“A2”), _

‘——————————————————-‘Update the controls on the Userform.

‘Update topic only if its new.

‘——————————————————-lblIDNum.Caption = recNum

lstWords.AddItem txtWord.Text

For I = 0 To cmbTopics.ListCount - 1

If cmbTopics.List(I) = txtTopic.Text Then

Exit Sub ‘The topic is not new, so exit sub.

End If

Next I

cmbTopics.AddItem txtTopic.Text

End Sub

Private Sub AddToFile()

Dim filePath As String

Dim curRec As PuzzleList

On Error GoTo FileIOError

Trang 23

‘——————————-‘Test for valid path.

‘——————————-filePath = ActiveWorkbook.Path & “\Wordfind.txt”

If Dir(filePath) <> “Wordfind.txt” Then

‘—————————————————————————-Open filePath For Random Access Write As #1 Len = Len(curRec)

Put #1, recNum, curRec

Dim I As Integer

Trang 24

Dim validTopic As Boolean

If Not validTopic Then

MsgBox “You must use a current topic before updating a record.”, _ vbOKOnly, “No Valid Topic”

Exit Sub

End If

‘———————————————————————————

‘Update record in worksheet, controls, and text file.

‘Only allow updates to the word and not the topic.

Trang 25

Private Sub UpdateFile()

Dim filePath As String

Dim curRec As PuzzleList

On Error GoTo FileIOError

filePath = ActiveWorkbook.Path & “\Wordfind.txt”

Open filePath For Random Access Write As #1 Len = Len(curRec)

Put #1, Val(lblIDNum.Caption), curRec

Ngày đăng: 12/08/2014, 16:21

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w