Welcome to Dream.In.Code
Become a VB Expert!

Join 149,928 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,803 people online right now. Registration is fast and FREE... Join Now!




Random file access help please

 
Reply to this topicStart new topic

Random file access help please

dee2020
15 Jan, 2008 - 06:05 AM
Post #1

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 8

Hello friendz

im working on "feedback form analysis" project
the first main aim of my project is to store data submitted by the user from the feedback form in a file which can be accessed randomly. For that.... i wrote the following code:

CODE
Option Explicit

Private Type feedbackdata
    Laboratory(0 To 4) As byte
    Library(0 To 4) As byte
    Teaching(0 To 4) As byte
End Type

Dim recordnum As Long

Private Sub cmdSubmit_click()
Dim feedback As feedbackdata, Index As Integer

    Open "C:\student.txt" For Random As #1 Len = Len(feedback)
    
        With feedback
            For Index = 0 To 4
                .Laboratory(Index) = Abs(OptLaboratory(Index).Value)
                .Library(Index) = Abs(OptLibrary(Index).Value)
                .Teaching(Index) = Abs(OptTeaching(Index).Value)
            Next Index
        End With
        recordnum = recordnum + 1
        Put #1, recordnum, feedback
    Close #1
    cmdReset_click
End Sub

Private Sub cmdReset_click()
Dim Index As Integer
    For Index = 0 To 4
        OptLaboratory(Index).Value = False
        OptLibrary(Index).Value = False
        OptTeaching(Index).Value = False
    Next
End Sub


Basically my form consists of 10 questions and 5 option buttons for each question. If an option is selected for a particular question, 1 should be written in the file and if option is not selected a 0 should be written.

For eg., for a question suppose 2nd option was selected and was submitted...the compiler should write the following in the file

QUOTE
0 1 0 0 0


The compiler should be able to write the result for every question in terms of 0's and 1's in a new line.
There should be a marker to indicate the start and end of form in the file

could u plz help me...??

This post has been edited by dee2020: 15 Jan, 2008 - 03:44 PM


Attached File(s)
Attached File  dee.doc ( 30k ) Number of downloads: 48
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Random File Access Help Please
16 Jan, 2008 - 12:34 AM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
It looks like you are trying to make a text file. OK, your problem is that you are printing to the file in binary mode, when you want to do it in text mode.

To do that you should change your code to this:
CODE

    Open "C:\student.txt" For Append As #1  'Appends to whatever is
                               'already in the file
    
        With feedback
            For Index = 0 To 4
                Print #1, Format(Abs(OptLaboratory(Index).Value)) + " ";
            Next Index
            Print #1   'new line

            For Index = 0 To 4
                Print #1, Format(Abs(OptLibrary(Index).Value)) + " ";
            Next Index
            Print #1

            For Index = 0 To 4
                Print #1, Format(Abs(OptTeaching(Index).Value)) + " ";
            Next Index
            Print #1
        End With

    Close #1


The above code will not populate your data structure

For interest, why do you want to store it like that? If it is going to be opened by another VB program written by you, then the original way is fine.

This post has been edited by Nayana: 16 Jan, 2008 - 12:35 AM
User is offlineProfile CardPM
+Quote Post

dee2020
RE: Random File Access Help Please
17 Jan, 2008 - 05:04 AM
Post #3

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 8

thank you nayana for ur reply.....but you have used sequential access for the file.......actually i want to pick the records in the file randomly using the RANDOM access.....Can you help me to do this...??

why i want it to store like that.......bcoz it is the proposed file format for my assignment
User is offlineProfile CardPM
+Quote Post

LookNAO
RE: Random File Access Help Please
17 Jan, 2008 - 11:10 AM
Post #4

D.I.C Head
**

Joined: 28 Dec, 2007
Posts: 66



Thanked: 1 times
My Contributions
QUOTE(dee2020 @ 17 Jan, 2008 - 06:04 AM) *

thank you nayana for ur reply.....but you have used sequential access for the file.......actually i want to pick the records in the file randomly using the RANDOM access.....Can you help me to do this...??

why i want it to store like that.......bcoz it is the proposed file format for my assignment



You must have MSDN installed:

Type open, highlight open and hit F1...
go to the example....
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Random File Access Help Please
17 Jan, 2008 - 05:15 PM
Post #5

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
OK, that's strange. If we're storing things in a text file, and then using markers to separate records, then you can't do that with random file access, you have to use sequential access to read the records one by one.

To use random access, you need to know the size of each record, and storing it as binary is fine.

<sermon style=preach>
It seems strange to me that a University or Technical Institute would teach you programming with VB6. You should find a better place to study (IMHO). One reason: with VB6, trying to do multi-lingual things (which are increasingly important in todays world) is a nightmare.

And in my opinion, if they teach C++, they should teach you to use Unicode strings from the get-go.

</sermon>

But I'm assuming that because you've been told to use a text file, you want to be able to easily load the thing into Excel. Well, there are several solutions. You could
  1. Continue using a text file, and write block start/end stuff yourself. You will not have the benefit of get/set to a record number, but you can write the code yourself if you know the exact size of every record. In fact, you don't even need record separators.
  2. Change to a binary format. Then get/set will work fine.
  3. Use normal sequential operation. To do random access, just read the entire file into memory (which computers these days have tonnes of) and just do in-memory random access.

I personally would choose option 3. So make a choice, try it out, and if you have any problems I will help you out some more.

Oh, and do read the documentation. If you don't have MSDN installed you can access it on the Internet at
http://msdn2.microsoft.com/en-us/library/a...032(VS.60).aspx

Although it helps with a lot, it doesn't really teach you how to use things like the Open statement all that well. But at least give it a read.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 03:14PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month