|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
flatfile database using vb
hi,
I am doing a project on maintaining the contact details of the employees in a text file. I am using Visual Basic for this. I am a bigginer in visual basic. The thing I have to do is 1. entering data into the text file. 2. retriving the entire data from the text file. 3. editing a part of the data, like editing the contact details of a manager. 4. Saving back to the textfile any changes done to the edited data I need to enter the data into the text file in a user friendly format through textboxes. and also i should edit the data of a particular employee in an user friendly format. like editing the contact details of the manager in the text boxes, textbox1 for name, textbox2 for phone number...etc. Please help me in suggesting how to approach and also if you have done it earlier, please send a copy of the code and the procedure you did that. |
|
#2
|
||||
|
||||
|
Is this for a class project?
__________________
Fisherman "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein |
|
#3
|
|||
|
|||
|
no its a job project
hi,
this is not a course project , its my job project , which i am currently working. if you have some information, please help me. bye, |
|
#4
|
|||
|
|||
|
Why don't you use a backend database? If your text file gets to any size it won't be too fast and will be difficult to update.
|
|
#5
|
|||
|
|||
|
the project needs text file as backend
hi,
my poject needs a text file as a backend. So it was a customer specificatio. So i need to use text file only. bye |
|
#6
|
||||
|
||||
|
Ok.. one more question - I'm guessing that you will have one text file with multiple lines, each line representing a different customer? If so, will each field be separated by commas, semicolons, how are you planning on delimiting the fields in the "records"
|
|
#7
|
|||
|
|||
|
abt the delimiters
hi,
I am going to use only one text file to store all the customer details. And one line for each customer. And I should extract each customers data into textboxes. Like one text box for name , one for phone number, one for address and so on. And also I should save back any changes done to any feild in any text boxes to same text file at the same place from where the data has been extracted. And also I should allow save the new data of the new customers in that field. Thanks bye, naveen |
|
#8
|
||||
|
||||
|
Well, there are a few ways to do this... the simplest way is to do something like this...
Code:
Dim Name As String, EmpType As String, Address As String, Pay As String, EmpClass As String
Open App.Path & "/Description.txt" For Input As #1
Input #1, Name, EmpType, Address, Pay, EmpClass
Close #1
the important thing to remember is that your text file has to have the member data in the exact order you want it read in - remember, there are no field names here, and no manager to recognize them! You can do the above code by also using several arrays of strings and loading the data in a loop by using the Do Until EOF(1)...Loop structure. The way I would handle it, though, would be to declare either a class or a UserDefined type called employee, and then dim an array of that type/class. Personally, I would handle all manipulation of the data in the array, and the use the index of the array to determine what line you need to modify in the text file... Code:
Private Type Employee
Name As String
EmpType As String
Address As String
Pay As String
EmpClass As String
End Type
Private Sub Form_Load()
Dim EmployeeDetail() As Employee
Dim i As Integer
Open App.Path & "/Description.txt" For Input As #1
Do Until EOF(1) = True
ReDim Preserve EmployeeDetail(i + 1)
Input #1, EmployeeDetail(i).Name, EmployeeDetail(i).EmpType, EmployeeDetail(i).Address, EmployeeDetail(i).Pay, EmployeeDetail(i).EmpClass
i = i + 1
Loop
Close #1
i = 0
Debug.Print EmployeeDetail(i).Name & " " & EmployeeDetail(i).EmpType & " " & EmployeeDetail(i).Address
End Sub
This is just a simple program where I have a file called "Description.txt" with comma-delimited strings as the records. I'm using a loop to load the data. One thing you will want to do - if you load records (check to make sure), then you will want to redim preserve the array after the loop to (i-1), otherwise, you'll have one more array element than you will need Good Luck |
|
#9
|
|||
|
|||
|
hi,
Thanks a lot, but the code you gave is not working, please check it once. Implemented it as ----------------------------------- Private Type Employee Name As String EmpType As String Address As String Pay As String EmpClass As String End Type ---------------------------------------------------------------- Private Sub Form_Load() Dim EmployeeDetail() As Employee Dim i As Integer Open "c:\Description.txt" For Input As #1 Do Until EOF(1) = True ReDim Preserve EmployeeDetail(i + 1) Input #1, EmployeeDetail(i).Name, EmployeeDetail(i).EmpType, EmployeeDetail(i).Address, EmployeeDetail(i).Pay, EmployeeDetail(i).EmpClass i = i + 1 Loop Close #1 i = 0 Debug.Print EmployeeDetail(i).Name & " " & EmployeeDetail(i).EmpType & " " & EmployeeDetail(i).Address End Sub ------------------------------------------------------------------------------------ Private Sub Command1_Click() Dim Name As String, EmpType As String, Address As String, Pay As String, EmpClass As String Open "c:\Description.txt" For Input As #1 Input #1, Name, EmpType, Address, Pay, EmpClass Close #1 End Sub ------------------------------------------------------------------------------------ Private Sub Command2_Click() End End Sub ------------------------------------------------------------------------------------ please check this.. bye, naveen |
|
#10
|
||||
|
||||
|
ok, well, what part isn't working? Is the program printing the record out in the immediate window? Also, what are you trying to accomplish in Command1_click? Is that where you're wanting to update the file?
|
|
#11
|
|||
|
|||
|
its not printing
hi,
the program is not printing in the immediate window. And in Command_Click() I want to update the file. plz check that. ![]() |
|
#12
|
||||
|
||||
|
if you are wanting to update your file in Command1_Click, then you want to update that file using the Open "Filename.txt" for APPEND as #1/2/3 - a word on that subject, if your file dubbed #1 is still open, then your next instance of that file must use a different IO number (ie, incrementing that value by 1,5, whatever you want, just so that you don't have two open instances using the same alias at the same time. One more thing you will want to know, there are three main modes for opening a file - input, output, and append. Input reads data into your app, output will write to the file and over any existing data, append will write to the file leaving current data intact. For your app, I would recommend that you modify your data inside your array, use the value of the array index (counter variable?) to determine which line you want to modify in your table, then open the the file for input, loop through the records until you reach the one you want to modify. Then, move one past that record, and capture all records after that into a second array (or extend/redim your existing array and capture all values into it). Now close your input and open another instance for append - move to the record to change and rewrite it and everything after it.
After all that I should say this - if you will not a lot of records in a table (more than 5-10,000), then you can just manipulate your data in the array, and open the file for OUTPUT - this will rewrite everything in the file, including existing data. A word of caution about this - if more than one instance of your app will be open at one time, then this could cause problems when two people try to update at once. You might find a way to lock the file once it's been opened for output. this is why Database Management Systems are so nice! ![]() |
|
#13
|
|||
|
|||
|
Private Type Employee
Name As String EmpType As String Address As String Pay As String EmpClass As String End Type Private Sub Form_Load() Dim EmployeeDetail() As Employee Dim i As Integer Open "c:\Description.txt" For Input As #1 'i = 0 Do Until EOF(1) = True ReDim Preserve EmployeeDetail(i + 1) Input #1, EmployeeDetail(i).Name, EmployeeDetail(i).EmpType, EmployeeDetail(i).Address, EmployeeDetail(i).Pay, EmployeeDetail(i).EmpClass i = i + 1 Loop Close #1 i = 0 'Debug.Print EmployeeDetail(i).Name & " " & EmployeeDetail(i).EmpType & " " & EmployeeDetail(i).Address MsgBox (EmployeeDetail(i).Name & " " & EmployeeDetail(i).EmpType & " " & EmployeeDetail(i).Address) End Sub Private Sub Command1_Click() Dim Name As String, EmpType As String, Address As String, Pay As String, EmpClass As String Open "c:\Description.txt" For Append As #2 'Write #2, Name, EmpType, Address, Pay, EmpClass Print #2, Text1.Text Close #2 End Sub Private Sub Command2_Click() End End Sub This is the code you I am trying but I am getting an error "INPUT PAST END OF FILE" in the Private sub Form_Load(). And at the line "Input #1, EmployeeDetail(i).Name, EmployeeDetail(i).EmpType, EmployeeDetail(i).Address, EmployeeDetail(i).Pay, EmployeeDetail(i).EmpClass " please check this out bye ![]() |
|
#14
|
||||
|
||||
|
usually you get this error when your program has reached the end of the file, and is expecting to read another value. The cause of this is typically that somewh |