Solved

I cant write to the file

Posted on 2001-06-15
7
141 Views
Last Modified: 2010-05-02
Hi folks, my cmdEnter code used to write to the file very well, and then I did some testing and code changing in the application and now it wont write.

Am I losing it or what.

Private Sub cmdEnter_Click()
    Const conMsg As String = "Account numbers range from 1 to 30."
    Const conBtns As Integer = vbOKOnly + vbInformation _
                + vbDefaultButton1 + vbApplicationModal
    Dim udtCredit As CreditStruc
    Dim strFilename As String, i As Integer
    Dim intUserResponse As Integer, recordLength As Long
    Dim intAccNum As Integer, x As Integer
   
        On Error GoTo errHandler

        intAccNum = Val(mskAccNum.Text)
        dlgCom.Filename = strFilename
        strFilename = "A:\Data.dat"
        recordLength = Len(udtCredit)


    If intAccNum < 0 And intAccNum > 30 Then
        intUserResponse = MsgBox(conMsg, conBtns, "Unlimited Credit")
    End If
        If mskAccNum.Text = "" Then
            MsgBox "You must enter an Account Number before saving this Record.", conBtns, "Validation Error"
        Else

            Open "A:\Data.dat" For Random As #1 Len = recordLength
                udtCredit.intAccNum = Val(mskAccNum.Text)
                udtCredit.dteBirth = mskBirth.Text
                udtCredit.strLast = txtLast.Text
                udtCredit.strFirst = txtFirst.Text
                udtCredit.curBalance = Val(mskBalance.Text)
                udtCredit.strTelNum = mskTelNum.Text
                udtCredit.intIDNum = Val(txtIDNum.Text)
                lblDate1.Caption = Date
               
            For x = 1 To 30
                Put #1, udtCredit.intAccNum, udtCredit
            Next
        End If
 
   
    Close #1
    Call ClearFields
errHandler:

        mskAccNum.SelStart = 0
        mskAccNum.SelLength = Len(mskAccNum.Text)
        mskAccNum.SetFocus

End Sub

I have tried commenting out the For Loop and moving it but I cant seem to get it to write.
It will create a new file if I delete the old one but it still wont write to it.
Many thanks for any help.  
0
Comment
Question by:Ned_Kelly
  • 3
  • 2
  • 2
7 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 6195954
I have seen several issues:
you write Put #1, udtCredit.intAccNum, udtCredit
but defined the file to have recordlength of len(udtCredit)

You have
If mskAccNum.Text = "" Then
  ...
Else
  Open "A:\Data.dat" ...
  ...
End If
Close #1

And it must be  
If mskAccNum.Text = "" Then
  ...
Else
  Open "A:\Data.dat" ...
  ...
  Close #1
End If

Hope this helps

           
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6195967
But why are you writing 30 times to the same account number?

and:

If intAccNum < 0 And intAccNum > 30 Then

will never occur because a number can't be both < 0 and > 30 at the same time.

You do have your old code to compare it against, right?  Post it so we can compare.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6196044
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:Ned_Kelly
ID: 6197724
This cmdEnter is from the frmCredit whereas the other question link(angellll) is for the cmdEnter on the frmTrans.
The original code has been overwritten as I resave the floppy to the main drive, it will be a practice that I intend to take up, keeping a copy of the working code.   I cant see the reason why it isn't working though.
I have worked out why it wont write, I changed the AllowPrompt in the properties box for the MaskEditBoxes from true to false.  The reason I did this was so that when I type in an account number and press find it will bring up the acount details.  Before I did the change it would not return the values for the MaskEditBoxes, but it does when you change it to false.
I have a problem

Thanks for your comments and help for a beginner.
Ned
0
 

Author Comment

by:Ned_Kelly
ID: 6198130
Hi, the line

If intAccNum < 0 And intAccNum > 30 Then

is for checking to see if the number falls between 1 and 30 as that is the range of records for the application, as it is a school project only.
If it is out of this range then the msgbox comes into play.
I'm going to accept angellll's answer as I need to ask another question.
Ned
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6203234
"see if the number falls between 1 and 30"

The proper syntax should be
1) <= 0, OR:

If intAccNum <= 0 OR intAccNum > 30 Then
  ' out of range

2) >0, <=30:

If intAccNum > 0 And intAccNum <= 30 Then
   ' in range
else
   ' out of range
end if
0
 

Author Comment

by:Ned_Kelly
ID: 6203314
Yes you are right, I can only go by what we are taught until we can then start to think for ourselves.
My Vb instructor uses the sink or swim method, ie
you go in to class and he starts by taking you through the tutorial, at the same time you are frantically typing to keep up with him, then you are left to fixed any errors of which they are almost always there.
You then go home and start on the project for the week, come back the next week and do it all over again, meanwhile the projects are getting harder.
You do this for ten weeks and that was the end of the first module, of which I passed.
The next six week module was a browser and then the Credit app of which I now ask questions, we have to hand it in so that is works well with no errors or we fail.

It is worth 80% of the module, I fail tomorrow night, But I know I tried my best, so I can only go and practice some more and resit the module.
But I haven't give up yet.

Thanks for you rcomment RspahitZ,
I will adjust my code accordingly thankyou.
Ned
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now