Okay, I'm totally new to this. The only thing I know is how to store something in a dat file by opening a file, using put then closing it.
I'm storing multiple records in a file. Each record has about 6 fields. I also want to be able to display idividual fields in a particular record into individual textboxes, how do I go about doing this?
eg Customer ID, Customer Name, Address, Phone Number etc stored in a dat file, using put #filenumber, , customerID etc.
I now want to display this information in textboxes, txtCustomerID, txtCustomerName etc.
Thank you.
13 6934
Simple - just use Get the same way you did the Put.
Try this sample code. -
Dim v As String
-
Open "E:\DEBASIS\DATA.DAT" For Binary As #1
-
Get #1, , v
-
Text2.Text = v
-
Close #1
-
I'd recommend explicitly telling VB what you want, rather than relying on defaults. In other words... -
Open "E:\DEBASIS\DATA.DAT" For Binary Access Read Shared As #1
-
Hm... you know, I think we may have a problem here. I'm pretty sure that you have to indicate how many bytes to read when you use Get. In other words, if you wanted to read 5 characters you would do something like this... -
Dim v As String
-
Open "E:\DEBASIS\DATA.DAT" For Binary Access Read Shared As #1
-
v = Space$(5)
-
Get #1, , v ' Amount read depends on length of v.
-
Text2.Text = v
-
Close #1
-
For many data types such as Integer, Long etc, this wouldn't matter because they are a specific size. But with a string, I don't know what you'll do. You might need to store the length first. Or instead of using binary format, use Print # or Write # to write in text format, then use Input # or Line Input # to read it back after opening for Input rather than Binary.
Okay I managed to display each individual field, but I had to first declare each field type with it's size, in a seperate module. - Public Type CustomerRecord
-
CustomerID As String * 6
-
FullName As String * 20
-
etc
-
End Type
I now have no problem's with it, thank you ^_^
..but I know have another question. Whats the code that lets me delete a certain record from a dat file?
Ah, that's not as simple a question as you might think. Changing the data in a file is perfectly simple in Binary mode, as long as the length of the data doesn't change.
To remove part of a file, you basically need to rewrite everything from that point on, and truncate (shorten) the file. Though there are ways to do that, it's basically risky because if anything goes wrong part-way through, you could totally ruin the file. Plus, I'm not sure how you go about truncating the file (though I'm fairly sure it's possible).
The more commonly-used technique is to write a copy of the file without the unwanted piece, then delete the original file and rename the new one. This is how many archiving utilities work, for example (Eg. WinZIP).
Of course, it's also possible to "logically delete" a record. That is, make some change to it so that it is ignored in future. Thus, while physically it is still there, you can consider it as having been deleted. This is actually how a lot of database programs "delete" records, which is why the database keeps getting larger and has to be "compacted" from time to time to remove the old junk.
If you try playing with the search box up top, you'll find this topic has been covered at least once or twice.
Alright! Thank you very much for your time, you've really helped me out a lot.
I did know about logically deleting a record, but I didn't want the files to keep increasing in size over a long period of time.
I think I'll take a look at how to copy to a new file and rename.
Thanks again!
Okay I'm having some trouble with this. - Private Sub cmdDeletCustomer_Click()
-
Dim FileName As String
-
Dim NewFileName As String
-
Open "c:\records.dat" For Random As #1 Len = Len(OneCustomer)
-
Open "c:\tempor.dat" For Random As #2 Len = Len(OneCustomer)
-
-
-
-
For loopindex = 1 To LOF(1) / Len(OneCustomer)
-
If loopindex <> (cboSelectCustomerD.ListIndex + 1) Then
-
Get #1, loopindex, OneCustomer
-
Put #2, loopindex, OneCustomer
-
Else: End If
-
Next loopindex
-
-
Close #1
-
Close #2
-
-
Kill "C:\records.dat"
-
FileName = "C:\tempor.dat"
-
NewFileName = "C:\records.dat"
-
Name FileName As NewFileName
-
-
Call UpdateCbo
-
End Sub
cboSelectCustomerD basically has a list of all the customer names, so I can select a name from the list and choose to delete it.
The code works perfectly when I choose to delete the last record in the file.
The problem arises when I select a record other than the last. It copies all the records I need to the new file, but there's also a gap where the old unwanted record used to be. It's because it's writing to the record number "loopindex". I want the code to write over the gap after it has missed out the record I don't want, I tried several different approaches but I still couldnt get it to work.
Please help =s
.........
Anyone? =<
Sorry, it can take a while before anyone has time to look at questions here. We're just volunteering our own time, after all.
It's Monday morning here. I'll try to have a look at this at lunchtime.
I did have a quick glance at the code, and saw what looks like a logic bug. I think in the FOR loop, you need to do the Get every time 'round, and only the Put should be dependent on the If test.
Also, I think (not positive about this) that you should leave out the record number parameter in the Get and Put. In other words, Put #2, , OneCustomer. I've used binary mode a fair bit, but not random mode. So I'm not certain this is the right way to go about it.
See whether this makes any sense, and I'll try to get back to it at lunchtime.
I finally had a chance to read what you actually said about the code, and it seems I was correct. The Get/Put logic is where the problem lies. You need to read all records, but only write out the wanted ones.
Leaving out the record number on the Put should do the trick, if this syntax is valid for random mode. I know it is for binary, but still not sure about random.
Otherwise, just use two counters, one for read position and one for write position. They will both be incremented by one each time around the loop, except that the write counter won't increment when you skip a record.
If performance becomes an issue (in other words, if it's too slow), then let me know. I believe we can make this process much faster with a fairly simple fix, using binary mode instead of random.
I finally had a chance to read what you actually said about the code, and it seems I was correct. The Get/Put logic is where the problem lies. You need to read all records, but only write out the wanted ones.
Leaving out the record number on the Put should do the trick, if this syntax is valid for random mode. I know it is for binary, but still not sure about random.
Otherwise, just use two counters, one for read position and one for write position. They will both be incremented by one each time around the loop, except that the write counter won't increment when you skip a record.
If performance becomes an issue (in other words, if it's too slow), then let me know. I believe we can make this process much faster with a fairly simple fix, using binary mode instead of random.
I know you're using your own time to help others, i'm sorry for bumping the thread like that! It's just I don't have that much time to complete this, I really want to get it done.
I don't think performance would be much of an issue, I'm not going to have that much data stored on my file anyway.
You have solved the problem for me, I really can't thank you enough!! You should really have a rep system for this forum :)
Thanks again!!
Hey, glad we could help. :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Moosebumps |
last post by:
So say I am working on two separate .py files in IDLE that are part of the
same program. Does anyone have problems where when you modify one file, and
then run the other, the changes you made in...
|
by: Jerry |
last post by:
I'm having just a bit of trouble wrapping my brain around the task of
working with folders that are above the site's root folder.
I let users upload photos (.jpg/.gif files) which can...
|
by: Mark B |
last post by:
Our webhost (www.usbusinessweb.net) had a W2K IIS5 server crash after a
scheduled hard-boot occurred during a ms-security patch install overnight.
They couldn't get the server working again so they...
|
by: Matt Frame |
last post by:
I have a client that has asked us to get a digital signature certificate and
start digitally signing all files we pass between each other. I have heard
of the subject and know about the certs but...
|
by: Axter |
last post by:
Sorry for OT question, but does any one have a working *.bat file to
get Comeau to work with VC++ 7.1, VC++ 8.0, or GNU 3.x compilers.
I've tried everything I can think off, to get it to work,...
|
by: saturday |
last post by:
I was having problems getting mysql to work with php on my apache
server. Thankfully I got it working after reading a different thread,
which had me copy over the libmysql file to the apache/bin...
|
by: George2 |
last post by:
Hello everyone,
From the definition of working set, it is a subset of virtual pages resident in physical memory -- from book Windows Internals. It means working set could not be larger than...
|
by: =?Utf-8?B?SmVycnkgQw==?= |
last post by:
I have a machine running IIS 6.0. I just replaced the web.config and several
aspx pages in the application and now the style sheets are not working. the
images from the themes work but not the css...
|
by: Seth Williams |
last post by:
I have done some new development on older files, locally - then I copy the
files, along with the .vb files for the webservices, to our DEV server.
Now, mysteriously, no web services are running - I...
|
by: josequinonesii |
last post by:
I've searched, I've read, I've tested and re-read numerous post but to no avail yet...
Quite simply, the settings I've applied to my httpd.conf, httpd-vhost.conf and my hosts files simply does not...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |