DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Visual Basic 4 / 5 / 6 (http://www.daniweb.com/forums/forum4.html)
-   -   Forms in Random access files (http://www.daniweb.com/forums/thread1794.html)

c_barnett01 Feb 2nd, 2004 7:45 am
Re: Forms in Random access files
 
thanks, for helping me, now 2nd question how do i put all my files into a list box, that when clicked on can show another form. also how do you add a loop so that when i click a button it sends my information i have entered to a word doc in notepad. i can only make it output one line of info and i wish for it to keep all the data i enter. thanks

c_barnett01 Feb 6th, 2004 6:34 am
Re: Forms in Random access files
 
this is my code whats wrong with it:


Private Type myrec
name As String * 10
numbr As Integer
End Type
Dim position As Integer
Private Sub add_Click()
Dim rec As myrec
rec.name = tname.Text
rec.numbr = tnumber.Text
Open "i:\records.dat" For Random As #1 Len = Len(rec)
Put #1, position + 1, rec
Close #1
position = position + 1
tname.Text = ""
tnumber.Text = ""
End Sub
Private Sub Command1_Click()
Dim rec As myrec
Dim recnumber As Integer
recnumber = txtfindrecno
If (recnumber > 0) And (recnumber <= position) Then
Open "i:\records.dat" For Random As #1 Len = Len(rec)
Get #1, recnumber, rec
tname = rec.name
tnumber = rec.numbr
Close #1
Else
MsgBox "invalid record number"
End If
End Sub
Private Sub Command2_Click()
Dim rec As myrec
Dim recname As String
Dim found As Boolean
Dim recordnumber As Integer
recordnumber = 0
found = False

Open "i:\records.dat" For Random As #1 Len = Len(rec)
Do While (Not EOF(1)) And (found = False)
recordnumber = recordnumber + 1
Get #1, recordnumber, rec
If rec.name = txtfindrecname.Text Then
found = True
tnumber = rec.numbr
tname = rec.name
End If
Loop
Close #1
If Not found Then
MsgBox "name " & txtfindrecname & " is not in file"
End If

End Sub
Private Sub Form_Load()
Dim rec As myrec
Open "c:\fiona.dat" For Random As #1 Len = Len(rec)
position = LOF(1) / Len(rec)
Close #1
End Sub
Private Sub recame_Change()
End Sub
Private Sub retrieve_Click()
Dim rec As myrec
Dim found As Boolean
Dim datatodisplay As String
Dim index As Integer
Open "c:\fiona.dat" For Random As #1 Len = Len(rec)
For index = 1 To position
Get #1, , rec
tname.Text = rec.name
tnumber.Text = rec.numbr
datatodisplay = rec.name & " " & rec.numbr
List1.AddItem datatodisplay
Next index
Close #1
End Sub

Paladine Feb 7th, 2004 5:32 pm
Re: Forms in Random access files
 
Ok a couple of things. 1. There is some logic to the code I a not understanding, so maybe you could give me a explaination on what is suppose to be happening.
2. To make your code easier to follow and read, for yourself and others, you should use hungarian notation and name your buttons appropriately.

i.e. lstRecords (for a list box with Records), cmdAdd or btnAdd (for a button that adds to something), strName (for a string variable called "name"), intNumber (for an integer variable), etc.

Just a pointer. No harm done!

Saying that, what part of your program is not working? It finds the records, it adds records, and it retrieves records. I mean, I didn't test it in everyway... but give us more details other that "something is wrong", which tells us very little.

Later

ption Explicit

Private Type myrec  '  USER DATA TYPE
    strName As String * 10
    intNumbr As Integer
End Type

Private intPosition As Integer

Private Sub add_Click()
    Dim rec As myrec
    rec.strName = tname.Text  '  Retrieves value from Textbox
    rec.intNumbr = tnumber.Text
    Open "c:\records.dat" For Random As #1 Len = Len(rec)
    MsgBox intPosition
    Put #1, intPosition + 1, rec
    Close #1
    intPosition = intPosition + 1
    tname.Text = ""
    tnumber.Text = ""
End Sub
Private Sub Command1_Click()
    Dim rec As myrec
    Dim recnumber As Integer
    recnumber = txtfindrecno
    If (recnumber > 0) And (recnumber <= intPosition) Then
        Open "c:\records.dat" For Random As #1 Len = Len(rec)
        Get #1, recnumber, rec
        tname = rec.strName
        tnumber = rec.intNumbr
        Close #1
    Else
        MsgBox "invalid record number"
    End If
End Sub
Private Sub Command2_Click()
    Dim rec As myrec
    Dim strRecname As String    '  This is for what?
    Dim fFound As Boolean
    Dim intRecordnumber As Integer
    intRecordnumber = 0
    fFound = False
    MsgBox txtfindrecname.Text
    Open "c:\records.dat" For Random As #1 Len = Len(rec)
    Do While (Not EOF(1)) And (fFound = False)
        intRecordnumber = intRecordnumber + 1
        Get #1, intRecordnumber, rec
        If CStr(rec.strName) = CStr(txtfindrecname.Text) Then
            fFound = True
            tnumber = rec.intNumbr
            tname = rec.strName
        End If
    Loop
    Close #1
    If Not found Then
        MsgBox "name " & txtfindrecname & " is not in file"
    End If

End Sub
Private Sub Form_Load()
    Dim rec As myrec
    Open "c:\fiona.dat" For Random As #1 Len = Len(rec)
    intPosition = LOF(1) / Len(rec)
    Close #1
End Sub
Private Sub recame_Change()
End Sub
Private Sub retrieve_Click()
    Dim rec As myrec
    Dim fFound As Boolean  '  What is this for?
    Dim strDatatodisplay As String
    Dim intIndex As Integer
    Open "c:\fiona.dat" For Random As #1 Len = Len(rec)
    For intIndex = 1 To intPosition
        Get #1, , rec
        tname.Text = rec.strName
        tnumber.Text = rec.intNumbr
        strDatatodisplay = rec.name & " " & rec.intNumbr
        List1.AddItem strDatatodisplay
    Next intIndex
    Close #1
End Sub

chexwebson Oct 19th, 2004 4:46 pm
Re: Forms in Random access files
 
This may need to be a new thread, and seems I am responding after quite some time, if needed, please move this where it needs to go. I am looking for even more info on the Random access files...your description was good, however, there are three different types, including binary, and then it is my understanding that you can assign block size and even encryption before placing into the Random access file? Could you possible describe more about how this is done, or point me in the direction for more information? I am using Visual Studio.Net 2003u sing vb.net for the programming language. Thanks in advance.

cd

nisha Jan 19th, 2005 10:59 am
Re: Forms in Random access files
 
hello
i want a simple demo to understand how to create random file access in vb.
pls help me.

jesan May 27th, 2008 10:47 pm
Re: Forms in Random access files
 
Hi , i am a newbie as you can tell and i was wondering if anyone could answer this and maybe ( by luck) make a demo or something

But heres the question :

AS you know i am using a random Access File (RAF), and these files carry Students names,age and absences. So I thought that why not that everytime you open a RAF that the students will be in a list. Also suppose I click on a name in the list, i want that person's data to show.
Example: Suppose I open a RAF : Ms.Cooks Class. Then the program will list everyone in her class. Suppose there is a student name Jeson . And If i click on him all his data would come out.

By the way, i am very new to Visual Basic 6, i am doing a course on this and ya we are still noobs in my class. So far though i know the very basics of these things like Putting, Creating and Reading RAFs. Thanks for anyone willing to help


All times are GMT -4. The time now is 10:57 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC