CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 2011
    Posts
    13

    [RESOLVED] Random Access File New Line

    I'm trying to use Random Access File to save records.
    My problem is that the next records is saved in the same line, but i need save the record line by line.
    This if what i try but don't work:


    Sub Grabar()
    mascaraquita


    elsize = FileLen(App.Path + "\DatosEmpleados.txt")
    TotalDeRecords = elsize / 150 'this make a new record

    posicion = TotalDeRecords + 1


    empleado.SeguroSocial = Me.txtSeguroSocial.Text
    filler1 = Space(1)
    empleado.Transaccion = Me.txtTransaccion.Text
    empleado.LlaveGenerica = Me.txtLlaveGenerica.Text
    empleado.Operador = Me.txtOperador.Text
    empleado.Terminal = Me.txtTerminal.Text
    empleado.FechadeEntrada = txtFechadeEntrada.Text
    empleado.HoradeEntrada = Me.txtHoradeEntrada.Text
    empleado.numerodetrimestre = Me.cmbanotrimestral.Text
    empleado.Codigo = Me.txtCodigo.Text
    empleado.SalariodeEmpleado = Me.txtSalariodeEmpleado.Text
    empleado.NumerodeCuenta = Me.txtNumerodeCuenta.Text
    filler2 = Space(4)
    empleado.CodigoDot = Me.txtCodigoDot.Text
    filler3 = Space(2)
    empleado.Indicador = Me.txtIndicador.Text
    empleado.NumerodeBatch = Me.txtNumerodeBatch.Text
    empleado.FechadeEfectividad = Me.txtFechadeEfectividad.Text
    empleado.CodigodeBatch = Me.txtCodigodeBatch.Text
    empleado.CodigodePagina = Me.txtCodigodePagina.Text
    empleado.CodigodeOriginacion = Me.cmbCodigodeOriginacion.Text
    empleado.NombredelEmpleado = Me.txtNombredelEmpleado.Text
    empleado.InicialdelEmpleado = Me.txtInicialdelEmpleado.Text
    empleado.ApellidoPaterno = Me.txtApellidoPaterno.Text
    empleado.ApellidoMaterno = Me.txtApelidoMaterno.Text
    filler4 = Space(4)
    filler5 = vbCrLf ' filler5 As String * 2
    Open App.Path + "\DatosEmpleados.txt" For Random As #2 Len = Len(empleado)
    Put #2, posicion, empleado
    Close #2

    End Sub

    How to save in the Text File this line by line?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Random Access File New Line

    What is your type definition for empleado?

    Do you need random access or could you just use the append and print# methods?


    What is this calling ?
    Code:
    mascaraquita
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2011
    Posts
    13

    Re: Random Access File New Line

    empleado as EmpleadoTipo


    Type EmpleadoTipo
    SeguroSocial As String * 9
    filler1 As String * 1
    Transaccion As String * 2
    LlaveGenerica As String * 4
    Operador As String * 8
    Terminal As String * 4
    FechadeEntrada As String * 6
    HoradeEntrada As String * 6
    numerodetrimestre As String * 3
    Codigo As String * 1
    SalariodeEmpleado As String * 7
    NumerodeCuenta As String * 9
    filler2 As String * 4
    CodigoDot As String * 8
    filler3 As String * 2
    Indicador As String * 1
    NumerodeBatch As String * 6
    FechadeEfectividad As String * 6
    CodigodeBatch As String * 3
    CodigodePagina As String * 3
    CodigodeOriginacion As String * 2
    NombredelEmpleado As String * 16
    InicialdelEmpleado As String * 1
    ApellidoPaterno As String * 16
    ApellidoMaterno As String * 16
    filler4 As String * 4
    filler5 As String * 2

    End Type

    Function mascaraquita()
    txtFechadeEntrada.Text = Format(Me.Maskfechadeentrada.Value, "yyMMdd")
    Me.txtFechadeEfectividad.Text = Format(Me.Maskfechadeefectividad.Value, "yyMMdd")
    End Function

    I need use Random to updated records created.

    Thanks any help or suggestion is very appreciate.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Random Access File New Line

    Your size and position calculations seem doubtful to me.
    Code:
    elsize = FileLen(App.Path + "\DatosEmpleados.txt")
    TotalDeRecords = elsize / 150 'this make a new record
    
    posicion = TotalDeRecords + 1
    At least total records should be elsize / Len(empleado)

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Random Access File New Line

    Actually the Len is ok [as long as the type def is not altered] and the code is creating the records but it is not inserting the CRLF so it is just one big run on line.

    Code:
    filler5 = vbCrLf ' filler5 As String * 2
    Should be
    Code:
    empleado.filler5=vbCrLf
    If option explicit were enabled this would have been flagged right away by the IDE.

    Same issue with other instances of filler vars in that code block.


    Always use Option Explicit and you will not run into these short of issues very often and when you do you will see them right away.
    Last edited by DataMiser; February 2nd, 2012 at 07:54 PM.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Sep 2011
    Posts
    13

    Re: Random Access File New Line

    Code:
    filler5 = vbCrLf ' filler5 As String * 2
    ' filler5 As String * 2 this is a comment

    Code:
    elsize = FileLen(App.Path + "\DatosEmpleados.txt")
    TotalDeRecords = elsize / 150 'this make a new record
    
    posicion = TotalDeRecords + 1
    What I can try to the variable posicion.

    I upload my project for a few days, look of what happen in the Text File.
    Don't fill the Hora de Entrada maskeditcontrol,is not working for now.

    This is the zip:
    http://www.4shared.com/zip/wvFKGVIN/..._Basic_6.html?

    Any Help or Suggestion is very appreciate.
    Last edited by pcfry; February 2nd, 2012 at 10:02 PM. Reason: username=castellano

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Random Access File New Line

    You should be using a database, if you have a lot of records (requiring RANDOM access). Otherwise, for a few hundred, just read them ALL at once. When you update, write them ALL again.

    With your method, entering too LONG a textbox field will overwrite your filler spaces. Otherwise, use comma delimited fields.`

    Here's a DB example.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Sep 2011
    Posts
    13

    Unhappy Re: Random Access File New Line

    What happen is that I need a text file that are specification for the declaration of salary.
    I don't know why the next record continue in the same line and I can't find forums threads with similar errors.
    I going to check the max length of the controls maybe I forget something there.
    Last edited by pcfry; February 2nd, 2012 at 11:47 PM.

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Random Access File New Line

    Quote Originally Posted by pcfry View Post
    Code:
    filler5 = vbCrLf ' filler5 As String * 2
    ' filler5 As String * 2 this is a comment

    Code:
    elsize = FileLen(App.Path + "\DatosEmpleados.txt")
    TotalDeRecords = elsize / 150 'this make a new record
     
    posicion = TotalDeRecords + 1
    What I can try to the variable posicion.

    I upload my project for a few days, look of what happen in the Text File.
    Don't fill the Hora de Entrada maskeditcontrol,is not working for now.

    This is the zip:
    http://www.4shared.com/zip/wvFKGVIN/..._Basic_6.html?

    Any Help or Suggestion is very appreciate.
    You did not pay attention.

    I know the latter part of the line is a comment, I am a professional afterall. The first part of that line is where your error is.


    I pointed out the problem and you seem to totally miss it.

    Once again it should be

    Code:
    empleado.filler5=vbCrLf
    You are assigning a bogus var named filler5 with a crlf expecting it to go into your file but that will never happen. You have repeated the same error with all of your filler vars in that block of code. Since the filler vars are not defined in the sub routine then either they are defined as form level, public or not at all and if the latter case that means you are not using Option Explicit which should always be used without exception.

    Your data is being written to the file in the correct position but no new line characters since you have not assigned them to the correct var in your type structure.

    If I am correct and you have not defined Filler5 as string somewhere outside your type statement then if Option Explicit was turned on your IDE would have given you the message Variable not defined and pointed you directly to the problem. The only reason not to use Option Explicit is if you want to be lazy and in the end it will backfire on you almost every time.
    Last edited by DataMiser; February 3rd, 2012 at 12:18 AM.
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,901

    Re: Random Access File New Line

    You are working on an assumption that each line has a length of 150 characters

    Thats fine so long as you guarantee each field has a fixed length, with the total adding up to 150 characters (or less)

    You are assuming this is the fact, but I can see no code which ensures each text field is a fixed length

    For example, if you wanted a field called NAME to always take 35 characters in a line of data, Followed by a 10 Character REFERENCE, then you have to do something like this


    Code:
    Private S35 As String * 35
    Private S10 As String * 10
    Private RecordToWrite as String
    
    Private Sub MakeLine
        RecordToWrite  = ""                'clear output buffer
        S35 = Name.Text
        RecordToWrite =  RecordToWrite & S35
        S10 = Reference.Text
        RecordToWrite =  RecordToWrite & S10
    End Sub
    However, this is a tricky way to handle your problem

    Database as recommended would be ideal

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Random Access File New Line

    Quote Originally Posted by dglienna View Post
    You should be using a database, if you have a lot of records (requiring RANDOM access). Otherwise, for a few hundred, just read them ALL at once. When you update, write them ALL again.

    With your method, entering too LONG a textbox field will overwrite your filler spaces. Otherwise, use comma delimited fields.`

    Here's a DB example.
    I agree that it is better to use a database in most cases.

    However If you want a single record and you know where it is RA is the fastest way to go, I'm sure db engines use it behind the scenes.

    Also the assumption that a longer text would over write the filler vars is incorrect. The strings are defined as a specific length and will always be written that way. If the text in the text box is shorter then it will be padded and if the text is longer then only the first x characters will be used according to the defined string length of the field.


    @George: The type declaration in post #3 shows that it is a fixed length data structure of 150 bytes.
    Last edited by DataMiser; February 3rd, 2012 at 12:25 AM.
    Always use [code][/code] tags when posting code.

  12. #12
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Random Access File New Line

    Except

    Code:
    empleado = new empleado
    Or, from somewhere else?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Random Access File New Line

    Quote Originally Posted by dglienna View Post
    Except

    Code:
    empleado = new empleado
    Or, from somewhere else?


    The code works aside from the fact that the 5 filler vars are not properly assigned so are written to the file without thier assigned values. Still writes 150 byte records to the file.

    The OP likely did not notice that the data was being written due to the fact there are no line feeds. If opened in notepad without word wrap turned on it would appear to never change unless you look closely

    With wordwrap it would be clear that the file is growing and if just that one line I pointed out is changed to use the definition rather than the bogus var a new line will show for each record.

    I've did a lot of work in the past with RA files, not so much the last few years but back in the 90s I used them a lot.

    Edit: There is a missing Dim statement in post#3 I assume that is just a copy/paste error as it would not run at all as is in the post.
    Last edited by DataMiser; February 3rd, 2012 at 03:00 AM.
    Always use [code][/code] tags when posting code.

  14. #14
    Join Date
    Sep 2011
    Posts
    13

    Resolved Re: Random Access File New Line

    DataMiser my apologies had not noticed that, thanks for help me with this.
    This was a fast response time.
    Thanks all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured